diff --git a/wagtail/wagtailusers/templates/wagtailusers/formatted_permissions.html b/wagtail/wagtailusers/templates/wagtailusers/formatted_permissions.html index 21855f282..8122b0767 100644 --- a/wagtail/wagtailusers/templates/wagtailusers/formatted_permissions.html +++ b/wagtail/wagtailusers/templates/wagtailusers/formatted_permissions.html @@ -1,12 +1,12 @@ +

Object permissions

- - {% for content_perms_dict in perms_array %} + {% for content_perms_dict in object_perms %} + + {% endfor %} +
Object Add Change DeleteOthers
{{ content_perms_dict.object|capfirst }} @@ -24,13 +24,18 @@ {% endwith %}
+

Other permissions

+ + {% for perm_tuple in other_perms %} + + {% endfor %} diff --git a/wagtail/wagtailusers/templatetags/wagtailusers_tags.py b/wagtail/wagtailusers/templatetags/wagtailusers_tags.py index 70245718a..074700460 100644 --- a/wagtail/wagtailusers/templatetags/wagtailusers_tags.py +++ b/wagtail/wagtailusers/templatetags/wagtailusers_tags.py @@ -2,24 +2,28 @@ from django import template register = template.Library() + @register.inclusion_tag('wagtailusers/formatted_permissions.html') def format_permissions(permission_bound_field): """ Given a bound field with a queryset of Permission objects, constructs a - list of dictionaries: + list of dictionaries for 'objects': - [ + 'objects': [ { 'object': name_of_some_content_object, 'add': (add_permission_for_object, checked_str) 'change': (change_permission_for_object, checked_str) 'delete': (delete_permission_for_object, checked_str) - 'others': [ - (any_other_permission_for_object, checked_str), - ] }, ] + and a list of other permissions: + + 'others': [ + (any_non_add_change_delete_permission, checked_str), + ] + and returns a table template formatted with this list. """ @@ -28,22 +32,24 @@ def format_permissions(permission_bound_field): content_type_ids = set(permissions.values_list('content_type_id', flat=True)) initial = permission_bound_field.form.initial['permissions'] - perms_array = [] + object_perms = [] + other_perms = [] + for content_type_id in content_type_ids: content_perms = permissions.filter(content_type_id=content_type_id) - content_perms_dict = { - 'others': [] - } + content_perms_dict = {} for perm in content_perms: - content_perms_dict['object'] = perm.content_type.name checked = 'checked="checked"' if perm.id in initial else '' - # identify the three main categories of permission, or bung in - # 'others' list + # identify the three main categories of permission, and assign to + # the relevant dict key, else bung in the 'other_perms' list if perm.codename.split('_')[0] in ['add', 'change', 'delete']: + content_perms_dict['object'] = perm.content_type.name content_perms_dict[perm.codename.split('_')[0]] = (perm, checked) else: - content_perms_dict['others'].append((perm, checked)) - perms_array.append(content_perms_dict) + other_perms.append((perm, checked)) + if content_perms_dict: + object_perms.append(content_perms_dict) return { - 'perms_array': perms_array, - } + 'object_perms': object_perms, + 'other_perms': other_perms, + }
{{ perm_tuple.0.name }} - {% for perm_tuple in content_perms_dict.others %} - {% if not forloop.last %}
{% endif %} - {% endfor %}