Customise permissions widget

This commit is contained in:
Nick Smith 2014-07-02 17:36:05 +01:00
parent 6749f688b6
commit ff7554c9f1
4 changed files with 88 additions and 1 deletions

View file

@ -0,0 +1,37 @@
<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 %}
<tr>
<td>{{ content_perms_dict.object|capfirst }}</td>
<td>
{% with content_perms_dict.add as perm_tuple %}
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}">
{% endwith %}
</td>
<td>
{% with content_perms_dict.change as perm_tuple %}
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}">
{% endwith %}
</td>
<td>
{% with content_perms_dict.delete as perm_tuple %}
<input id="id_permissions_{{ perm_tuple.0.id }}" name="permissions" type="checkbox" {{ perm_tuple.1 }} value="{{ perm_tuple.0.id }}">
{% endwith %}
</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 }}
</label>
{% if not forloop.last %}<br>{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</table>

View file

@ -1,5 +1,6 @@
{% extends "wagtailadmin/base.html" %}
{% load image_tags %}
{% load wagtailusers_tags %}
{% load i18n %}
{% block titletag %}{% trans "Editing" %} {{ group.name }}{% endblock %}
{% block bodyclass %}menu-groups{% endblock %}
@ -13,7 +14,7 @@
<div class="nice-padding">
<ul class="fields">
{% include "wagtailadmin/shared/field_as_li.html" with field=form.name %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.permissions %}
{% format_permissions permission_bound_field=form.permissions %}
<li><input type="submit" value="{% trans 'Save' %}" /></li>
</ul>
</div>

View file

@ -0,0 +1,49 @@
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:
[
{
'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 returns a table template formatted with this list.
"""
permissions = permission_bound_field.field._queryset
# get a distinct list of the content types that these permissions relate to
content_type_ids = set(permissions.values_list('content_type_id', flat=True))
initial = permission_bound_field.form.initial['permissions']
perms_array = []
for content_type_id in content_type_ids:
content_perms = permissions.filter(content_type_id=content_type_id)
content_perms_dict = {
'others': []
}
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
if perm.codename.split('_')[0] in ['add', 'change', 'delete']:
content_perms_dict[perm.codename.split('_')[0]] = (perm, checked)
else:
content_perms_dict['others'].append((perm, checked))
perms_array.append(content_perms_dict)
return {
'perms_array': perms_array,
}