Merge branch 'dev' of ssh://git.auf/auf_rh_dae into dev
[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
549830eb 16COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant'))
7821915f
PP
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
72ac5e55
PP
54@register.inclusion_tag('admin/filter_select.html', takes_context=True)
55def filter_echeance_contrat(context):
eb204143
JPC
56 today = datetime.date.today()
57 three_months = today + datetime.timedelta(days=3*30)
58 six_months = today + datetime.timedelta(days=6*30)
59 twelve_months = today + datetime.timedelta(days=12*30)
60
61 field_name = 'date_fin'
72ac5e55 62 return {'title': u"échéance",
eb204143
JPC
63 'choices': prepare_choices_date(field_name, context, links=(
64 ('Tous', {}),
65 ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'),
66 '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}),
67 ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'),
68 '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}),
69 ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'),
70 '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}),
71 )
72 )}
73
74@register.inclusion_tag('admin/filter_select.html', takes_context=True)
75def filter_debut_contrat(context):
76 year = datetime.date.today().timetuple()[0]
77 this_year = datetime.date(year, 1, 1)
78 next_year = datetime.date(year + 1, 1, 1)
79 last_year = datetime.date(year - 1, 12,31)
80
81 field_name = 'date_debut'
82 return {'title': u"date début",
83 'choices': prepare_choices_date(field_name, context, links=(
84 ('Tous', {}),
2953bb4d 85 ('années à venir', {'%s__gte' % field_name: next_year.strftime('%Y-%m-%d')}),
eb204143
JPC
86 ('cette anneée', {'%s__gte' % field_name: this_year.strftime('%Y-%m-%d'),
87 '%s__lt' % field_name: next_year.strftime('%Y-%m-%d')}),
02d63d23 88 ('années passées', {'%s__lte' % field_name: last_year.strftime('%Y-%m-%d')}),
eb204143
JPC
89 )
90 )}
91
35beb92c
JPC
92@register.inclusion_tag('admin/filter_select.html', takes_context=True)
93def filter_a_venir(context):
94 today = datetime.date.today()
95 year, month, day = datetime.date.today().timetuple()[:3]
96 mois_prochain = datetime.date(year+((month+1)/13), (month+1)%12, 1)
97 this_month = datetime.date(year, month, 1)
98
99 field_name = 'date_debut'
100 return {'title': u"à venir",
101 'choices': prepare_choices_date(field_name, context, links=(
102 ('Tous', {}),
103 ('à venir', {'%s__gt' % field_name: today.strftime('%Y-%m-%d')}),
104 ('à venir mois prochain', {'%s__gte' % field_name: mois_prochain.strftime('%Y-%m-%d')}),
105 ('à venir ce mois', {'%s__gte' % field_name: this_month.strftime('%Y-%m-%d'),
106 '%s__lt' % field_name: mois_prochain.strftime('%Y-%m-%d')}),
107 )
108 )}
109
110
72ac5e55
PP
111
112
af5073aa
PP
113@register.inclusion_tag('admin/filter_select.html', takes_context=True)
114def filter_region_remun(context):
115 return {'title': u"région",
116 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
117
118
119@register.inclusion_tag('admin/filter_select.html', takes_context=True)
120def filter_implantation_remun(context):
121 return {'title': u"implantation",
122 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossiers__poste__implantation', context)}
123
4dade240 124@register.inclusion_tag('rh/rapports/table_header.html', takes_context=True)
9d53bc73
JPC
125def table_header(context, headers):
126 return {'headers': headers}
af5073aa 127
fd009814
PP
128def get_query_string(request, new_params=None, remove=None):
129 if new_params is None: new_params = {}
130 if remove is None: remove = []
131 p = dict(request.GET.items())
132 for r in remove:
133 for k in p.keys():
134 if k.startswith(r):
135 del p[k]
136 for k, v in new_params.items():
137 if v is None:
138 if k in p:
139 del p[k]
140 else:
141 p[k] = v
142 return '?%s' % urlencode(p)
143
144
145def prepare_choices(choices, query_param, context, remove=[]):
146 request = context['request']
147 query_val = request.GET.get(query_param)
148 result = [{'selected': query_val is None,
149 'query_string': get_query_string(request, {}, [query_param] + remove),
150 'display': 'Tout'}]
151 for k, v in choices:
152 result.append({'selected': smart_unicode(k) == query_val,
153 'query_string': get_query_string(request, {query_param: k}, remove),
154 'display': v})
155 return result
72ac5e55
PP
156
157
eb204143 158def prepare_choices_date(field_name, context, links, remove=[]):
72ac5e55
PP
159 request = context['request']
160 params = request.GET
161 field_generic = '%s__' % field_name
162 date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)])
163
164
eb204143 165
72ac5e55
PP
166 result = []
167 for title, param_dict in links:
168 result.append({'selected': date_params == param_dict,
169 'query_string': get_query_string(request, param_dict, [field_generic]),
170 'display': title})
171 return result
9d53bc73 172
9d53bc73 173class SortHeaders:
4dade240
JPC
174 def __init__(self, request, headers, order_field_type, order_field, default_order_type="asc", not_sortable=[]):
175 self.request = request
176 self.order_field_type = order_field_type
177 self.order_field = order_field
9d53bc73 178 self.header_defs = headers
4dade240
JPC
179 self.params = dict(request.GET)
180 if default_order_type not in ('asc', 'desc'):
181 raise AttributeError(u"L'ordre par défaut n'est pas valide")
182 self.default_order_type = default_order_type
183 self.current_order_field = self.params[self.order_field][0] if self.order_field in self.params else None
184 self.current_order_field_type = self.params[self.order_field_type][0] if self.order_field_type in self.params else None
185 self.not_sortable = not_sortable
9d53bc73
JPC
186
187 def headers(self):
4dade240
JPC
188 for h in self.header_defs:
189 params = self.params
190 if h[0] == self.current_order_field:
191 order_type = {'asc': 'desc', 'desc': 'asc'}[self.current_order_field_type]
192 class_order = "%sending" % self.current_order_field_type
193 else:
194 order_type = self.default_order_type
195 class_order = ""
196 params[self.order_field_type] = [order_type]
197 params[self.order_field] = [h[0]]
198 url = "?%s" % "&".join(['%s=%s' % (param, value[0]) for param, value in params.items()])
9d53bc73 199 yield {
4dade240
JPC
200 "class_attr": "sorted %s" % class_order if self.field_sorted(h[0]) else "",
201 "sortable": self.field_sorted(h[0]),
202 "url": url,
203 "text": h[1]
9d53bc73
JPC
204 }
205
4dade240
JPC
206 def field_sorted(self, field):
207 return True if field not in self.not_sortable else False
208
9202c5db
JPC
209@register.filter
210def split(str, splitter):
211 return str.split(splitter)