Skip to content

Commit

Permalink
Enabled bulk changes for ListView matllubos#157
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasRychtecky committed Jan 13, 2016
1 parent ed5d12a commit 5ac8c81
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
27 changes: 20 additions & 7 deletions is_core/generic_views/table_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion is_core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions is_core/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()))
9 changes: 8 additions & 1 deletion is_core/templates/generic_views/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@
{% endif %}
{% endblock %}

{% if enable_bulk_change %}
<span class="btn btn-primary" id="bulk-change-{{ table_slug }}" data-form-snippet="{{ bulk_change_snippet_name }}" data-form-url="{{ bulk_change_form_url }}" data-api-url="{{ api_url }}">{% trans 'Bulk change' %}</span>
{% endif %}

{% block table %}
<table class="{% block table-classes %}grid js-grid table table-striped{% endblock %}" data-resource="{{ api_url }}{% if query_string_filter %}?{{ query_string_filter }}{% endif %}" data-model="{{ module_name|capfirst }}" data-cols="{{ list_display|join:',' }}" data-rest-fields="{{ rest_fieldset }}" data-confirm="{% trans 'Do you really want to delete &#37;s?' %}" data-btn-yes="{% trans 'Yes' %}" data-btn-no="{% trans 'No' %}" data-context="{{ menu_group_pattern_name }}" data-confirm-title="{% trans 'Are you sure?' %}"{% if enable_columns_manager %} data-columns-manager="columns-manager-{{ table_slug }}"{% endif %} id="table-{{ table_slug }}" {% block table-attributes %}{% endblock %}>
<table class="{% block table-classes %}grid js-grid table table-striped{% endblock %}" data-resource="{{ api_url }}{% if query_string_filter %}?{{ query_string_filter }}{% endif %}" data-model="{{ module_name|capfirst }}" data-cols="{{ list_display|join:',' }}" data-rest-fields="{{ rest_fieldset }}" data-confirm="{% trans 'Do you really want to delete &#37;s?' %}" data-btn-yes="{% trans 'Yes' %}" data-btn-no="{% trans 'No' %}" data-context="{{ menu_group_pattern_name }}" data-confirm-title="{% trans 'Are you sure?' %}"{% if enable_columns_manager %} data-columns-manager="columns-manager-{{ table_slug }}"{% endif %}{% if enable_bulk_change %} data-bulk-change="bulk-change-{{ table_slug }}" data-row-selectable="true"{% endif %} id="table-{{ table_slug }}" {% block table-attributes %}{% endblock %}>
<thead>
{% block table-header %}
<tr>
{% if enable_bulk_change %}
<th class="select-all"><input id="select-all-{{ table_slug }}" class="select-all" type="checkbox"></th>
{% endif %}
{% for header in headers %}
<th class="{{ header.field_name }}">
<span{% if header.order_by %} class="sortable" data-col="{{ header.order_by }}"{% endif %}>{{ header.text|capfirst }}</span><div class="sort-direction"><div class="triangle"></div></div>{{ header.filter }}
Expand Down

0 comments on commit 5ac8c81

Please sign in to comment.