bb339655361959ff3564320cce83ab009d4f9240
1 # -*- coding: utf-8 -*-
5 from datamaster_modeles
.models
import Implantation
, Region
6 from django
.utils
.encoding
import smart_unicode
7 from django
.template
import Library
8 from django
.utils
.http
import urlencode
10 from project
.rh
.models
import TypeContrat
16 COMBLE_CHOICES
= (('c', 'Comblé'), ('n', 'Vacant'))
19 @register.inclusion_tag('admin/filter.html', takes_context
=True)
20 def filter_comble(context
):
23 'choices': prepare_choices(COMBLE_CHOICES
, 'comble', context
)
27 @register.inclusion_tag('admin/filter.html', takes_context
=True)
28 def filter_region(context
, prefix
=None):
29 label
= "".join([prefix
or "", "implantation__region"])
32 'choices': prepare_choices(
33 Region
.objects
.values_list('id', 'nom'), label
, context
,
34 remove
=['pays', 'nord_sud']
39 @register.inclusion_tag('admin/filter.html', takes_context
=True)
40 def filter_implantation(context
, prefix
=None):
41 label
= "".join([prefix
or "", "implantation"])
43 'title': u
"implantation",
44 'choices': prepare_choices(
45 Implantation
.objects
.values_list('id', 'nom'), label
, context
50 @register.inclusion_tag('admin/filter.html', takes_context
=True)
51 def filter_region_contrat(context
):
54 'choices': prepare_choices(
55 Region
.objects
.values_list('id', 'nom'),
56 'dossier__poste__implantation__region', context
,
57 remove
=['pays', 'nord_sud']
62 @register.inclusion_tag('admin/filter.html', takes_context
=True)
63 def filter_region_dossier(context
):
66 'choices': prepare_choices(
67 Region
.objects
.values_list('id', 'nom'),
68 'poste__implantation__region', context
,
69 remove
=['pays', 'nord_sud']
74 @register.inclusion_tag('admin/filter.html', takes_context
=True)
75 def filter_implantation_dossier(context
):
77 'title': u
"implantation",
78 'choices': prepare_choices(
79 Implantation
.objects
.values_list('id', 'nom'),
80 'poste__implantation', context
85 @register.inclusion_tag('admin/filter.html', takes_context
=True)
86 def filter_implantation_contrat(context
):
88 'title': u
"implantation",
89 'choices': prepare_choices(
90 Implantation
.objects
.values_list('id', 'nom'),
91 'dossier__poste__implantation', context
96 @register.inclusion_tag('admin/filter.html', takes_context
=True)
97 def filter_type_contrat(context
):
99 'title': u
"type de contrat",
100 'choices': prepare_choices(
101 TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat',
107 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
108 def filter_echeance_contrat(context
):
109 today
= datetime
.date
.today()
110 three_months
= today
+ datetime
.timedelta(days
=3 * 30)
111 six_months
= today
+ datetime
.timedelta(days
=6 * 30)
112 twelve_months
= today
+ datetime
.timedelta(days
=12 * 30)
114 field_name
= 'date_fin'
115 return {'title': u
"échéance",
116 'choices': prepare_choices_date(field_name
, context
, links
=(
118 ('moins de 3 mois', {
119 '%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
120 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')
123 '%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
124 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')
127 '%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
128 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')
134 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
135 def filter_debut_contrat(context
):
136 year
= datetime
.date
.today().timetuple()[0]
137 this_year
= datetime
.date(year
, 1, 1)
138 next_year
= datetime
.date(year
+ 1, 1, 1)
139 last_year
= datetime
.date(year
- 1, 12, 31)
141 field_name
= 'date_debut'
142 return {'title': u
"date début",
143 'choices': prepare_choices_date(field_name
, context
, links
=(
146 '%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')
149 '%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
150 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')
153 '%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')
159 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
160 def filter_a_venir(context
):
161 today
= datetime
.date
.today()
162 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
163 mois_prochain
= datetime
.date(
164 year
+ (month
+ 1) / 13, (month
+ 1) % 12, 1
166 this_month
= datetime
.date(year
, month
, 1)
168 field_name
= 'date_debut'
169 return {'title': u
"à venir",
170 'choices': prepare_choices_date(field_name
, context
, links
=(
173 '%s__gt' % field_name
: today
.strftime('%Y-%m-%d')
175 ('à venir mois prochain', {
176 '%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
178 ('à venir ce mois', {
179 '%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
180 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
186 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
187 def filter_region_remun(context
):
190 'choices': prepare_choices(
191 Region
.objects
.values_list('id', 'nom'),
192 'dossiers__poste__implantation__region', context
,
193 remove
=['pays', 'nord_sud']
198 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
199 def filter_implantation_remun(context
):
201 'title': u
"implantation",
202 'choices': prepare_choices(
203 Implantation
.objects
.values_list('id', 'nom'),
204 'dossiers__poste__implantation', context
209 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
210 def table_header(context
, headers
):
211 return {'headers': headers
}
214 def get_query_string(request
, new_params
=None, remove
=None):
215 if new_params
is None:
219 p
= dict(request
.GET
.items())
224 for k
, v
in new_params
.items():
230 return '?%s' % urlencode(p
)
233 def prepare_choices(choices
, query_param
, context
, remove
=[]):
234 request
= context
['request']
235 query_val
= request
.GET
.get(query_param
)
237 'selected': query_val
is None,
238 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
243 'selected': smart_unicode(k
) == query_val
,
244 'query_string': get_query_string(
245 request
, {query_param
: k
}, remove
252 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
253 request
= context
['request']
255 field_generic
= '%s__' % field_name
257 (k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)
261 for title
, param_dict
in links
:
263 'selected': date_params
== param_dict
,
264 'query_string': get_query_string(
265 request
, param_dict
, [field_generic
]
273 def __init__(self
, request
, headers
, order_field_type
, order_field
,
274 default_order_type
="asc", not_sortable
=[]):
275 self
.request
= request
276 self
.order_field_type
= order_field_type
277 self
.order_field
= order_field
278 self
.header_defs
= headers
279 self
.params
= dict(request
.GET
)
280 if default_order_type
not in ('asc', 'desc'):
281 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
282 self
.default_order_type
= default_order_type
283 self
.current_order_field
= \
284 self
.params
[self
.order_field
][0] \
285 if self
.order_field
in self
.params
else None
286 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] \
287 if self
.order_field_type
in self
.params
else None
288 self
.not_sortable
= not_sortable
291 for h
in self
.header_defs
:
293 if h
[0] == self
.current_order_field
:
295 'asc': 'desc', 'desc': 'asc'
296 }[self
.current_order_field_type
]
297 class_order
= "%sending" % self
.current_order_field_type
299 order_type
= self
.default_order_type
301 params
[self
.order_field_type
] = [order_type
]
302 params
[self
.order_field
] = [h
[0]]
303 url
= "?%s" % "&".join([
304 '%s=%s' % (param
, value
[0]) for param
, value
in params
.items()
308 "sorted %s" % class_order
309 if self
.field_sorted(h
[0]) else ""
311 "sortable": self
.field_sorted(h
[0]),
314 "style_attr": h
[2] if len(h
) > 2 else "",
317 def field_sorted(self
, field
):
318 return True if field
not in self
.not_sortable
else False
322 def split(str, splitter
):
323 return str.split(splitter
)
331 raise Exception("%s does not exist" % key
)
336 if type(v
) == type(float()):
341 def contains(value
, arg
):