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 ugettext_lazy as _
11from superdjango.conf import SUPERDJANGO
12from superdjango.shortcuts import get_user_name
14UserModel = get_user_model()
16# Exports
18__all__ = (
19 "OwnedByListFilter",
20)
22# List Filters
25class OwnedByListFilter(admin.SimpleListFilter):
26 """Filter for ``owned_by``.
28 **Order By**
30 Ordering for the lookup defaults to ``first_name``. You can change this by setting
31 ``SUPERDJANGO_ORDER_USERS_BY`` in your ``settings.py`` file.
33 **Implementation**
35 When extending, set the ``parameter_name`` to the name of the audit field.
37 """
38 title = _("Owned By")
39 parameter_name = "owned_by"
41 def lookups(self, request, model_admin):
43 qs = UserModel.objects.all().order_by(SUPERDJANGO.USER_ORDER_USERS_BY)
45 a = list()
46 for user in qs:
47 a.append((user.pk, get_user_name(user)))
49 return a
51 def queryset(self, request, queryset):
52 lookup = "%s__pk" % self.parameter_name
53 criteria = {lookup: self.value()}
55 if self.value() is not None:
56 return queryset.filter(**criteria)