Retirer debug
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
CommitLineData
fd009814
PP
1# -*- coding: utf-8 -*-
2
72ac5e55
PP
3import datetime
4
fd009814
PP
5from django.utils.encoding import smart_unicode
6from django.template import Library
7from django.utils.http import urlencode
8
9from datamaster_modeles.models import Implantation, Region
23e749e1 10from rh.models import TypeContrat
fd009814
PP
11
12
13register = Library()
14
15
7821915f
PP
16COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Non-comblé'))
17
18
19@register.inclusion_tag('admin/filter.html', takes_context=True)
20def filter_comble(context):
21 return {'title': 'comblé',
22 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)}
23
24
fd009814
PP
25@register.inclusion_tag('admin/filter_select.html', takes_context=True)
26def filter_region(context):
27 return {'title': u"région",
28 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])}
29
30
31@register.inclusion_tag('admin/filter_select.html', takes_context=True)
32def filter_implantation(context):
33 return {'title': u"implantation",
34 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
35
36
23e749e1
PP
37@register.inclusion_tag('admin/filter_select.html', takes_context=True)
38def filter_region_contrat(context):
39 return {'title': u"région",
40 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
41
42
43@register.inclusion_tag('admin/filter_select.html', takes_context=True)
44def filter_implantation_contrat(context):
45 return {'title': u"implantation",
46 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)}
47
48
49@register.inclusion_tag('admin/filter_select.html', takes_context=True)
50def filter_type_contrat(context):
51 return {'title': u"type de contrat",
52 'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)}
53
54
72ac5e55
PP
55@register.inclusion_tag('admin/filter_select.html', takes_context=True)
56def filter_echeance_contrat(context):
57 return {'title': u"échéance",
58 'choices': prepare_choices_date('date_fin', context)}
59
60
fd009814
PP
61def get_query_string(request, new_params=None, remove=None):
62 if new_params is None: new_params = {}
63 if remove is None: remove = []
64 p = dict(request.GET.items())
65 for r in remove:
66 for k in p.keys():
67 if k.startswith(r):
68 del p[k]
69 for k, v in new_params.items():
70 if v is None:
71 if k in p:
72 del p[k]
73 else:
74 p[k] = v
75 return '?%s' % urlencode(p)
76
77
78def prepare_choices(choices, query_param, context, remove=[]):
79 request = context['request']
80 query_val = request.GET.get(query_param)
81 result = [{'selected': query_val is None,
82 'query_string': get_query_string(request, {}, [query_param] + remove),
83 'display': 'Tout'}]
84 for k, v in choices:
85 result.append({'selected': smart_unicode(k) == query_val,
86 'query_string': get_query_string(request, {query_param: k}, remove),
87 'display': v})
88 return result
72ac5e55
PP
89
90
91def prepare_choices_date(field_name, context, remove=[]):
92 request = context['request']
93 params = request.GET
94 field_generic = '%s__' % field_name
95 date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)])
96
97
98 today = datetime.date.today()
99 three_months = today + datetime.timedelta(days=3*30)
100 six_months = today + datetime.timedelta(days=6*30)
101 twelve_months = today + datetime.timedelta(days=12*30)
102
103 links = (
104 ('Tous', {}),
105 ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'),
106 '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}),
107 ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'),
108 '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}),
109 ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'),
110 '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}),
111 )
112
113 result = []
114 for title, param_dict in links:
115 result.append({'selected': date_params == param_dict,
116 'query_string': get_query_string(request, param_dict, [field_generic]),
117 'display': title})
118 return result