from django.utils.translation import ugettext_lazy as _ from wagtail.wagtailadmin.forms import ( BaseGroupCollectionMemberPermissionFormSet, ) from wagtail.wagtailcore import hooks from wagtail.wagtailcore.models import Collection, Page from .utils import get_kfet_root_collection, get_kfet_root_page def get_kfetcms_group_formview_extra(): # Prevents querying the database too soon. from .forms import ( CmsGroupForm, SnippetsCmsGroupForm, prepare_page_permissions_formset, prepare_collection_member_permissions_formset, ) forms = [] # Misc cms-related permissions. forms.append((CmsGroupForm, {'prefix': 'kfetcms'})) # Snippets permissions. forms.append((SnippetsCmsGroupForm, {'prefix': 'kfetcms-snippets'})) # Setup PagePermissionsFormSet for kfet root page descendants only. root_page = get_kfet_root_page() if root_page: pages = Page.objects.descendant_of(root_page, inclusive=True) forms.append( (prepare_page_permissions_formset(pages=pages), {}) ) # Setup CollectionPermissions for kfet root collection descendants only. root_collection = get_kfet_root_collection() if root_collection: collections = Collection.objects.descendant_of( root_collection, inclusive=True) # Retrieve forms based on CollectionMemberPermissionFormSet displayed # by Wagtail admin site. # http://docs.wagtail.io/en/stable/reference/hooks.html#register-group-permission-panel collectionmember_form_classes = [] for fn in hooks.get_hooks('register_group_permission_panel'): form_cls = fn() if issubclass( form_cls, BaseGroupCollectionMemberPermissionFormSet ): collectionmember_form_classes.append(form_cls) # Apply choices limit. for form_cls in collectionmember_form_classes: forms.append(( prepare_collection_member_permissions_formset( form_cls, collections=collections), {}, )) # The 'extra' definition of kfetcms. extra = { 'title': _("Site"), 'description': _( "Permissions liées aux pages à contenu libre." ), 'form_classes': forms, } return extra