djangopermissionswagtailmodeladmin

Looking for wagtail modeladmin PermissionHelper example


In "wagtail_hooks.py" I have the code below. As wagtail admin I can see the StudentModelAdmin, but as a user with restricted access to the admin interface I can't.

I would like to allow users with wagtail admin access and the specific permission below to access the student model admin. How do I go about creating the "CourseRegisterPermission" class?

from wagtail.contrib.modeladmin.options import (ModelAdmin, modeladmin_register)
from wagtail.wagtailcore import hooks
from .models import Participant

@hooks.register('register_permissions')
def view_course_registrations():
    return Permission.objects.filter(codename="view_course_registrations")


class CourseRegisterPermission(PermissionHelper):
    # how do I allow users with the permission to view course registrations 
    # to see the 'StudentModelAdmin" below?


class StudentModelAdmin(ModelAdmin):
    model = Participant
    menu_label = "Student Registrations"
    menu_icon = "group"
    search_fields = ('name', 'supervisor_name')
    list_display = ('name', 'email')
    list_filter = ('course',)
    permission_helper_class = CourseRegisterPermission

I tried to find some examples of wagtail PermissionHelper but wasn't able to find any.

Any hint would be appreciated!


Solution

  • You can use the wagtail.contrib.modeladmin.helpers.PermissionHelper or wagtail.contrib.modeladmin.helpers.PagePermissionHelper permission helper classes from Wagtail's sources as an example. See methods like user_can_list, user_can_create, etc.

    But... Are you sure that you need to define your own permission helper class? It seems to me that you can just create a new (or edit existing) group in the Wagtail admin and give required object permissions to your Participant model.

    On my screenshot Programme is the model that I manage using ModelAdmin.

    enter image description here