date range filter
[auf_rh_dae.git] / project / rh / filters.py
1 # -*- encoding: utf-8 -*-
2
3 import datetime
4 from django.contrib.admin.filterspecs import FilterSpec
5
6 year = datetime.date.today().year
7 start = year + 5
8 end = year - 5
9
10 class DateDebutFieldFilterSpec(FilterSpec):
11 def __init__(self, f, request, params, model, model_admin,
12 field_path=None):
13 super(DateDebutFieldFilterSpec, self).__init__(f, request, params, model,
14 model_admin,
15 field_path=field_path)
16
17 self.field_generic = '%s__' % self.field_path
18
19 self.date_params = dict([(k, v) for k, v in params.items()
20 if k.startswith(self.field_generic)])
21 today = datetime.date.today()
22 self.links = [
23 (u"Toutes" ,{}),
24 ]
25 annees = [i for i in range(start, end, -1)]
26 for i in annees:
27 t = (i, {
28 '%s__gte' % self.field_path: "%s-01-01" % str(i),
29 })
30 self.links.append(t)
31
32 def title(self):
33 return "Date borne gauche"
34
35 def choices(self, cl):
36 for title, param_dict in self.links:
37 yield {'selected': self.date_params == param_dict,
38 'query_string': cl.get_query_string(
39 param_dict,
40 [self.field_generic]),
41 'display': title}
42
43 class DateFinFieldFilterSpec(FilterSpec):
44 def __init__(self, f, request, params, model, model_admin,
45 field_path=None):
46 super(DateFinFieldFilterSpec, self).__init__(f, request, params, model,
47 model_admin,
48 field_path=field_path)
49
50 self.field_generic = '%s__' % self.field_path
51
52 self.date_params = dict([(k, v) for k, v in params.items()
53 if k.startswith(self.field_generic)])
54 today = datetime.date.today()
55 self.links = [
56 (u"Toutes" ,{}),
57 ]
58 annees = [i for i in range(start, end, -1)]
59 for i in annees:
60 t = (i, {
61 '%s__lte' % self.field_path: "%s-12-31" % str(i),
62 })
63 self.links.append(t)
64
65 def title(self):
66 return "Date borne droite"
67
68 def choices(self, cl):
69 for title, param_dict in self.links:
70 yield {'selected': self.date_params == param_dict,
71 'query_string': cl.get_query_string(
72 param_dict,
73 [self.field_generic]),
74 'display': title}
75
76 FilterSpec.filter_specs.insert(0, (lambda f: f.name == 'date_debut', DateDebutFieldFilterSpec))
77 FilterSpec.filter_specs.insert(0, (lambda f: f.name == 'date_fin', DateFinFieldFilterSpec))