Ajout de la recherche temporelle pour les dossiers
[auf_rh_dae.git] / project / rh / templatetags / change_list.py
CommitLineData
a9f403cf
JPC
1from django.template import Library
2from django.db import connection
3from django import forms
4
5register = Library()
6
7@register.simple_tag
8def query_string_builder(cl, name, value):
9 return cl.get_query_string({name: value})
10
11@register.simple_tag
12def add_selected(cl, key, value):
13 return 'selected="selected" ' if key in cl.params and cl.params[key] == value else ''
14
15@register.inclusion_tag('admin/rh/annee_select.html')
16def recherche_par_annees(cl):
17
18 cursor = connection.cursor()
19 cursor.execute("SELECT year(date_debut) FROM rh_dossier WHERE year(date_debut) IS NOT NULL GROUP BY year(date_debut)")
20 set_annees = set(row[0] for row in cursor.fetchall())
21 cursor.execute("SELECT year(date_fin) FROM rh_dossier WHERE year(date_fin) IS NOT NULL GROUP BY year(date_fin)")
22 for row in cursor.fetchall():
23 set_annees.add(row[0])
24 list_annees = list(set_annees)
25 list_annees.insert(0, '')
26
27 class RechercheTemporelle(forms.Form):
28 periode = forms.ChoiceField(
29 choices=((cl.get_query_string({'periode': p }, ('annee', 'date_debut', 'date_fin')), p) for p in cl.PERIODE_CHOICE),
30 widget=forms.Select(attrs = {
31 'onchange' : """window.location=window.location.pathname+this.options[this.selectedIndex].value""",
32 }))
33 annee = forms.ChoiceField(choices=((cl.get_query_string({'annee': a}, {'periode', 'date_debut', 'date_fin'}), a) for a in list_annees),
34 widget=forms.Select(attrs = {
35 'onchange' : """window.location=window.location.pathname+this.options[this.selectedIndex].value""",
36 }))
37 date_debut = forms.DateField()
38 date_fin = forms.DateField()
39 params = cl.params
40 if 'periode' in params:
41 params['periode'] = cl.get_query_string({'periode': params['periode']})
42 if 'annee' in params:
43 params['annee'] = cl.get_query_string({'annee': params['annee']})
44 f = RechercheTemporelle(params)
45 return {
46 'form': f,
47 'plage_date_querystring': cl.get_query_string(remove={'annee', 'periode'})
48 }