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 | ||
9 | from datamaster_modeles.models import Implantation, Region | |
23e749e1 | 10 | from rh.models import TypeContrat |
fd009814 PP |
11 | |
12 | ||
13 | register = Library() | |
14 | ||
15 | ||
549830eb | 16 | COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant')) |
7821915f PP |
17 | |
18 | ||
19 | @register.inclusion_tag('admin/filter.html', takes_context=True) | |
20 | def filter_comble(context): | |
21 | return {'title': 'comblé', | |
22 | 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)} | |
23 | ||
24 | ||
f0f6b03e | 25 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
df37184c JPC |
26 | def filter_region(context, prefix=None): |
27 | ||
28 | label = "".join([prefix or "", "implantation__region"]) | |
fd009814 | 29 | return {'title': u"région", |
df37184c | 30 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), label, context, remove=['pays', 'nord_sud'])} |
fd009814 PP |
31 | |
32 | ||
f0f6b03e | 33 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
df37184c JPC |
34 | def filter_implantation(context, prefix=None): |
35 | ||
36 | label = "".join([prefix or "", "implantation"]) | |
fd009814 | 37 | return {'title': u"implantation", |
df37184c | 38 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), label, context)} |
fd009814 PP |
39 | |
40 | ||
f0f6b03e | 41 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 PP |
42 | def filter_region_contrat(context): |
43 | return {'title': u"région", | |
44 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])} | |
45 | ||
80518280 JPC |
46 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
47 | def filter_region_dossier(context): | |
48 | return {'title': u"région", | |
49 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'poste__implantation__region', context, remove=['pays', 'nord_sud'])} | |
50 | ||
51 | @register.inclusion_tag('admin/filter.html', takes_context=True) | |
52 | def filter_implantation_dossier(context): | |
53 | return {'title': u"implantation", | |
54 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'poste__implantation', context)} | |
55 | ||
23e749e1 | 56 | |
f0f6b03e | 57 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 PP |
58 | def filter_implantation_contrat(context): |
59 | return {'title': u"implantation", | |
60 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)} | |
61 | ||
62 | ||
f0f6b03e | 63 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
23e749e1 PP |
64 | def filter_type_contrat(context): |
65 | return {'title': u"type de contrat", | |
66 | 'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)} | |
67 | ||
72ac5e55 PP |
68 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
69 | def filter_echeance_contrat(context): | |
eb204143 JPC |
70 | today = datetime.date.today() |
71 | three_months = today + datetime.timedelta(days=3*30) | |
72 | six_months = today + datetime.timedelta(days=6*30) | |
73 | twelve_months = today + datetime.timedelta(days=12*30) | |
74 | ||
75 | field_name = 'date_fin' | |
72ac5e55 | 76 | return {'title': u"échéance", |
eb204143 JPC |
77 | 'choices': prepare_choices_date(field_name, context, links=( |
78 | ('Tous', {}), | |
79 | ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'), | |
80 | '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}), | |
81 | ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'), | |
82 | '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}), | |
83 | ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'), | |
84 | '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}), | |
85 | ) | |
86 | )} | |
87 | ||
88 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) | |
89 | def filter_debut_contrat(context): | |
90 | year = datetime.date.today().timetuple()[0] | |
91 | this_year = datetime.date(year, 1, 1) | |
92 | next_year = datetime.date(year + 1, 1, 1) | |
93 | last_year = datetime.date(year - 1, 12,31) | |
94 | ||
95 | field_name = 'date_debut' | |
96 | return {'title': u"date début", | |
97 | 'choices': prepare_choices_date(field_name, context, links=( | |
98 | ('Tous', {}), | |
2953bb4d | 99 | ('années à venir', {'%s__gte' % field_name: next_year.strftime('%Y-%m-%d')}), |
eb204143 JPC |
100 | ('cette anneée', {'%s__gte' % field_name: this_year.strftime('%Y-%m-%d'), |
101 | '%s__lt' % field_name: next_year.strftime('%Y-%m-%d')}), | |
02d63d23 | 102 | ('années passées', {'%s__lte' % field_name: last_year.strftime('%Y-%m-%d')}), |
eb204143 JPC |
103 | ) |
104 | )} | |
105 | ||
35beb92c JPC |
106 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
107 | def filter_a_venir(context): | |
108 | today = datetime.date.today() | |
109 | year, month, day = datetime.date.today().timetuple()[:3] | |
110 | mois_prochain = datetime.date(year+((month+1)/13), (month+1)%12, 1) | |
111 | this_month = datetime.date(year, month, 1) | |
112 | ||
113 | field_name = 'date_debut' | |
114 | return {'title': u"à venir", | |
115 | 'choices': prepare_choices_date(field_name, context, links=( | |
116 | ('Tous', {}), | |
117 | ('à venir', {'%s__gt' % field_name: today.strftime('%Y-%m-%d')}), | |
118 | ('à venir mois prochain', {'%s__gte' % field_name: mois_prochain.strftime('%Y-%m-%d')}), | |
119 | ('à venir ce mois', {'%s__gte' % field_name: this_month.strftime('%Y-%m-%d'), | |
120 | '%s__lt' % field_name: mois_prochain.strftime('%Y-%m-%d')}), | |
121 | ) | |
122 | )} | |
123 | ||
124 | ||
72ac5e55 PP |
125 | |
126 | ||
af5073aa PP |
127 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
128 | def filter_region_remun(context): | |
129 | return {'title': u"région", | |
130 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context, remove=['pays', 'nord_sud'])} | |
131 | ||
132 | ||
133 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) | |
134 | def filter_implantation_remun(context): | |
135 | return {'title': u"implantation", | |
136 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossiers__poste__implantation', context)} | |
137 | ||
4dade240 | 138 | @register.inclusion_tag('rh/rapports/table_header.html', takes_context=True) |
9d53bc73 | 139 | def table_header(context, headers): |
9fd09bdc | 140 | return {'headers': headers} |
af5073aa | 141 | |
fd009814 PP |
142 | def get_query_string(request, new_params=None, remove=None): |
143 | if new_params is None: new_params = {} | |
144 | if remove is None: remove = [] | |
145 | p = dict(request.GET.items()) | |
146 | for r in remove: | |
147 | for k in p.keys(): | |
148 | if k.startswith(r): | |
149 | del p[k] | |
150 | for k, v in new_params.items(): | |
151 | if v is None: | |
152 | if k in p: | |
153 | del p[k] | |
154 | else: | |
155 | p[k] = v | |
156 | return '?%s' % urlencode(p) | |
157 | ||
158 | ||
159 | def prepare_choices(choices, query_param, context, remove=[]): | |
160 | request = context['request'] | |
161 | query_val = request.GET.get(query_param) | |
162 | result = [{'selected': query_val is None, | |
163 | 'query_string': get_query_string(request, {}, [query_param] + remove), | |
164 | 'display': 'Tout'}] | |
165 | for k, v in choices: | |
166 | result.append({'selected': smart_unicode(k) == query_val, | |
167 | 'query_string': get_query_string(request, {query_param: k}, remove), | |
168 | 'display': v}) | |
169 | return result | |
72ac5e55 PP |
170 | |
171 | ||
eb204143 | 172 | def prepare_choices_date(field_name, context, links, remove=[]): |
72ac5e55 PP |
173 | request = context['request'] |
174 | params = request.GET | |
175 | field_generic = '%s__' % field_name | |
176 | date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)]) | |
177 | ||
178 | ||
eb204143 | 179 | |
72ac5e55 PP |
180 | result = [] |
181 | for title, param_dict in links: | |
182 | result.append({'selected': date_params == param_dict, | |
183 | 'query_string': get_query_string(request, param_dict, [field_generic]), | |
184 | 'display': title}) | |
185 | return result | |
9d53bc73 | 186 | |
9d53bc73 | 187 | class SortHeaders: |
4dade240 JPC |
188 | def __init__(self, request, headers, order_field_type, order_field, default_order_type="asc", not_sortable=[]): |
189 | self.request = request | |
190 | self.order_field_type = order_field_type | |
191 | self.order_field = order_field | |
9d53bc73 | 192 | self.header_defs = headers |
4dade240 JPC |
193 | self.params = dict(request.GET) |
194 | if default_order_type not in ('asc', 'desc'): | |
195 | raise AttributeError(u"L'ordre par défaut n'est pas valide") | |
196 | self.default_order_type = default_order_type | |
197 | self.current_order_field = self.params[self.order_field][0] if self.order_field in self.params else None | |
198 | self.current_order_field_type = self.params[self.order_field_type][0] if self.order_field_type in self.params else None | |
199 | self.not_sortable = not_sortable | |
9d53bc73 JPC |
200 | |
201 | def headers(self): | |
4dade240 JPC |
202 | for h in self.header_defs: |
203 | params = self.params | |
204 | if h[0] == self.current_order_field: | |
205 | order_type = {'asc': 'desc', 'desc': 'asc'}[self.current_order_field_type] | |
206 | class_order = "%sending" % self.current_order_field_type | |
207 | else: | |
208 | order_type = self.default_order_type | |
209 | class_order = "" | |
210 | params[self.order_field_type] = [order_type] | |
211 | params[self.order_field] = [h[0]] | |
212 | url = "?%s" % "&".join(['%s=%s' % (param, value[0]) for param, value in params.items()]) | |
9d53bc73 | 213 | yield { |
4dade240 JPC |
214 | "class_attr": "sorted %s" % class_order if self.field_sorted(h[0]) else "", |
215 | "sortable": self.field_sorted(h[0]), | |
216 | "url": url, | |
9fd09bdc JPC |
217 | "text": h[1], |
218 | "style_attr": h[2] if len(h) > 2 else "", | |
9d53bc73 JPC |
219 | } |
220 | ||
4dade240 JPC |
221 | def field_sorted(self, field): |
222 | return True if field not in self.not_sortable else False | |
223 | ||
9202c5db JPC |
224 | @register.filter |
225 | def split(str, splitter): | |
226 | return str.split(splitter) | |
57e2b793 JPC |
227 | |
228 | @register.filter | |
3244a3c2 | 229 | def hash(h, key): |
57e2b793 JPC |
230 | if key in h: |
231 | return h[key] | |
232 | else: | |
233 | raise Exception("%s does not exist" % key) | |
3244a3c2 JPC |
234 | |
235 | @register.filter | |
236 | def is_float(v): | |
237 | if type(v) == type(float()): | |
238 | return True | |
a6927189 JPC |
239 | |
240 | @register.filter() | |
241 | def contains(value, arg): | |
242 | return arg in value |