source: asadb/util/admin.py

space-accessstablestage
Last change on this file was 358b26f, checked in by Alex Dehnert <adehnert@…>, 13 years ago

Filter by whether an item is current (ASA-#99)

OfficeHolder?, SpaceAccessListEntry?, and SpaceAssignment? all track a start and
end time for each of their records. This allows filtering in the admin by
whether a record was previously valid, is currently valid, or will be valid in
the future.

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import datetime
2
3from django.contrib.admin import SimpleListFilter
4
5class TimePeriodFilter(SimpleListFilter):
6    title = 'time period'
7    parameter_name = 'time_period'
8
9    def lookups(self, request, model_admin):
10        return (
11            ('past', 'Past', ),
12            ('present', 'Current', ),
13            ('future', 'Future', ),
14        )
15
16    def queryset(self, request, queryset):
17        value = self.value()
18        now = datetime.datetime.now()
19        if value == None:
20            return queryset
21        elif value == 'past':
22            dct = {
23                '%s__lt' % (self.end_field, ): now,
24            }
25            return queryset.filter(**dct)
26        elif value == 'present':
27            dct = {
28                '%s__lt' % (self.start_field, ): now,
29                '%s__gt' % (self.end_field, ): now,
30            }
31            return queryset.filter(**dct)
32        elif value == 'future':
33            dct = {
34                '%s__gt' % (self.start_field, ): now,
35            }
36            return queryset.filter(**dct)
37        else:
38            raise ValueError, "unknown period %s" % (value, )
Note: See TracBrowser for help on using the repository browser.