Commit | Line | Data |
---|---|---|
fd009814 PP |
1 | # -*- coding: utf-8 -*- |
2 | ||
72ac5e55 PP |
3 | import datetime |
4 | ||
fd009814 PP |
5 | from django.utils.encoding import smart_unicode |
6 | from django.template import Library | |
7 | from django.utils.http import urlencode | |
8 | ||
4c17e518 | 9 | from auf.django.references.models import Implantation, Region |
75f0e87b | 10 | |
82c5e37d DB |
11 | from project import groups |
12 | from project.groups import get_employe_from_user | |
fd009814 | 13 | |
82c5e37d | 14 | from project.rh.models import TypeContrat |
fd009814 PP |
15 | |
16 | register = Library() | |
17 | ||
18 | ||
549830eb | 19 | COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant')) |
7821915f PP |
20 | |
21 | ||
22 | @register.inclusion_tag('admin/filter.html', takes_context=True) | |
23 | def filter_comble(context): | |
b31ce2d7 EMS |
24 | return { |
25 | 'title': 'comblé', | |
26 | 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context) | |
27 | } | |
7821915f PP |
28 | |
29 | ||
f0f6b03e | 30 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
df37184c | 31 | def filter_region(context, prefix=None): |
df37184c | 32 | label = "".join([prefix or "", "implantation__region"]) |
b31ce2d7 EMS |
33 | return { |
34 | 'title': u"région", | |
35 | 'choices': prepare_choices( | |
36 | Region.objects.values_list('id', 'nom'), label, context, | |
37 | remove=['pays', 'nord_sud'] | |
38 | ) | |
39 | } | |
fd009814 PP |
40 | |
41 | ||
f0f6b03e | 42 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
df37184c | 43 | def filter_implantation(context, prefix=None): |
df37184c | 44 | label = "".join([prefix or "", "implantation"]) |
b31ce2d7 EMS |
45 | return { |
46 | 'title': u"implantation", | |
47 | 'choices': prepare_choices( | |
48 | Implantation.objects.values_list('id', 'nom'), label, context | |
49 | ) | |
50 | } | |
fd009814 PP |
51 | |
52 | ||
f0f6b03e | 53 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 | 54 | def filter_region_contrat(context): |
f96ec25c OL |
55 | request = context['request'] |
56 | user_groups = request.user.groups.all() | |
57 | if groups.grp_correspondants_rh in user_groups or\ | |
58 | groups.grp_administrateurs in user_groups or\ | |
59 | groups.grp_directeurs_bureau in user_groups: | |
60 | employe = get_employe_from_user(request.user) | |
61 | regions = Region.objects.filter(id=employe.implantation.region.id) | |
62 | else: | |
63 | regions = Region.objects.all() | |
23e749e1 | 64 | return {'title': u"région", |
f96ec25c | 65 | 'choices': prepare_choices(regions.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])} |
23e749e1 | 66 | |
23e749e1 | 67 | |
80518280 JPC |
68 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
69 | def filter_region_dossier(context): | |
b31ce2d7 EMS |
70 | return { |
71 | 'title': u"région", | |
72 | 'choices': prepare_choices( | |
73 | Region.objects.values_list('id', 'nom'), | |
74 | 'poste__implantation__region', context, | |
75 | remove=['pays', 'nord_sud'] | |
76 | ) | |
77 | } | |
78 | ||
80518280 JPC |
79 | |
80 | @register.inclusion_tag('admin/filter.html', takes_context=True) | |
81 | def filter_implantation_dossier(context): | |
b31ce2d7 EMS |
82 | return { |
83 | 'title': u"implantation", | |
84 | 'choices': prepare_choices( | |
85 | Implantation.objects.values_list('id', 'nom'), | |
86 | 'poste__implantation', context | |
87 | ) | |
88 | } | |
80518280 | 89 | |
23e749e1 | 90 | |
f0f6b03e | 91 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 | 92 | def filter_implantation_contrat(context): |
f96ec25c OL |
93 | request = context['request'] |
94 | user_groups = request.user.groups.all() | |
95 | if groups.grp_correspondants_rh in user_groups or\ | |
96 | groups.grp_administrateurs in user_groups or\ | |
97 | groups.grp_directeurs_bureau in user_groups: | |
98 | employe = get_employe_from_user(request.user) | |
99 | implantations = Implantation.objects.filter(region=employe.implantation.region) | |
100 | else: | |
101 | implantations = Implantation.objects.all() | |
23e749e1 | 102 | return {'title': u"implantation", |
f96ec25c | 103 | 'choices': prepare_choices(implantations.values_list('id', 'nom'), 'dossier__poste__implantation', context)} |
23e749e1 PP |
104 | |
105 | ||
f0f6b03e | 106 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 | 107 | def filter_type_contrat(context): |
b31ce2d7 EMS |
108 | return { |
109 | 'title': u"type de contrat", | |
110 | 'choices': prepare_choices( | |
111 | TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', | |
112 | context | |
113 | ) | |
114 | } | |
115 | ||
23e749e1 | 116 | |
72ac5e55 PP |
117 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
118 | def filter_echeance_contrat(context): | |
eb204143 | 119 | today = datetime.date.today() |
b31ce2d7 EMS |
120 | three_months = today + datetime.timedelta(days=3 * 30) |
121 | six_months = today + datetime.timedelta(days=6 * 30) | |
122 | twelve_months = today + datetime.timedelta(days=12 * 30) | |
eb204143 JPC |
123 | |
124 | field_name = 'date_fin' | |
72ac5e55 | 125 | return {'title': u"échéance", |
eb204143 JPC |
126 | 'choices': prepare_choices_date(field_name, context, links=( |
127 | ('Tous', {}), | |
b31ce2d7 EMS |
128 | ('moins de 3 mois', { |
129 | '%s__gte' % field_name: today.strftime('%Y-%m-%d'), | |
130 | '%s__lte' % field_name: three_months.strftime('%Y-%m-%d') | |
131 | }), | |
132 | ('3 à 6 mois', { | |
133 | '%s__gte' % field_name: three_months.strftime('%Y-%m-%d'), | |
134 | '%s__lte' % field_name: six_months.strftime('%Y-%m-%d') | |
135 | }), | |
136 | ('6 à 12 mois', { | |
137 | '%s__gte' % field_name: six_months.strftime('%Y-%m-%d'), | |
138 | '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d') | |
139 | }), | |
eb204143 JPC |
140 | ) |
141 | )} | |
142 | ||
b31ce2d7 | 143 | |
eb204143 JPC |
144 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
145 | def filter_debut_contrat(context): | |
146 | year = datetime.date.today().timetuple()[0] | |
147 | this_year = datetime.date(year, 1, 1) | |
148 | next_year = datetime.date(year + 1, 1, 1) | |
b31ce2d7 | 149 | last_year = datetime.date(year - 1, 12, 31) |
eb204143 JPC |
150 | |
151 | field_name = 'date_debut' | |
152 | return {'title': u"date début", | |
153 | 'choices': prepare_choices_date(field_name, context, links=( | |
154 | ('Tous', {}), | |
b31ce2d7 EMS |
155 | ('années à venir', { |
156 | '%s__gte' % field_name: next_year.strftime('%Y-%m-%d') | |
157 | }), | |
158 | ('cette anneée', { | |
159 | '%s__gte' % field_name: this_year.strftime('%Y-%m-%d'), | |
160 | '%s__lt' % field_name: next_year.strftime('%Y-%m-%d') | |
161 | }), | |
162 | ('années passées', { | |
163 | '%s__lte' % field_name: last_year.strftime('%Y-%m-%d') | |
164 | }), | |
eb204143 JPC |
165 | ) |
166 | )} | |
167 | ||
b31ce2d7 | 168 | |
35beb92c JPC |
169 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
170 | def filter_a_venir(context): | |
171 | today = datetime.date.today() | |
172 | year, month, day = datetime.date.today().timetuple()[:3] | |
b31ce2d7 EMS |
173 | mois_prochain = datetime.date( |
174 | year + (month + 1) / 13, (month + 1) % 12, 1 | |
175 | ) | |
176 | this_month = datetime.date(year, month, 1) | |
35beb92c JPC |
177 | |
178 | field_name = 'date_debut' | |
179 | return {'title': u"à venir", | |
180 | 'choices': prepare_choices_date(field_name, context, links=( | |
181 | ('Tous', {}), | |
b31ce2d7 EMS |
182 | ('à venir', { |
183 | '%s__gt' % field_name: today.strftime('%Y-%m-%d') | |
184 | }), | |
185 | ('à venir mois prochain', { | |
186 | '%s__gte' % field_name: mois_prochain.strftime('%Y-%m-%d') | |
187 | }), | |
188 | ('à venir ce mois', { | |
189 | '%s__gte' % field_name: this_month.strftime('%Y-%m-%d'), | |
190 | '%s__lt' % field_name: mois_prochain.strftime('%Y-%m-%d') | |
191 | }), | |
35beb92c JPC |
192 | ) |
193 | )} | |
194 | ||
195 | ||
af5073aa PP |
196 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
197 | def filter_region_remun(context): | |
b31ce2d7 EMS |
198 | return { |
199 | 'title': u"région", | |
200 | 'choices': prepare_choices( | |
201 | Region.objects.values_list('id', 'nom'), | |
202 | 'dossiers__poste__implantation__region', context, | |
203 | remove=['pays', 'nord_sud'] | |
204 | ) | |
205 | } | |
af5073aa PP |
206 | |
207 | ||
208 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) | |
209 | def filter_implantation_remun(context): | |
b31ce2d7 EMS |
210 | return { |
211 | 'title': u"implantation", | |
212 | 'choices': prepare_choices( | |
213 | Implantation.objects.values_list('id', 'nom'), | |
214 | 'dossiers__poste__implantation', context | |
215 | ) | |
216 | } | |
217 | ||
af5073aa | 218 | |
4dade240 | 219 | @register.inclusion_tag('rh/rapports/table_header.html', takes_context=True) |
9d53bc73 | 220 | def table_header(context, headers): |
9fd09bdc | 221 | return {'headers': headers} |
af5073aa | 222 | |
b31ce2d7 | 223 | |
fd009814 | 224 | def get_query_string(request, new_params=None, remove=None): |
b31ce2d7 EMS |
225 | if new_params is None: |
226 | new_params = {} | |
227 | if remove is None: | |
228 | remove = [] | |
fd009814 PP |
229 | p = dict(request.GET.items()) |
230 | for r in remove: | |
231 | for k in p.keys(): | |
232 | if k.startswith(r): | |
233 | del p[k] | |
234 | for k, v in new_params.items(): | |
235 | if v is None: | |
236 | if k in p: | |
237 | del p[k] | |
238 | else: | |
239 | p[k] = v | |
240 | return '?%s' % urlencode(p) | |
241 | ||
242 | ||
243 | def prepare_choices(choices, query_param, context, remove=[]): | |
244 | request = context['request'] | |
245 | query_val = request.GET.get(query_param) | |
b31ce2d7 EMS |
246 | result = [{ |
247 | 'selected': query_val is None, | |
248 | 'query_string': get_query_string(request, {}, [query_param] + remove), | |
249 | 'display': 'Tout' | |
250 | }] | |
fd009814 | 251 | for k, v in choices: |
b31ce2d7 EMS |
252 | result.append({ |
253 | 'selected': smart_unicode(k) == query_val, | |
254 | 'query_string': get_query_string( | |
255 | request, {query_param: k}, remove | |
256 | ), | |
257 | 'display': v | |
258 | }) | |
fd009814 | 259 | return result |
72ac5e55 PP |
260 | |
261 | ||
eb204143 | 262 | def prepare_choices_date(field_name, context, links, remove=[]): |
72ac5e55 PP |
263 | request = context['request'] |
264 | params = request.GET | |
265 | field_generic = '%s__' % field_name | |
b31ce2d7 EMS |
266 | date_params = dict([ |
267 | (k, v) for k, v in params.items() if k.startswith(field_generic) | |
268 | ]) | |
72ac5e55 | 269 | |
72ac5e55 PP |
270 | result = [] |
271 | for title, param_dict in links: | |
b31ce2d7 EMS |
272 | result.append({ |
273 | 'selected': date_params == param_dict, | |
274 | 'query_string': get_query_string( | |
275 | request, param_dict, [field_generic] | |
276 | ), | |
277 | 'display': title | |
278 | }) | |
72ac5e55 | 279 | return result |
9d53bc73 | 280 | |
b31ce2d7 | 281 | |
9d53bc73 | 282 | class SortHeaders: |
b31ce2d7 EMS |
283 | def __init__(self, request, headers, order_field_type, order_field, |
284 | default_order_type="asc", not_sortable=[]): | |
4dade240 JPC |
285 | self.request = request |
286 | self.order_field_type = order_field_type | |
287 | self.order_field = order_field | |
9d53bc73 | 288 | self.header_defs = headers |
4dade240 JPC |
289 | self.params = dict(request.GET) |
290 | if default_order_type not in ('asc', 'desc'): | |
291 | raise AttributeError(u"L'ordre par défaut n'est pas valide") | |
292 | self.default_order_type = default_order_type | |
b31ce2d7 EMS |
293 | self.current_order_field = \ |
294 | self.params[self.order_field][0] \ | |
295 | if self.order_field in self.params else None | |
296 | self.current_order_field_type = self.params[self.order_field_type][0] \ | |
297 | if self.order_field_type in self.params else None | |
4dade240 | 298 | self.not_sortable = not_sortable |
9d53bc73 JPC |
299 | |
300 | def headers(self): | |
4dade240 JPC |
301 | for h in self.header_defs: |
302 | params = self.params | |
303 | if h[0] == self.current_order_field: | |
b31ce2d7 EMS |
304 | order_type = { |
305 | 'asc': 'desc', 'desc': 'asc' | |
306 | }[self.current_order_field_type] | |
4dade240 JPC |
307 | class_order = "%sending" % self.current_order_field_type |
308 | else: | |
309 | order_type = self.default_order_type | |
310 | class_order = "" | |
311 | params[self.order_field_type] = [order_type] | |
312 | params[self.order_field] = [h[0]] | |
b31ce2d7 EMS |
313 | url = "?%s" % "&".join([ |
314 | '%s=%s' % (param, value[0]) for param, value in params.items() | |
315 | ]) | |
9d53bc73 | 316 | yield { |
b31ce2d7 EMS |
317 | "class_attr": ( |
318 | "sorted %s" % class_order | |
319 | if self.field_sorted(h[0]) else "" | |
320 | ), | |
4dade240 JPC |
321 | "sortable": self.field_sorted(h[0]), |
322 | "url": url, | |
9fd09bdc JPC |
323 | "text": h[1], |
324 | "style_attr": h[2] if len(h) > 2 else "", | |
9d53bc73 JPC |
325 | } |
326 | ||
4dade240 JPC |
327 | def field_sorted(self, field): |
328 | return True if field not in self.not_sortable else False | |
329 | ||
b31ce2d7 | 330 | |
9202c5db JPC |
331 | @register.filter |
332 | def split(str, splitter): | |
333 | return str.split(splitter) | |
57e2b793 | 334 | |
b31ce2d7 | 335 | |
57e2b793 | 336 | @register.filter |
3244a3c2 | 337 | def hash(h, key): |
57e2b793 JPC |
338 | if key in h: |
339 | return h[key] | |
340 | else: | |
341 | raise Exception("%s does not exist" % key) | |
3244a3c2 | 342 | |
b31ce2d7 | 343 | |
3244a3c2 JPC |
344 | @register.filter |
345 | def is_float(v): | |
346 | if type(v) == type(float()): | |
347 | return True | |
a6927189 | 348 | |
b31ce2d7 | 349 | |
a6927189 JPC |
350 | @register.filter() |
351 | def contains(value, arg): | |
b31ce2d7 | 352 | return arg in value |