Source code for superdjango.db.completed.filters

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

"""

# Imports

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

UserModel = get_user_model()

# Exports

__all__ = (
    "CompletedByListFilter",
)

# Constants

SUPERDJANGO_ORDER_USERS_BY = getattr(settings, "SUPERDJANGO_ORDER_USERS_BY", UserModel.USERNAME_FIELD)

# List Filters


[docs]class CompletedByListFilter(admin.SimpleListFilter): """Filter for ``completed_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. **Implementation** When extending, set the ``parameter_name`` to the name of the audit field. """ title = _("Completed By") parameter_name = "completed_by"
[docs] def lookups(self, request, model_admin): qs = UserModel.objects.all().order_by(SUPERDJANGO_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)