Source code for superdjango.db.archived.filters

"""
Audit filters may be used in the Django Admin to create list filters using the full name of the user.

"""

# Imports

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from superdjango.conf import SUPERDJANGO
from superdjango.shortcuts import get_user_name

UserModel = get_user_model()

# Exports

__all__ = (
    "ArchivedByListFilter",
)

# List Filters


[docs]class ArchivedByListFilter(admin.SimpleListFilter): """Filter for ``archived_by``. **Order By** Ordering for the lookup defaults to ``USERNAME_FIELD``. You can change this by setting ``SUPERDJANGO_ORDER_USERS_BY`` in your ``settings.py`` file. """ title = _("Archived By") parameter_name = "archived_by"
[docs] def lookups(self, request, model_admin): qs = UserModel.objects.all().order_by(SUPERDJANGO.USER_ORDER_USERS_BY) a = list() for user in qs: a.append((user.pk, get_user_name(user))) return a
[docs] def queryset(self, request, queryset): lookup = "%s__pk" % self.parameter_name criteria = {lookup: self.value()} if self.value() is not None: return queryset.filter(**criteria)