Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Audit filters may be used in the Django Admin to create list filters using the full name of the user.
4"""
6# Imports
8from django.contrib import admin
9from django.contrib.auth import get_user_model
10from django.utils.translation import gettext_lazy as _
11from superdjango.conf import SUPERDJANGO
12from superdjango.shortcuts import get_user_name
14UserModel = get_user_model()
16# Exports
18__all__ = (
19 "BaseAuditListFilter",
20 "AddedByListFilter",
21 "ModifiedByListFilter",
22 "ViewedByListFilter",
23)
25# List Filters
28class BaseAuditListFilter(admin.SimpleListFilter):
29 """Base class for creating list filters based on user audit fields.
31 **Order By**
33 Ordering for the lookup defaults to ``USERNAME_FIELD``. You can change this by setting
34 ``SUPERDJANGO_ORDER_USERS_BY`` in your ``settings.py`` file.
36 **Implementation**
38 When extending, set the ``parameter_name`` to the name of the audit field.
40 """
42 def lookups(self, request, model_admin):
44 qs = UserModel.objects.all().order_by(SUPERDJANGO.USER_ORDER_USERS_BY)
46 a = list()
47 for user in qs:
48 a.append((user.pk, get_user_name(user)))
50 return a
52 def queryset(self, request, queryset):
53 lookup = "%s__pk" % self.parameter_name
54 criteria = {lookup: self.value()}
56 if self.value() is not None:
57 return queryset.filter(**criteria)
60class AddedByListFilter(BaseAuditListFilter):
61 """Filter for ``added_by``."""
62 title = _("Added By")
63 parameter_name = "added_by"
66class ModifiedByListFilter(BaseAuditListFilter):
67 """Filter for ``modified_by``."""
68 title = _("Modified By")
69 parameter_name = "modified_by"
72class ViewedByListFilter(BaseAuditListFilter):
73 """Filter for ``viewed_by``."""
74 title = _("Viewed By")
75 parameter_name = "viewed_by"