retrait de actif
[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 class DateDebutFieldFilterSpec(FilterSpec):
7 def __init__(self, f, request, params, model, model_admin,
8 field_path=None):
9 super(DateDebutFieldFilterSpec, self).__init__(f, request, params, model,
10 model_admin,
11 field_path=field_path)
12
13 self.field_generic = '%s__' % self.field_path
14
15 self.date_params = dict([(k, v) for k, v in params.items()
16 if k.startswith(self.field_generic)])
17 today = datetime.date.today()
18 self.links = [
19 (u"Toutes" ,{}),
20 (u"Cette année", {'%s__gte' % self.field_path: "%s-01-01" % str(today.year), }),
21 (u"Sans valeur" ,{'%s__isnull' % self.field_path: str(True)}),
22 ]
23 for i in range(1, 10):
24 t = (today.year-i, {
25 '%s__gte' % self.field_path: "%s-01-01" % str(today.year-i),
26 })
27 self.links.append(t)
28
29 def title(self):
30 return self.field.verbose_name
31
32 def choices(self, cl):
33 for title, param_dict in self.links:
34 yield {'selected': self.date_params == param_dict,
35 'query_string': cl.get_query_string(
36 param_dict,
37 [self.field_generic]),
38 'display': title}
39
40 class DateFinFieldFilterSpec(FilterSpec):
41 def __init__(self, f, request, params, model, model_admin,
42 field_path=None):
43 super(DateFinFieldFilterSpec, self).__init__(f, request, params, model,
44 model_admin,
45 field_path=field_path)
46
47 self.field_generic = '%s__' % self.field_path
48
49 self.date_params = dict([(k, v) for k, v in params.items()
50 if k.startswith(self.field_generic)])
51 today = datetime.date.today()
52 self.links = [
53 (u"Cette année", {'%s__lte' % self.field_path: "%s-12-31" % str(today.year)}),
54 (u"Toutes" ,{}),
55 (u"Sans valeur" ,{'%s__isnull' % self.field_path: str(True)}),
56 ]
57 for i in range(1, 10):
58 t = (today.year-i, {
59 '%s__lte' % self.field_path: "%s-12-31" % str(today.year-i),
60 })
61 self.links.append(t)
62
63 def title(self):
64 return self.field.verbose_name
65
66 def choices(self, cl):
67 for title, param_dict in self.links:
68 yield {'selected': self.date_params == param_dict,
69 'query_string': cl.get_query_string(
70 param_dict,
71 [self.field_generic]),
72 'display': title}
73
74 FilterSpec.filter_specs.insert(0, (lambda f: f.name == 'date_debut', DateDebutFieldFilterSpec))
75 FilterSpec.filter_specs.insert(0, (lambda f: f.name == 'date_fin', DateFinFieldFilterSpec))