5d26dfdc99e33cc74da92c41ec7a35fe018ff2df
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
1 # -*- coding: utf-8 -*-
2
3 from django.utils.encoding import smart_unicode
4 from django.template import Library
5 from django.utils.http import urlencode
6
7 from datamaster_modeles.models import Implantation, Region
8 from rh.models import TypeContrat
9
10
11 register = Library()
12
13
14 COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Non-comblé'))
15
16
17 @register.inclusion_tag('admin/filter.html', takes_context=True)
18 def filter_comble(context):
19 return {'title': 'comblé',
20 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)}
21
22
23 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
24 def filter_region(context):
25 return {'title': u"région",
26 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])}
27
28
29 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
30 def filter_implantation(context):
31 return {'title': u"implantation",
32 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
33
34
35 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
36 def filter_region_contrat(context):
37 return {'title': u"région",
38 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
39
40
41 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
42 def filter_implantation_contrat(context):
43 return {'title': u"implantation",
44 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)}
45
46
47 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
48 def filter_type_contrat(context):
49 return {'title': u"type de contrat",
50 'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)}
51
52
53 def get_query_string(request, new_params=None, remove=None):
54 if new_params is None: new_params = {}
55 if remove is None: remove = []
56 p = dict(request.GET.items())
57 for r in remove:
58 for k in p.keys():
59 if k.startswith(r):
60 del p[k]
61 for k, v in new_params.items():
62 if v is None:
63 if k in p:
64 del p[k]
65 else:
66 p[k] = v
67 return '?%s' % urlencode(p)
68
69
70 def prepare_choices(choices, query_param, context, remove=[]):
71 request = context['request']
72 query_val = request.GET.get(query_param)
73 result = [{'selected': query_val is None,
74 'query_string': get_query_string(request, {}, [query_param] + remove),
75 'display': 'Tout'}]
76 for k, v in choices:
77 result.append({'selected': smart_unicode(k) == query_val,
78 'query_string': get_query_string(request, {query_param: k}, remove),
79 'display': v})
80 return result