membershipfee_filter.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. from django.contrib.admin import SimpleListFilter
  3. import datetime
  4. class MembershipFeeFilter(SimpleListFilter):
  5. # Human-readable title which will be displayed in the
  6. # right admin sidebar just above the filter options.
  7. title = 'Cotisations'
  8. # Parameter for the filter that will be used in the URL query.
  9. parameter_name = 'fee'
  10. def lookups(self, request, model_admin):
  11. """
  12. Returns a list of tuples. The first element in each
  13. tuple is the coded value for the option that will
  14. appear in the URL query. The second element is the
  15. human-readable name for the option that will appear
  16. in the right sidebar.
  17. """
  18. return (
  19. ('ok', 'À jour de cotisation'),
  20. ('retard', 'En retard'),
  21. )
  22. def queryset(self, request, queryset):
  23. """
  24. Returns the filtered queryset based on the value
  25. provided in the query string and retrievable via
  26. `self.value()`.
  27. """
  28. if self.value() == 'ok':
  29. return queryset.filter(
  30. membership_fees__start_date__lte=datetime.date.today,
  31. membership_fees__end_date__gte=datetime.date.today)
  32. if self.value() == 'retard':
  33. return queryset.exclude(
  34. membership_fees__start_date__lte=datetime.date.today,
  35. membership_fees__end_date__gte=datetime.date.today)