Commit | Line | Data |
---|---|---|
fd009814 PP |
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 | ||
9 | ||
10 | register = Library() | |
11 | ||
12 | ||
7821915f PP |
13 | COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Non-comblé')) |
14 | ||
15 | ||
16 | @register.inclusion_tag('admin/filter.html', takes_context=True) | |
17 | def filter_comble(context): | |
18 | return {'title': 'comblé', | |
19 | 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)} | |
20 | ||
21 | ||
fd009814 PP |
22 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
23 | def filter_region(context): | |
24 | return {'title': u"région", | |
25 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])} | |
26 | ||
27 | ||
28 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) | |
29 | def filter_implantation(context): | |
30 | return {'title': u"implantation", | |
31 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)} | |
32 | ||
33 | ||
34 | def get_query_string(request, new_params=None, remove=None): | |
35 | if new_params is None: new_params = {} | |
36 | if remove is None: remove = [] | |
37 | p = dict(request.GET.items()) | |
38 | for r in remove: | |
39 | for k in p.keys(): | |
40 | if k.startswith(r): | |
41 | del p[k] | |
42 | for k, v in new_params.items(): | |
43 | if v is None: | |
44 | if k in p: | |
45 | del p[k] | |
46 | else: | |
47 | p[k] = v | |
48 | return '?%s' % urlencode(p) | |
49 | ||
50 | ||
51 | def prepare_choices(choices, query_param, context, remove=[]): | |
52 | request = context['request'] | |
53 | query_val = request.GET.get(query_param) | |
54 | result = [{'selected': query_val is None, | |
55 | 'query_string': get_query_string(request, {}, [query_param] + remove), | |
56 | 'display': 'Tout'}] | |
57 | for k, v in choices: | |
58 | result.append({'selected': smart_unicode(k) == query_val, | |
59 | 'query_string': get_query_string(request, {query_param: k}, remove), | |
60 | 'display': v}) | |
61 | return result |