Hide keyboard shortcuts

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. 

3 

4""" 

5 

6# Imports 

7 

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 

13 

14UserModel = get_user_model() 

15 

16# Exports 

17 

18__all__ = ( 

19 "ArchivedByListFilter", 

20) 

21 

22# List Filters 

23 

24 

25class ArchivedByListFilter(admin.SimpleListFilter): 

26 """Filter for ``archived_by``. 

27 

28 **Order By** 

29 

30 Ordering for the lookup defaults to ``USERNAME_FIELD``. You can change this by setting 

31 ``SUPERDJANGO_ORDER_USERS_BY`` in your ``settings.py`` file. 

32 

33 """ 

34 title = _("Archived By") 

35 parameter_name = "archived_by" 

36 

37 def lookups(self, request, model_admin): 

38 

39 qs = UserModel.objects.all().order_by(SUPERDJANGO.USER_ORDER_USERS_BY) 

40 

41 a = list() 

42 for user in qs: 

43 a.append((user.pk, get_user_name(user))) 

44 

45 return a 

46 

47 def queryset(self, request, queryset): 

48 lookup = "%s__pk" % self.parameter_name 

49 criteria = {lookup: self.value()} 

50 

51 if self.value() is not None: 

52 return queryset.filter(**criteria)