import avec project.
[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.db import connection
4 from django.template import Library
5
6 from project.rh import change_list as pcl
7
8 register = Library()
9
10 @register.simple_tag
11 def query_string_builder(cl, name, value):
12 return cl.get_query_string({name: value})
13
14 @register.simple_tag
15 def add_selected(cl, key, value):
16 return 'selected="selected" ' if key in cl.params and cl.params[key] == value else ''
17
18 @register.inclusion_tag('admin/rh/annee_select.html')
19 def recherche_par_annees(cl):
20 list_annees = cl.get_annees()
21 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]
22 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]
23 on_change = """window.location=window.location.pathname+this.options[this.selectedIndex].value"""
24
25 class RechercheTemporelle(forms.Form):
26 statut = forms.ChoiceField(choices=statut_choices, widget=forms.Select(attrs = {'onchange' : on_change}))
27 annee = forms.ChoiceField(choices=annee_choices, widget=forms.Select(attrs = {'onchange' : on_change}))
28 date_debut = forms.DateField(widget=adminwidgets.AdminDateWidget)
29 date_fin = forms.DateField(widget=adminwidgets.AdminDateWidget)
30
31 params = cl.params.copy()
32 if pcl.KEY_STATUT in params:
33 params[pcl.KEY_STATUT] = cl.get_query_string({pcl.KEY_STATUT: params[pcl.KEY_STATUT]})
34 if pcl.KEY_ANNEE in params:
35 params[pcl.KEY_ANNEE] = cl.get_query_string({pcl.KEY_ANNEE: params[pcl.KEY_ANNEE]})
36 f = RechercheTemporelle(params)
37 return {
38 'form': f,
39 'plage_date_querystring': cl.get_query_string(remove=(pcl.KEY_ANNEE, pcl.KEY_STATUT))
40 }