Split permissions table display into object permissions and other permissions

This commit is contained in:
Nick Smith 2014-07-04 12:04:49 +01:00
parent afe5283bef
commit 951f8ade47
2 changed files with 33 additions and 22 deletions

View file

@ -1,12 +1,12 @@
<h3>Object permissions</h3>
<table class="listing">
<tr>
<th>Object</th>
<th>Add</th>
<th>Change</th>
<th>Delete</th>
<th>Others</th>
</tr>
{% for content_perms_dict in perms_array %}
{% for content_perms_dict in object_perms %}
<tr>
<td>{{ content_perms_dict.object|capfirst }}</td>
<td>
@ -24,13 +24,18 @@
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}">
{% endwith %}
</td>
</tr>
{% endfor %}
</table>
<h3>Other permissions</h3>
<table class="listing">
{% for perm_tuple in other_perms %}
<tr>
<td>{{ perm_tuple.0.name }}</td>
<td>
{% for perm_tuple in content_perms_dict.others %}
<label for="id_permissions_{{ perm_tuple.0.id }}">
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}"> {{ perm_tuple.0.name }}
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}">
</label>
{% if not forloop.last %}<br>{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}

View file

@ -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,
}