0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Implement register_group_permission_panel hook for adding new panels to the group edit form

This commit is contained in:
Matt Westcott 2016-02-12 20:38:08 +00:00
parent 7f7a4be155
commit 766e2501e4
5 changed files with 66 additions and 16 deletions

View File

@ -145,6 +145,14 @@ Hooks for building new areas of the admin interface (alongside pages, images, do
]
.. _register_group_permission_panel:
``register_group_permission_panel``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add a new panel to the Groups form in the 'settings' area. The callable passed to this hook must return a ModelForm / ModelFormSet-like class, with a constructor that accepts a group object as its ``instance`` keyword argument, and which implements the methods ``save``, ``is_valid``, and ``as_admin_panel`` (which returns the HTML to be included on the group edit page).
.. _register_settings_menu_item:
``register_settings_menu_item``

View File

@ -5,6 +5,7 @@ from django.db import transaction
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import Group, Permission
from django.template.loader import render_to_string
from wagtail.wagtailcore import hooks
from wagtail.wagtailadmin.widgets import AdminPageChooser
@ -336,6 +337,11 @@ class BaseGroupPagePermissionFormSet(forms.BaseFormSet):
for (page, permission_type) in permissions_to_add
])
def as_admin_panel(self):
return render_to_string('wagtailusers/groups/includes/page_permissions_formset.html', {
'formset': self
})
GroupPagePermissionFormSet = forms.formset_factory(
PagePermissionsForm, formset=BaseGroupPagePermissionFormSet, extra=0, can_delete=True

View File

@ -23,9 +23,11 @@
<li>
{% format_permissions permission_bound_field=form.permissions %}
</li>
<li>
{% include "wagtailusers/groups/includes/page_permissions_formset.html" with formset=formset only %}
</li>
{% for panel in permission_panels %}
<li>
{{ panel.as_admin_panel }}
</li>
{% endfor %}
<li><input type="submit" value='{% trans "Add group" %}' /></li>
</ul>
</form>

View File

@ -24,9 +24,11 @@
<li>
{% format_permissions permission_bound_field=form.permissions %}
</li>
<li>
{% include "wagtailusers/groups/includes/page_permissions_formset.html" with formset=formset only %}
</li>
{% for panel in permission_panels %}
<li>
{{ panel.as_admin_panel }}
</li>
{% endfor %}
<li>
<input type="submit" value="{% trans 'Save' %}" />

View File

@ -5,12 +5,26 @@ from django.utils.translation import ugettext as _
from django.views.decorators.vary import vary_on_headers
from wagtail.utils.pagination import paginate
from wagtail.wagtailcore import hooks
from wagtail.wagtailadmin import messages
from wagtail.wagtailadmin.forms import SearchForm
from wagtail.wagtailadmin.utils import permission_required, any_permission_required
from wagtail.wagtailusers.forms import GroupForm, GroupPagePermissionFormSet
_permission_panel_classes = None
def get_permission_panel_classes():
global _permission_panel_classes
if _permission_panel_classes is None:
_permission_panel_classes = [GroupPagePermissionFormSet]
for fn in hooks.get_hooks('register_group_permission_panel'):
_permission_panel_classes.append(fn())
return _permission_panel_classes
@any_permission_required('auth.add_group', 'auth.change_group', 'auth.delete_group')
@vary_on_headers('X-Requested-With')
def index(request):
@ -65,10 +79,16 @@ def create(request):
group = Group()
if request.POST:
form = GroupForm(request.POST, instance=group)
formset = GroupPagePermissionFormSet(request.POST, instance=group)
if form.is_valid() and formset.is_valid():
permission_panels = [
cls(request.POST, instance=group)
for cls in get_permission_panel_classes()
]
if form.is_valid() and all(panel.is_valid() for panel in permission_panels):
form.save()
formset.save()
for panel in permission_panels:
panel.save()
messages.success(request, _("Group '{0}' created.").format(group), buttons=[
messages.button(reverse('wagtailusers_groups:edit', args=(group.id,)), _('Edit'))
])
@ -77,11 +97,14 @@ def create(request):
messages.error(request, _("The group could not be created due to errors."))
else:
form = GroupForm(instance=group)
formset = GroupPagePermissionFormSet(instance=group)
permission_panels = [
cls(instance=group)
for cls in get_permission_panel_classes()
]
return render(request, 'wagtailusers/groups/create.html', {
'form': form,
'formset': formset,
'permission_panels': permission_panels,
})
@ -90,10 +113,16 @@ def edit(request, group_id):
group = get_object_or_404(Group, id=group_id)
if request.POST:
form = GroupForm(request.POST, instance=group)
formset = GroupPagePermissionFormSet(request.POST, instance=group)
if form.is_valid() and formset.is_valid():
permission_panels = [
cls(request.POST, instance=group)
for cls in get_permission_panel_classes()
]
if form.is_valid() and all(panel.is_valid() for panel in permission_panels):
form.save()
formset.save()
for panel in permission_panels:
panel.save()
messages.success(request, _("Group '{0}' updated.").format(group), buttons=[
messages.button(reverse('wagtailusers_groups:edit', args=(group.id,)), _('Edit'))
])
@ -102,12 +131,15 @@ def edit(request, group_id):
messages.error(request, _("The group could not be saved due to errors."))
else:
form = GroupForm(instance=group)
formset = GroupPagePermissionFormSet(instance=group)
permission_panels = [
cls(instance=group)
for cls in get_permission_panel_classes()
]
return render(request, 'wagtailusers/groups/edit.html', {
'group': group,
'form': form,
'formset': formset,
'permission_panels': permission_panels,
})