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