Filtres sur rapport poste
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
CommitLineData
fd009814
PP
1# -*- coding: utf-8 -*-
2
3from django.utils.encoding import smart_unicode
4from django.template import Library
5from django.utils.http import urlencode
6
7from datamaster_modeles.models import Implantation, Region
8
9
10register = Library()
11
12
13@register.inclusion_tag('admin/filter_select.html', takes_context=True)
14def filter_region(context):
15 return {'title': u"région",
16 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])}
17
18
19@register.inclusion_tag('admin/filter_select.html', takes_context=True)
20def filter_implantation(context):
21 return {'title': u"implantation",
22 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
23
24
25def get_query_string(request, new_params=None, remove=None):
26 if new_params is None: new_params = {}
27 if remove is None: remove = []
28 p = dict(request.GET.items())
29 for r in remove:
30 for k in p.keys():
31 if k.startswith(r):
32 del p[k]
33 for k, v in new_params.items():
34 if v is None:
35 if k in p:
36 del p[k]
37 else:
38 p[k] = v
39 return '?%s' % urlencode(p)
40
41
42def prepare_choices(choices, query_param, context, remove=[]):
43 request = context['request']
44 query_val = request.GET.get(query_param)
45 result = [{'selected': query_val is None,
46 'query_string': get_query_string(request, {}, [query_param] + remove),
47 'display': 'Tout'}]
48 for k, v in choices:
49 result.append({'selected': smart_unicode(k) == query_val,
50 'query_string': get_query_string(request, {query_param: k}, remove),
51 'display': v})
52 return result