Skip to content

Admin views

hypha.apply.users.admin_views

User module-attribute

User = get_user_model()

add_user_perm module-attribute

add_user_perm = format(AUTH_USER_APP_LABEL, lower())

change_user_perm module-attribute

change_user_perm = format(AUTH_USER_APP_LABEL, lower())

delete_user_perm module-attribute

delete_user_perm = format(AUTH_USER_APP_LABEL, lower())

UserFilterSet

Bases: WagtailFilterSet

STATUS_CHOICES class-attribute instance-attribute

STATUS_CHOICES = (('inactive', 'INACTIVE'), ('active', 'ACTIVE'))

roles class-attribute instance-attribute

roles = ModelChoiceFilter(queryset=all(), label='Roles', method='filter_by_roles')

status class-attribute instance-attribute

status = ChoiceFilter(choices=STATUS_CHOICES, label='Status', method='filter_by_status')

Meta

model class-attribute instance-attribute
model = User
fields class-attribute instance-attribute
fields = ['roles', 'status', 'is_superuser']

filter_by_roles

filter_by_roles(queryset, name, value)
Source code in hypha/apply/users/admin_views.py
def filter_by_roles(self, queryset, name, value):
    queryset = queryset.filter(groups__name=value)
    return queryset

filter_by_status

filter_by_status(queryset, name, value)
Source code in hypha/apply/users/admin_views.py
def filter_by_status(self, queryset, name, value):
    if value == "active":
        return queryset.filter(is_active=True)
    elif value == "inactive":
        return queryset.filter(is_active=False)
    return queryset

CustomUserIndexView

Bases: Index

Override wagtail's users index view to filter by full_name. This view also allows for the addition of custom fields to the list_export and list filtering.

list_export class-attribute instance-attribute

list_export = ['email', 'full_name', 'slack', 'roles', 'is_superuser', 'is_active', 'date_joined', 'last_login']

default_ordering class-attribute instance-attribute

default_ordering = 'name'

list_filter class-attribute instance-attribute

list_filter = ('is_active')

filterset_class class-attribute instance-attribute

filterset_class = UserFilterSet

get_context_data

get_context_data(*args, object_list=None, **kwargs)
Source code in hypha/apply/users/admin_views.py
def get_context_data(self, *args, object_list=None, **kwargs):
    ctx = super().get_context_data(*args, object_list=object_list, **kwargs)
    ctx["filters"] = self.get_filterset_class()(
        self.request.GET, queryset=self.get_queryset(), request=self.request
    )
    return ctx

get_queryset

get_queryset()

Override the original queryset to filter by full_name, mostly copied from super().get_queryset() with the addition of the custom code

Source code in hypha/apply/users/admin_views.py
def get_queryset(self):
    """
    Override the original queryset to filter by full_name, mostly copied from
    super().get_queryset() with the addition of the custom code
    """
    model_fields = set(self.model_fields)
    if self.is_searching:
        conditions = get_users_filter_query(self.search_query, model_fields)

        # == custom code
        for term in self.search_query.split():
            if "full_name" in model_fields:
                conditions |= Q(full_name__icontains=term)
        # == custom code end

        users = User.objects.filter(self.group_filter & conditions)
    else:
        users = User.objects.filter(self.group_filter)

    if self.locale:
        users = users.filter(locale=self.locale)

    users = users.annotate(
        display_name=Coalesce(
            NullIf("full_name", Value("")), "email", output_field=CharField()
        ),
    )

    if "wagtail_userprofile" in model_fields:
        users = users.select_related("wagtail_userprofile")

    # == custom code
    if "full_name" in model_fields:
        users = users.order_by(Lower("display_name"))
    # == custom code end

    if self.get_ordering() == "username":
        users = users.order_by(User.USERNAME_FIELD)

    if self.get_ordering() == "name":
        users = users.order_by(Lower("display_name"))

    # == custom code
    if not self.group:
        filterset_class = self.get_filterset_class()
        users = filterset_class(
            self.request.GET, queryset=users, request=self.request
        ).qs
    # == end custom code

    return users

CustomGroupIndexView

Bases: IndexView

Overriding of wagtail.users.views.groups.IndexView to allow for the addition of help text to the displayed group names. This is done utilizing the get_queryset method

model class-attribute instance-attribute

model = Group

get_queryset

get_queryset()

Overriding the normal queryset that would return all Group objects, this returned an iterable of groups with custom names containing HTML help text.

Source code in hypha/apply/users/admin_views.py
def get_queryset(self):
    """
    Overriding the normal queryset that would return all Group objects, this returned an iterable of groups with custom names containing HTML help text.
    """
    group_qs = super().get_queryset()

    custom_groups = []

    for group in group_qs:
        # Check if the group is a role
        help_text = getattr(
            roles.registered_roles.get(group.name, {}), "help_text", ""
        )
        if help_text:
            group.name = mark_safe(
                f"{group.name}<p class=group-help-text>{help_text}</p>"
            )

        custom_groups.append(group)

    return custom_groups

CustomGroupViewSet

CustomGroupViewSet(name, **kwargs)

Bases: GroupViewSet

Overriding the wagtail.users.views.groups.GroupViewSet just to use custom users view(index) when getting all users for a group.

Source code in hypha/apply/users/admin_views.py
def __init__(self, name, **kwargs):
    super().__init__(name, **kwargs)

index_view_class class-attribute instance-attribute

index_view_class = CustomGroupIndexView

users_view property

users_view

users_results_view property

users_results_view