From dcfae46c0663099100529d8c644e91aa3c9cb5f1 Mon Sep 17 00:00:00 2001 From: Lukas Rychtecky Date: Wed, 13 Jan 2016 16:44:43 +0100 Subject: [PATCH] Enabled bulk changes for ListView #157 --- is_core/generic_views/table_views.py | 27 ++++++++++++++++------ is_core/main.py | 6 ++++- is_core/patterns.py | 8 +++++-- is_core/templates/generic_views/table.html | 9 +++++++- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/is_core/generic_views/table_views.py b/is_core/generic_views/table_views.py index c642082b..d81c4b2f 100644 --- a/is_core/generic_views/table_views.py +++ b/is_core/generic_views/table_views.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +from django.core.urlresolvers import reverse from django.views.generic.base import TemplateView from django.db.models.fields import FieldDoesNotExist from django.forms.forms import pretty_name @@ -208,18 +209,30 @@ def _get_list_filter(self): def _get_add_url(self): return self.core.get_add_url(self.request) + def is_bulk_change_enabled(self): + return hasattr(self.core, 'is_bulk_change_enabled') and self.core.is_bulk_change_enabled() + def get_context_data(self, **kwargs): context_data = super(TableView, self).get_context_data(**kwargs) context_data.update({ - 'add_url': self._get_add_url(), - 'view_type': self.view_type, - 'add_button_value': self.core.model._ui_meta.add_button_verbose_name % - {'verbose_name': self.core.model._meta.verbose_name, - 'verbose_name_plural': self.core.model._meta.verbose_name_plural}, - 'export_types': self._get_export_types() - }) + 'add_url': self._get_add_url(), + 'view_type': self.view_type, + 'add_button_value': self.core.model._ui_meta.add_button_verbose_name % { + 'verbose_name': self.core.model._meta.verbose_name, + 'verbose_name_plural': self.core.model._meta.verbose_name_plural}, + 'export_types': self._get_export_types(), + 'enable_bulk_change': self.is_bulk_change_enabled(), + 'bulk_change_snippet_name': self.get_bulk_change_snippet_name(), + 'bulk_change_form_url': self.get_bulk_change_form_url(), + }) return context_data + def get_bulk_change_snippet_name(self): + return '-'.join(('default', self.model._meta.object_name.lower(), 'form')) + + def get_bulk_change_form_url(self): + return reverse('IS:bulk-change-' + self.model._meta.object_name.lower()) + def has_get_permission(self, **kwargs): return self.core.has_ui_read_permission(self.request) diff --git a/is_core/main.py b/is_core/main.py index fac00e56..3e96581b 100644 --- a/is_core/main.py +++ b/is_core/main.py @@ -116,6 +116,7 @@ class ModelISCore(PermissionsMixin, ISCore): form_class = SmartModelForm ordering = None + enable_bulk_change = False def get_form_fields(self, request, obj=None): return self.form_fields @@ -178,6 +179,9 @@ def get_list_actions(self, request, obj): def get_default_action(self, request, obj): return None + def is_bulk_change_enabled(self): + return self.enable_bulk_change + class UIISCore(PermissionsUIMixin, ISCore): abstract = True @@ -431,7 +435,7 @@ def get_rest_class(self): def get_resource_patterns(self): resource_patterns = super(RestModelISCore, self).get_resource_patterns() resource_patterns.update(DoubleRestPattern( - self.get_rest_class(), + self.get_rest_class(), self.default_rest_resource_pattern_class, self ).patterns) return resource_patterns diff --git a/is_core/patterns.py b/is_core/patterns.py index a97f1a8e..6f819e49 100644 --- a/is_core/patterns.py +++ b/is_core/patterns.py @@ -230,7 +230,11 @@ def patterns(self): self.resource_class, self.core, ('get', 'put', 'delete'), clone_view_class=False ) result['api'] = self.pattern_class( - 'api-%s' % self.core.get_menu_group_pattern_name(), self.core.site_name, r'^/?$', self.resource_class, self.core, - ('get', 'post'), clone_view_class=False + 'api-%s' % self.core.get_menu_group_pattern_name(), self.core.site_name, r'^/?$', self.resource_class, + self.core, self._get_api_allowed_methods(), clone_view_class=False ) return result + + def _get_api_allowed_methods(self): + return (('get', 'post') + ( + ('put',) if hasattr(self.core, 'is_bulk_change_enabled') and self.core.is_bulk_change_enabled() else ())) diff --git a/is_core/templates/generic_views/table.html b/is_core/templates/generic_views/table.html index 9134bed6..887a0c56 100644 --- a/is_core/templates/generic_views/table.html +++ b/is_core/templates/generic_views/table.html @@ -73,11 +73,18 @@ {% endif %} {% endblock %} + {% if enable_bulk_change %} + {% trans 'Bulk change' %} + {% endif %} + {% block table %} - +
{% block table-header %} + {% if enable_bulk_change %} + + {% endif %} {% for header in headers %}
{{ header.text|capfirst }}
{{ header.filter }}