customising_group_views.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. .. _customising_group_views:
  2. Customising group edit/create views
  3. ===================================
  4. The views for managing groups within the app are collected into a 'viewset' class, which acts as a single point of reference for all shared components of those views, such as forms. By subclassing the viewset, it is possible to override those components and customise the behaviour of the group management interface.
  5. Custom edit/create forms
  6. ^^^^^^^^^^^^^^^^^^^^^^^^
  7. This example shows how to customize forms on the 'edit group' and 'create group' views in the Wagtail
  8. admin.
  9. Let's say you need to connect Active Directory groups with Django groups.
  10. We create a model for Active Directory groups as follows:
  11. .. code-block:: python
  12. from django.contrib.auth.models import Group
  13. from django.db import models
  14. class ADGroup(models.Model):
  15. guid = models.CharField(verbose_name="GUID", max_length=64, db_index=True, unique=True)
  16. name = models.CharField(verbose_name="Group", max_length=255)
  17. domain = models.CharField(verbose_name="Domain", max_length=255, db_index=True)
  18. description = models.TextField(verbose_name="Description", blank=True, null=True)
  19. roles = models.ManyToManyField(Group, verbose_name="Role", related_name="adgroups", blank=True)
  20. class Meta:
  21. verbose_name = "AD group"
  22. verbose_name_plural = "AD groups"
  23. However, there is no role field on the Wagtail group 'edit' or 'create' view.
  24. To add it, inherit from ``wagtail.users.forms.GroupForm`` and add a new field:
  25. .. code-block:: python
  26. from django import forms
  27. from wagtail.users.forms import GroupForm as WagtailGroupForm
  28. from .models import ADGroup
  29. class GroupForm(WagtailGroupForm):
  30. adgroups = forms.ModelMultipleChoiceField(
  31. label="AD groups",
  32. required=False,
  33. queryset=ADGroup.objects.order_by("name"),
  34. )
  35. class Meta(WagtailGroupForm.Meta):
  36. fields = WagtailGroupForm.Meta.fields + ("adgroups",)
  37. def __init__(self, initial=None, instance=None, **kwargs):
  38. if instance is not None:
  39. if initial is None:
  40. initial = {}
  41. initial["adgroups"] = instance.adgroups.all()
  42. super().__init__(initial=initial, instance=instance, **kwargs)
  43. def save(self, commit=True):
  44. instance = super().save()
  45. instance.adgroups.set(self.cleaned_data["adgroups"])
  46. return instance
  47. Now add your custom form into the group viewset by inheriting the default Wagtail
  48. ``GroupViewSet`` class and overriding the ``get_form_class`` method.
  49. .. code-block:: python
  50. from wagtail.users.views.groups import GroupViewSet as WagtailGroupViewSet
  51. from .forms import GroupForm
  52. class GroupViewSet(WagtailGroupViewSet):
  53. def get_form_class(self, for_update=False):
  54. return GroupForm
  55. Add the field to the group 'edit'/'create' templates:
  56. .. code-block:: html+Django
  57. {% extends "wagtailusers/groups/edit.html" %}
  58. {% load wagtailusers_tags wagtailadmin_tags i18n %}
  59. {% block extra_fields %}
  60. {% include "wagtailadmin/shared/field_as_li.html" with field=form.adgroups %}
  61. {% endblock extra_fields %}
  62. Finally we configure the ``wagtail.users`` application to use the custom viewset,
  63. by setting up a custom ``AppConfig`` class. Within your project folder (i.e. the
  64. package containing the top-level settings and urls modules), create ``apps.py``
  65. (if it does not exist already) and add:
  66. .. code-block:: python
  67. from wagtail.users.apps import WagtailUsersAppConfig
  68. class CustomUsersAppConfig(WagtailUsersAppConfig):
  69. group_viewset = "myapplication.someapp.viewsets.GroupViewSet"
  70. Replace ``wagtail.users`` in ``settings.INSTALLED_APPS`` with the path to
  71. ``CustomUsersAppConfig``.
  72. .. code-block:: python
  73. INSTALLED_APPS = [
  74. ...,
  75. "myapplication.apps.CustomUsersAppConfig",
  76. # "wagtail.users",
  77. ...,
  78. ]