membershipfee_filter.py 1.7 KB

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