08045faa5d8a0aeacc72925d2fe0c3215703d305
1 # -*- coding: utf-8 -*-
5 from django
.utils
.encoding
import smart_unicode
6 from django
.template
import Library
7 from django
.utils
.http
import urlencode
9 from auf
.django
.references
.models
import \
10 Implantation
, Region
, ZoneAdministrative
12 from project
import groups
14 from project
.rh
.models
import TypeContrat
19 COMBLE_CHOICES
= (('c', 'Comblé'), ('n', 'Vacant'))
22 @register.inclusion_tag('admin/filter.html', takes_context
=True)
23 def filter_comble(context
):
26 'choices': prepare_choices(COMBLE_CHOICES
, 'comble', context
)
30 @register.inclusion_tag('admin/filter.html', takes_context
=True)
31 def filter_region(context
, prefix
=None):
32 label
= "".join([prefix
or "", "implantation__region"])
35 'choices': prepare_choices(
36 Region
.objects
.values_list('id', 'nom'), label
, context
,
37 remove
=['pays', 'nord_sud']
42 @register.inclusion_tag('admin/filter.html', takes_context
=True)
43 def filter_zone_administrative(context
, prefix
=None):
44 label
= "".join([prefix
or "", "implantation__zone_administrative"])
46 'title': u
"zone administrative",
47 'choices': prepare_choices(
48 ZoneAdministrative
.objects
.values_list('code', 'nom'), label
,
49 context
, remove
=['pays', 'nord_sud']
54 @register.inclusion_tag('admin/filter.html', takes_context
=True)
55 def filter_implantation(context
, prefix
=None):
56 label
= "".join([prefix
or "", "implantation"])
58 'title': u
"implantation",
59 'choices': prepare_choices(
60 Implantation
.objects
.values_list('id', 'nom'), label
, context
65 @register.inclusion_tag('admin/filter.html', takes_context
=True)
66 def filter_zone_administrative_contrat(context
):
67 request
= context
['request']
68 user_groups
= [g
.name
for g
in request
.user
.groups
.all()]
69 if groups
.CORRESPONDANT_RH
in user_groups
or\
70 groups
.ADMINISTRATEURS
in user_groups
or\
71 groups
.DIRECTEUR_DE_BUREAU
in user_groups
:
72 employe
= groups
.get_employe_from_user(request
.user
)
73 zones
= ZoneAdministrative
.objects
.filter(
74 code
=employe
.implantation
.zone_administrative
.code
77 zones
= ZoneAdministrative
.objects
.all()
80 'choices': prepare_choices(
81 zones
.values_list('code', 'nom'),
82 'dossier__poste__implantation__zone_administrative',
83 context
, remove
=['pays', 'nord_sud']
88 @register.inclusion_tag('admin/filter.html', takes_context
=True)
89 def filter_zone_administrative_dossier(context
):
92 'choices': prepare_choices(
93 ZoneAdministrative
.objects
.values_list('code', 'nom'),
94 'poste__implantation__zone_administrative', context
,
95 remove
=['pays', 'nord_sud']
100 @register.inclusion_tag('admin/filter.html', takes_context
=True)
101 def filter_implantation_dossier(context
):
103 'title': u
"implantation",
104 'choices': prepare_choices(
105 Implantation
.objects
.values_list('id', 'nom'),
106 'poste__implantation', context
111 @register.inclusion_tag('admin/filter.html', takes_context
=True)
112 def filter_implantation_contrat(context
):
113 request
= context
['request']
114 user_groups
= [g
.name
for g
in request
.user
.groups
.all()]
115 if groups
.CORRESPONDANT_RH
in user_groups
or\
116 groups
.ADMINISTRATEURS
in user_groups
or\
117 groups
.DIRECTEUR_DE_BUREAU
in user_groups
:
118 employe
= groups
.get_employe_from_user(request
.user
)
119 implantations
= Implantation
.objects
.filter(
120 zone_administrative
=employe
.implantation
.zone_administrative
123 implantations
= Implantation
.objects
.all()
125 'title': u
"implantation",
126 'choices': prepare_choices(
127 implantations
.values_list('id', 'nom'),
128 'dossier__poste__implantation', context
133 @register.inclusion_tag('admin/filter.html', takes_context
=True)
134 def filter_type_contrat(context
):
136 'title': u
"type de contrat",
137 'choices': prepare_choices(
138 TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat',
144 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
145 def filter_echeance_contrat(context
):
146 today
= datetime
.date
.today()
147 three_months
= today
+ datetime
.timedelta(days
=3 * 30)
148 six_months
= today
+ datetime
.timedelta(days
=6 * 30)
149 twelve_months
= today
+ datetime
.timedelta(days
=12 * 30)
151 field_name
= 'date_fin'
152 return {'title': u
"échéance",
153 'choices': prepare_choices_date(field_name
, context
, links
=(
155 ('moins de 3 mois', {
156 '%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
157 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')
160 '%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
161 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')
164 '%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
165 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')
171 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
172 def filter_debut_contrat(context
):
173 year
= datetime
.date
.today().timetuple()[0]
174 this_year
= datetime
.date(year
, 1, 1)
175 next_year
= datetime
.date(year
+ 1, 1, 1)
176 last_year
= datetime
.date(year
- 1, 12, 31)
178 field_name
= 'date_debut'
179 return {'title': u
"date début",
180 'choices': prepare_choices_date(field_name
, context
, links
=(
183 '%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')
186 '%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
187 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')
190 '%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')
196 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
197 def filter_a_venir(context
):
198 today
= datetime
.date
.today()
199 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
200 mois_prochain
= datetime
.date(
201 year
+ (month
+ 1) / 13, (month
+ 1) % 12, 1
203 this_month
= datetime
.date(year
, month
, 1)
205 field_name
= 'date_debut'
206 return {'title': u
"à venir",
207 'choices': prepare_choices_date(field_name
, context
, links
=(
210 '%s__gt' % field_name
: today
.strftime('%Y-%m-%d')
212 ('à venir mois prochain', {
213 '%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
215 ('à venir ce mois', {
216 '%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
217 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
223 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
224 def filter_zone_administrative_remun(context
):
227 'choices': prepare_choices(
228 ZoneAdministrative
.objects
.values_list('code', 'nom'),
229 'dossiers__poste__implantation__zone_administrative', context
,
230 remove
=['pays', 'nord_sud']
235 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
236 def filter_implantation_remun(context
):
238 'title': u
"implantation",
239 'choices': prepare_choices(
240 Implantation
.objects
.values_list('id', 'nom'),
241 'dossiers__poste__implantation', context
246 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
247 def table_header(context
, headers
):
248 return {'headers': headers
}
251 def get_query_string(request
, new_params
=None, remove
=None):
252 if new_params
is None:
256 p
= dict(request
.GET
.items())
261 for k
, v
in new_params
.items():
267 return '?%s' % urlencode(p
)
270 def prepare_choices(choices
, query_param
, context
, remove
=[]):
271 request
= context
['request']
272 query_val
= request
.GET
.get(query_param
)
274 'selected': query_val
is None,
275 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
280 'selected': smart_unicode(k
) == query_val
,
281 'query_string': get_query_string(
282 request
, {query_param
: k
}, remove
289 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
290 request
= context
['request']
292 field_generic
= '%s__' % field_name
294 (k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)
298 for title
, param_dict
in links
:
300 'selected': date_params
== param_dict
,
301 'query_string': get_query_string(
302 request
, param_dict
, [field_generic
]
310 def __init__(self
, request
, headers
, order_field_type
, order_field
,
311 default_order_type
="asc", not_sortable
=[]):
312 self
.request
= request
313 self
.order_field_type
= order_field_type
314 self
.order_field
= order_field
315 self
.header_defs
= headers
316 self
.params
= dict(request
.GET
)
317 if default_order_type
not in ('asc', 'desc'):
318 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
319 self
.default_order_type
= default_order_type
320 self
.current_order_field
= \
321 self
.params
[self
.order_field
][0] \
322 if self
.order_field
in self
.params
else None
323 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] \
324 if self
.order_field_type
in self
.params
else None
325 self
.not_sortable
= not_sortable
328 for h
in self
.header_defs
:
330 if h
[0] == self
.current_order_field
:
332 'asc': 'desc', 'desc': 'asc'
333 }[self
.current_order_field_type
]
334 class_order
= "%sending" % self
.current_order_field_type
336 order_type
= self
.default_order_type
338 params
[self
.order_field_type
] = [order_type
]
339 params
[self
.order_field
] = [h
[0]]
340 url
= "?%s" % "&".join([
341 '%s=%s' % (param
, value
[0]) for param
, value
in params
.items()
345 "sorted %s" % class_order
346 if self
.field_sorted(h
[0]) else ""
348 "sortable": self
.field_sorted(h
[0]),
351 "style_attr": h
[2] if len(h
) > 2 else "",
354 def field_sorted(self
, field
):
355 return True if field
not in self
.not_sortable
else False
359 def split(str, splitter
):
360 return str.split(splitter
)
368 raise Exception("%s does not exist" % key
)
373 if type(v
) == type(float()):
378 def contains(value
, arg
):