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
, context
,
49 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()
124 return {'title': u
"implantation",
125 'choices': prepare_choices(implantations
.values_list('id', 'nom'), 'dossier__poste__implantation', context
)}
128 @register.inclusion_tag('admin/filter.html', takes_context
=True)
129 def filter_type_contrat(context
):
131 'title': u
"type de contrat",
132 'choices': prepare_choices(
133 TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat',
139 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
140 def filter_echeance_contrat(context
):
141 today
= datetime
.date
.today()
142 three_months
= today
+ datetime
.timedelta(days
=3 * 30)
143 six_months
= today
+ datetime
.timedelta(days
=6 * 30)
144 twelve_months
= today
+ datetime
.timedelta(days
=12 * 30)
146 field_name
= 'date_fin'
147 return {'title': u
"échéance",
148 'choices': prepare_choices_date(field_name
, context
, links
=(
150 ('moins de 3 mois', {
151 '%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
152 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')
155 '%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
156 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')
159 '%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
160 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')
166 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
167 def filter_debut_contrat(context
):
168 year
= datetime
.date
.today().timetuple()[0]
169 this_year
= datetime
.date(year
, 1, 1)
170 next_year
= datetime
.date(year
+ 1, 1, 1)
171 last_year
= datetime
.date(year
- 1, 12, 31)
173 field_name
= 'date_debut'
174 return {'title': u
"date début",
175 'choices': prepare_choices_date(field_name
, context
, links
=(
178 '%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')
181 '%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
182 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')
185 '%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')
191 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
192 def filter_a_venir(context
):
193 today
= datetime
.date
.today()
194 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
195 mois_prochain
= datetime
.date(
196 year
+ (month
+ 1) / 13, (month
+ 1) % 12, 1
198 this_month
= datetime
.date(year
, month
, 1)
200 field_name
= 'date_debut'
201 return {'title': u
"à venir",
202 'choices': prepare_choices_date(field_name
, context
, links
=(
205 '%s__gt' % field_name
: today
.strftime('%Y-%m-%d')
207 ('à venir mois prochain', {
208 '%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
210 ('à venir ce mois', {
211 '%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
212 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
218 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
219 def filter_zone_administrative_remun(context
):
222 'choices': prepare_choices(
223 ZoneAdministrative
.objects
.values_list('code', 'nom'),
224 'dossiers__poste__implantation__zone_administrative', context
,
225 remove
=['pays', 'nord_sud']
230 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
231 def filter_implantation_remun(context
):
233 'title': u
"implantation",
234 'choices': prepare_choices(
235 Implantation
.objects
.values_list('id', 'nom'),
236 'dossiers__poste__implantation', context
241 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
242 def table_header(context
, headers
):
243 return {'headers': headers
}
246 def get_query_string(request
, new_params
=None, remove
=None):
247 if new_params
is None:
251 p
= dict(request
.GET
.items())
256 for k
, v
in new_params
.items():
262 return '?%s' % urlencode(p
)
265 def prepare_choices(choices
, query_param
, context
, remove
=[]):
266 request
= context
['request']
267 query_val
= request
.GET
.get(query_param
)
269 'selected': query_val
is None,
270 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
275 'selected': smart_unicode(k
) == query_val
,
276 'query_string': get_query_string(
277 request
, {query_param
: k
}, remove
284 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
285 request
= context
['request']
287 field_generic
= '%s__' % field_name
289 (k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)
293 for title
, param_dict
in links
:
295 'selected': date_params
== param_dict
,
296 'query_string': get_query_string(
297 request
, param_dict
, [field_generic
]
305 def __init__(self
, request
, headers
, order_field_type
, order_field
,
306 default_order_type
="asc", not_sortable
=[]):
307 self
.request
= request
308 self
.order_field_type
= order_field_type
309 self
.order_field
= order_field
310 self
.header_defs
= headers
311 self
.params
= dict(request
.GET
)
312 if default_order_type
not in ('asc', 'desc'):
313 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
314 self
.default_order_type
= default_order_type
315 self
.current_order_field
= \
316 self
.params
[self
.order_field
][0] \
317 if self
.order_field
in self
.params
else None
318 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] \
319 if self
.order_field_type
in self
.params
else None
320 self
.not_sortable
= not_sortable
323 for h
in self
.header_defs
:
325 if h
[0] == self
.current_order_field
:
327 'asc': 'desc', 'desc': 'asc'
328 }[self
.current_order_field_type
]
329 class_order
= "%sending" % self
.current_order_field_type
331 order_type
= self
.default_order_type
333 params
[self
.order_field_type
] = [order_type
]
334 params
[self
.order_field
] = [h
[0]]
335 url
= "?%s" % "&".join([
336 '%s=%s' % (param
, value
[0]) for param
, value
in params
.items()
340 "sorted %s" % class_order
341 if self
.field_sorted(h
[0]) else ""
343 "sortable": self
.field_sorted(h
[0]),
346 "style_attr": h
[2] if len(h
) > 2 else "",
349 def field_sorted(self
, field
):
350 return True if field
not in self
.not_sortable
else False
354 def split(str, splitter
):
355 return str.split(splitter
)
363 raise Exception("%s does not exist" % key
)
368 if type(v
) == type(float()):
373 def contains(value
, arg
):