Added support for new thresholds and groups

Removed support for old thresholds.
Added tabs to edit groups and thresholds.

Change-Id: I51f366b12ee770f1b464d5c4cb93cbb61f3340b3
This commit is contained in:
Stéphane Albert 2015-05-12 20:02:47 +02:00
parent f10b8017a0
commit bfacd0ca63
7 changed files with 503 additions and 19 deletions

View File

@ -50,8 +50,7 @@ class CreateMappingForm(forms.SelfHandlingForm):
cost = forms.DecimalField(label=_("Cost"))
type = forms.ChoiceField(label=_("Type"),
choices=(("flat", _("Flat")),
("rate", _("Rate")),
("threshold", _("Threshold"))))
("rate", _("Rate"))))
group_id = forms.CharField(label=_("Group"), required=False)
def handle(self, request, data):
@ -60,6 +59,49 @@ class CreateMappingForm(forms.SelfHandlingForm):
return mapping_client.create(**mapping)
class CreateGroupForm(forms.SelfHandlingForm):
name = forms.CharField(label=_("Name"))
def handle(self, request, data):
name = data['name']
LOG.info('Creating group with name %s' % (name))
return api.cloudkittyclient(request).hashmap.groups.create(name=name)
class CreateServiceThresholdForm(forms.SelfHandlingForm):
level = forms.DecimalField(label=_("Level"))
cost = forms.DecimalField(label=_("Cost"))
type = forms.ChoiceField(label=_("Type"),
choices=(("flat", _("Flat")),
("rate", _("Rate"))))
group_id = forms.CharField(label=_("Group"), required=False)
service_id = forms.CharField(label=_("Service ID"),
widget=forms.TextInput(
attrs={'readonly': 'readonly'}))
def handle(self, request, data):
client = api.cloudkittyclient(request).hashmap.thresholds
threshold = {k: v for k, v in data.items() if v and v != ''}
return client.create(**threshold)
class CreateFieldThresholdForm(forms.SelfHandlingForm):
level = forms.DecimalField(label=_("Level"))
cost = forms.DecimalField(label=_("Cost"))
type = forms.ChoiceField(label=_("Type"),
choices=(("flat", _("Flat")),
("rate", _("Rate"))))
group_id = forms.CharField(label=_("Group"), required=False)
field_id = forms.CharField(label=_("Field"),
widget=forms.TextInput(
attrs={'readonly': 'readonly'}))
def handle(self, request, data):
client = api.cloudkittyclient(request).hashmap.thresholds
threshold = {k: v for k, v in data.items() if v and v != ''}
return client.create(**threshold)
class CreateFieldMappingForm(CreateMappingForm):
field_id = forms.CharField(label=_("Field ID"),
widget=forms.TextInput(
@ -105,3 +147,27 @@ class EditFieldMappingForm(CreateFieldMappingForm):
mapping = {k: v for k, v in data.items() if v and v != ''}
mapping['mapping_id'] = self.initial['mapping_id']
return mapping_client.update(**mapping)
class EditServiceThresholdForm(CreateServiceThresholdForm):
threshold_id = forms.CharField(label=_("Threshold ID"),
widget=forms.TextInput(
attrs={'readonly': 'readonly'}))
def handle(self, request, data):
threshold_client = api.cloudkittyclient(request).hashmap.thresholds
threshold = {k: v for k, v in data.items() if v and v != ''}
threshold['threshold_id'] = self.initial['threshold_id']
return threshold_client.update(**threshold)
class EditFieldThresholdForm(CreateFieldThresholdForm):
threshold_id = forms.CharField(label=_("Threshold ID"),
widget=forms.TextInput(
attrs={'readonly': 'readonly'}))
def handle(self, request, data):
threshold_client = api.cloudkittyclient(request).hashmap.thresholds
threshold = {k: v for k, v in data.items() if v and v != ''}
threshold['threshold_id'] = self.initial['threshold_id']
return threshold_client.update(**threshold)

View File

@ -59,6 +59,201 @@ class ServicesTable(tables.DataTable):
row_actions = (DeleteService,)
class CreateGroup(tables.LinkAction):
name = "creategroup"
verbose_name = _("Create new Group")
url = 'horizon:admin:hashmap:group_create'
icon = "create"
ajax = True
classes = ("ajax-modal",)
class DeleteGroup(tables.BatchAction):
name = "deletegroup"
verbose_name = _("Delete Group")
action_present = _("Delete")
action_past = _("Deleted")
data_type_singular = _("Group")
data_type_plural = _("Groups")
icon = "remove"
def action(self, request, group_id):
api.cloudkittyclient(request).hashmap.groups.delete(
group_id=group_id
)
class GroupsTable(tables.DataTable):
"""This table list the available groups.
Clicking on a group name sends you to a GroupsTab page.
"""
name = tables.Column('name', verbose_name=_("Name"))
class Meta(object):
name = "groups"
verbose_name = _("Groups")
table_actions = (CreateGroup,)
row_actions = (DeleteGroup,)
class GroupsTab(tabs.TableTab):
name = _("Groups")
slug = "hashmap_groups"
table_classes = (GroupsTable,)
template_name = "horizon/common/_detail_table.html"
preload = True
def get_groups_data(self):
client = api.cloudkittyclient(self.request)
groups = client.hashmap.groups.list()
return api.identify(groups)
class CreateServiceThreshold(tables.LinkAction):
name = "createservicethreshold"
verbose_name = _("Create new Service Threshold")
icon = "create"
ajax = False
classes = ("ajax-modal",)
def get_link_url(self, datum=None):
url = 'horizon:admin:hashmap:service_threshold_create'
service_id = self.table.request.service_id
return reverse(url, args=[service_id])
class CreateFieldThreshold(tables.LinkAction):
name = "createfieldthreshold"
verbose_name = _("Create new Field Threshold")
icon = "create"
ajax = False
classes = ("ajax-modal",)
def get_link_url(self, datum=None):
url = 'horizon:admin:hashmap:field_threshold_create'
field_id = self.table.request.field_id
return reverse(url, args=[field_id])
class DeleteServiceThreshold(tables.BatchAction):
name = "deletetservicehreshold"
verbose_name = _("Delete Service Threshold")
action_present = _("Delete")
action_past = _("Deleted")
data_type_singular = _("Service Threshold")
data_type_plural = _("Service Thresholds")
icon = "remove"
def action(self, request, threshold_id):
api.cloudkittyclient(request).hashmap.thresholds.delete(
threshold_id=threshold_id
)
class DeleteFieldThreshold(tables.BatchAction):
name = "deletefieldthreshold"
verbose_name = _("Delete Field Threshold")
action_present = _("Delete")
action_past = _("Deleted")
data_type_singular = _("Field Threshold")
data_type_plural = _("Field Thresholds")
icon = "remove"
def action(self, request, threshold_id):
api.cloudkittyclient(request).hashmap.thresholds.delete(
threshold_id=threshold_id
)
class EditServiceThreshold(tables.LinkAction):
name = "editservicethreshold"
verbose_name = _("Edit Service Threshold")
icon = "edit"
ajax = True
classes = ("ajax-modal",)
def get_link_url(self, datum=None):
url = 'horizon:admin:hashmap:service_threshold_edit'
return reverse(url, args=[datum.threshold_id])
class ServiceThresholdsTable(tables.DataTable):
"""This table list the available groups.
Clicking on a group name sends you to a GroupsTab page.
"""
threshold_id = tables.Column('id', verbose_name=_("Id"))
level = tables.Column('level', verbose_name=_("Level"))
cost = tables.Column('cost', verbose_name=_("Cost"))
type = tables.Column('type', verbose_name=_("Type"))
group_id = tables.Column('group_id', verbose_name=_("Group"))
class Meta(object):
name = "service_thresholds"
verbose_name = _("Service Threshold")
table_actions = (CreateServiceThreshold,)
row_actions = (EditServiceThreshold, DeleteServiceThreshold)
class ServiceThresholdsTab(tabs.TableTab):
name = _("Service Thresholds")
slug = "hashmap_service_thresholds"
table_classes = (ServiceThresholdsTable,)
template_name = "horizon/common/_detail_table.html"
preload = True
def get_service_thresholds_data(self):
client = api.cloudkittyclient(self.request)
thresholds = client.hashmap.thresholds.list(
service_id=self.request.service_id)
return api.identify(thresholds)
class EditFieldThreshold(tables.LinkAction):
name = "editfieldthreshold"
verbose_name = _("Edit Field Threshold")
icon = "edit"
ajax = True
classes = ("ajax-modal",)
def get_link_url(self, datum=None):
url = 'horizon:admin:hashmap:field_threshold_edit'
return reverse(url, args=[datum.threshold_id])
class FieldThresholdsTable(tables.DataTable):
"""This table list the available groups.
Clicking on a group name sends you to a GroupsTab page.
"""
threshold_id = tables.Column('id', verbose_name=_("Id"))
level = tables.Column('level', verbose_name=_("Level"))
cost = tables.Column('cost', verbose_name=_("Cost"))
type = tables.Column('type', verbose_name=_("Type"))
group_id = tables.Column('group_id', verbose_name=_("Group"))
class Meta(object):
name = "field_thresholds"
verbose_name = _("Field Threshold")
table_actions = (CreateFieldThreshold,)
row_actions = (EditFieldThreshold, DeleteFieldThreshold)
class FieldThresholdsTab(tabs.TableTab):
name = _("Field Thresholds")
slug = "hashmap_field_thresholds"
table_classes = (FieldThresholdsTable,)
template_name = "horizon/common/_detail_table.html"
preload = True
def get_field_thresholds_data(self):
client = api.cloudkittyclient(self.request)
thresholds = client.hashmap.thresholds.list(
field_id=self.request.field_id)
return api.identify(thresholds)
class DeleteField(tables.BatchAction):
name = "deletefield"
verbose_name = _("Delete Field")
@ -206,6 +401,21 @@ class FieldMappingsTable(tables.DataTable):
table_actions = (CreateFieldMapping,)
class FieldMappingsTab(tabs.TableTab):
name = _("Field Mappings")
slug = "hashmap_field_mappings"
table_classes = (FieldMappingsTable,)
template_name = "horizon/common/_detail_table.html"
preload = True
def get_mappings_data(self):
client = api.cloudkittyclient(self.request)
mappings = client.hashmap.mappings.list(
field_id=self.request.field_id
)
return api.identify(mappings)
class MappingsTab(tabs.TableTab):
name = _("Mappings")
slug = "hashmap_mappings"
@ -221,7 +431,13 @@ class MappingsTab(tabs.TableTab):
return api.identify(mappings)
class FieldTabs(tabs.TabGroup):
slug = "field_tabs"
tabs = (FieldThresholdsTab, FieldMappingsTab)
sticky = True
class ServiceTabs(tabs.TabGroup):
slug = "services_tabs"
tabs = (FieldsTab, MappingsTab)
tabs = (FieldsTab, MappingsTab, GroupsTab, ServiceThresholdsTab)
sticky = True

View File

@ -1,13 +1,11 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Fields" %}{% endblock %}
{% block title %}{% trans "Hashmap" %}{% endblock %}
{% block main %}
{{ table.render }}
{{ modules }}
<div class="row-fluid">
<div class="span12">
{{ tab_group.render }}
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Hashmap" %}{% endblock %}
{% block main %}
<div class="row-fluid">
<div class="span12">
<!-- {{ tab_group.render }} -->
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Hashmap" %}{% endblock %}
{% block main %}
<div class="row-fluid">
<div class="span12">
{{ tab_group.render }}
</div>
</div>
{% endblock %}

View File

@ -29,9 +29,27 @@ urlpatterns = patterns(
url(r'^field/(?P<field_id>[^/]+)/?$',
views.FieldView.as_view(),
name='field'),
url(r'^group/(?P<group_id>[^/]+)/?$',
views.GroupView.as_view(),
name='group'),
url(r'^service_threshold/(?P<threshold_id>[^/]+)/?$',
views.ServiceThresholdView.as_view(),
name='service_threshold'),
url(r'^field_threshold/(?P<threshold_id>[^/]+)/?$',
views.FieldThresholdView.as_view(),
name='field_threshold'),
url(r'^create_field/service/(?P<service_id>[^/]+)/?$',
views.FieldCreateView.as_view(),
name='field_create'),
url(r'^create_group/?$',
views.GroupCreateView.as_view(),
name='group_create'),
url(r'^create_threshold/service/(?P<service_id>[^/]+)/?$',
views.ServiceThresholdCreateView.as_view(),
name='service_threshold_create'),
url(r'^create_threshold/field/(?P<field_id>[^/]+)/?$',
views.FieldThresholdCreateView.as_view(),
name='field_threshold_create'),
url(r'^create_mapping/service/(?P<service_id>[^/]+)/?$',
views.ServiceMappingCreateView.as_view(),
name='service_mapping_create'),
@ -44,4 +62,10 @@ urlpatterns = patterns(
url(r'^edit_mapping/field/(?P<mapping_id>[^/]+)/?$',
views.FieldMappingEditView.as_view(),
name='field_mapping_edit'),
url(r'^edit_threshold/service/(?P<threshold_id>[^/]+)/?$',
views.ServiceThresholdEditView.as_view(),
name='service_threshold_edit'),
url(r'^edit_threshold/field/(?P<threshold_id>[^/]+)/?$',
views.FieldThresholdEditView.as_view(),
name='field_threshold_edit'),
)

View File

@ -57,8 +57,8 @@ class ServiceCreateView(forms.ModalFormView):
return obj.service_id
class FieldView(tables.DataTableView):
table_class = hashmap_tables.FieldMappingsTable
class FieldView(tabs.TabbedTableView):
tab_group_class = hashmap_tables.FieldTabs
template_name = 'admin/hashmap/field_details.html'
def get(self, *args, **kwargs):
@ -69,12 +69,6 @@ class FieldView(tables.DataTableView):
self.page_title = "Hashmap Field : %s" % field.name
return super(FieldView, self).get(*args, **kwargs)
def get_data(self):
out = api.cloudkittyclient(self.request).hashmap.mappings.list(
field_id=self.kwargs['field_id']
)
return api.identify(out)
class FieldCreateView(forms.ModalFormView):
form_class = hashmap_forms.CreateFieldForm
@ -192,3 +186,167 @@ class FieldMappingEditView(FieldMappingCreateView):
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:field',
args=(self.initial['field_id'], ))
class GroupCreateView(forms.ModalFormView):
form_class = hashmap_forms.CreateGroupForm
template_name = 'horizon/common/modal_form.html'
success_url = reverse_lazy('horizon:admin:hashmap:index')
submit_url = reverse_lazy('horizon:admin:hashmap:group_create')
def get_object_id(self, obj):
return obj.group_id
'''
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:group',
args=(kwargs['group_id'], ))
'''
class ServiceThresholdCreateView(forms.ModalFormView):
form_class = hashmap_forms.CreateServiceThresholdForm
template_name = 'horizon/common/modal_form.html'
success_url = 'horizon:admin:hashmap:service'
submit_url = 'horizon:admin:hashmap:service_threshold_create'
def get_object_id(self, obj):
return obj.field_id
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:service',
args=(self.kwargs['service_id'],))
def get_context_data(self, **kwargs):
context = super(ServiceThresholdCreateView,
self).get_context_data(**kwargs)
context["service_id"] = self.kwargs.get('service_id')
args = (context['service_id'],)
context['submit_url'] = reverse_lazy(self.submit_url, args=args)
return context
def get_initial(self):
return {"service_id": self.kwargs["service_id"]}
class ServiceThresholdEditView(ServiceThresholdCreateView):
form_class = hashmap_forms.EditServiceThresholdForm
submit_url = 'horizon:admin:hashmap:service_threshold_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.thresholds.get(
threshold_id=self.kwargs['threshold_id'])
self.initial = out.to_dict()
return self.initial
def get_context_data(self, **kwargs):
context = super(ServiceThresholdEditView,
self).get_context_data(**kwargs)
context["threshold_id"] = self.kwargs.get('threshold_id')
context['submit_url'] = reverse_lazy(self.submit_url,
args=(context['threshold_id'], ))
return context
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:service',
args=(self.initial['service_id'], ))
class ServiceThresholdView(tabs.TabbedTableView):
tab_group_class = hashmap_tables.ServiceThresholdsTab
def get(self, *args, **kwargs):
threshold = api.cloudkittyclient(self.request).hashmap.thresholds.get(
threshold_id=kwargs['threshold_id']
)
self.request.threshold_id = threshold.threshold_id
self.page_title = "Hashmap Threshold : %s" % threshold.threshold_id
return super(ServiceThresholdView, self).get(*args, **kwargs)
def get_data(self):
out = api.cloudkittyclient(self.request).hashmaps.thresholds.list(
threshold_id=self.kwargs['threshold_id'])
return api.identify(out)
class FieldThresholdCreateView(forms.ModalFormView):
form_class = hashmap_forms.CreateFieldThresholdForm
template_name = 'horizon/common/modal_form.html'
success_url = 'horizon:admin:hashmap:field'
submit_url = 'horizon:admin:hashmap:field_threshold_create'
def get_object_id(self, obj):
return obj.field_id
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:field',
args=(self.kwargs['field_id'],))
def get_context_data(self, **kwargs):
context = super(FieldThresholdCreateView,
self).get_context_data(**kwargs)
context["field_id"] = self.kwargs.get('field_id')
args = (context['field_id'],)
context['submit_url'] = reverse_lazy(self.submit_url, args=args)
return context
def get_initial(self):
return {"field_id": self.kwargs["field_id"]}
class FieldThresholdEditView(FieldThresholdCreateView):
form_class = hashmap_forms.EditFieldThresholdForm
submit_url = 'horizon:admin:hashmap:field_threshold_edit'
def get_initial(self):
out = api.cloudkittyclient(self.request).hashmap.thresholds.get(
threshold_id=self.kwargs['threshold_id'])
self.initial = out.to_dict()
return self.initial
def get_context_data(self, **kwargs):
context = super(FieldThresholdEditView,
self).get_context_data(**kwargs)
context["threshold_id"] = self.kwargs.get('threshold_id')
context['submit_url'] = reverse_lazy(self.submit_url,
args=(context['threshold_id'], ))
return context
def get_success_url(self, **kwargs):
return reverse('horizon:admin:hashmap:field',
args=(self.initial['field_id'], ))
class FieldThresholdView(tabs.TabbedTableView):
tab_group_class = hashmap_tables.FieldThresholdsTab
def get(self, *args, **kwargs):
threshold = api.cloudkittyclient(self.request).hashmap.thresholds.get(
threshold_id=kwargs['threshold_id']
)
self.request.threshold_id = threshold.threshold_id
self.page_title = "Hashmap Threshold : %s" % threshold.threshold_id
return super(FieldThresholdView, self).get(*args, **kwargs)
def get_data(self):
out = api.cloudkittyclient(self.request).hashmaps.thresholds.list(
threshold_id=self.kwargs['threshold_id'])
return api.identify(out)
class GroupView(tabs.TabbedTableView):
tab_group_class = hashmap_tables.GroupsTab
template_name = 'admin/hashmap/group_details.html'
def get(self, *args, **kwargs):
group = api.cloudkittyclient(self.request).hashmap.groups.get(
group_id=kwargs['group_id']
)
self.request.group_id = group.group_id
self.page_title = "Hashmap Group : %s" % group.name
return super(GroupView, self).get(*args, **kwargs)
def get_data(self):
out = api.cloudkittyclient(self.request).hashmap.groups.list(
)
return api.identify(out)