fix restore recherche temporelle
[auf_rh_dae.git] / project / rh / templatetags / change_list.py
1 from django import forms
2 from django.contrib.admin import widgets as adminwidgets
3 from django.template import Library
4
5 from project.rh import change_list as pcl
6
7 register = Library()
8
9 @register.simple_tag
10 def query_string_builder(cl, name, value):
11 return cl.get_query_string({name: value})
12
13 @register.simple_tag
14 def add_selected(cl, key, value):
15 return 'selected="selected" ' if key in cl.params and cl.params[key] == value else ''
16
17 @register.inclusion_tag('admin/rh/annee_select.html')
18 def recherche_par_annees(cl):
19 list_annees = cl.get_annees()
20 statut_choices = [(cl.get_query_string(remove=(pcl.KEY_STATUT,)), 'Tous')] + [(cl.get_query_string({pcl.KEY_STATUT : p }, (pcl.KEY_ANNEE, pcl.KEY_DATE_DEBUT, pcl.KEY_DATE_FIN)), p) for p in cl.STATUT_CHOICES]
21 annee_choices = [(cl.get_query_string(remove=(pcl.KEY_ANNEE,)), 'Toutes')] + [(cl.get_query_string({pcl.KEY_ANNEE : a}, (pcl.KEY_STATUT , pcl.KEY_DATE_DEBUT, pcl.KEY_DATE_FIN)), a) for a in list_annees]
22 on_change = """window.location=window.location.pathname+this.options[this.selectedIndex].value"""
23
24 class RechercheTemporelle(forms.Form):
25 statut = forms.ChoiceField(choices=statut_choices, widget=forms.Select(attrs = {'onchange' : on_change}))
26 annee = forms.ChoiceField(choices=annee_choices, widget=forms.Select(attrs = {'onchange' : on_change}))
27 date_debut = forms.DateField(widget=adminwidgets.AdminDateWidget)
28 date_fin = forms.DateField(widget=adminwidgets.AdminDateWidget)
29
30 params = cl.params.copy()
31 if pcl.KEY_STATUT in params:
32 params[pcl.KEY_STATUT] = cl.get_query_string({pcl.KEY_STATUT: params[pcl.KEY_STATUT]})
33 if pcl.KEY_ANNEE in params:
34 params[pcl.KEY_ANNEE] = cl.get_query_string({pcl.KEY_ANNEE: params[pcl.KEY_ANNEE]})
35 f = RechercheTemporelle(params)
36 return {
37 'form': f,
38 'plage_date_querystring': cl.get_query_string(remove=(pcl.KEY_ANNEE, pcl.KEY_STATUT))
39 }