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 Implantation
, Region
11 from project
.rh
.models
import TypeContrat
17 COMBLE_CHOICES
= (('c', 'Comblé'), ('n', 'Vacant'))
20 @register.inclusion_tag('admin/filter.html', takes_context
=True)
21 def filter_comble(context
):
24 'choices': prepare_choices(COMBLE_CHOICES
, 'comble', context
)
28 @register.inclusion_tag('admin/filter.html', takes_context
=True)
29 def filter_region(context
, prefix
=None):
30 label
= "".join([prefix
or "", "implantation__region"])
33 'choices': prepare_choices(
34 Region
.objects
.values_list('id', 'nom'), label
, context
,
35 remove
=['pays', 'nord_sud']
40 @register.inclusion_tag('admin/filter.html', takes_context
=True)
41 def filter_implantation(context
, prefix
=None):
42 label
= "".join([prefix
or "", "implantation"])
44 'title': u
"implantation",
45 'choices': prepare_choices(
46 Implantation
.objects
.values_list('id', 'nom'), label
, context
51 @register.inclusion_tag('admin/filter.html', takes_context
=True)
52 def filter_region_contrat(context
):
55 'choices': prepare_choices(
56 Region
.objects
.values_list('id', 'nom'),
57 'dossier__poste__implantation__region', context
,
58 remove
=['pays', 'nord_sud']
63 @register.inclusion_tag('admin/filter.html', takes_context
=True)
64 def filter_region_dossier(context
):
67 'choices': prepare_choices(
68 Region
.objects
.values_list('id', 'nom'),
69 'poste__implantation__region', context
,
70 remove
=['pays', 'nord_sud']
75 @register.inclusion_tag('admin/filter.html', takes_context
=True)
76 def filter_implantation_dossier(context
):
78 'title': u
"implantation",
79 'choices': prepare_choices(
80 Implantation
.objects
.values_list('id', 'nom'),
81 'poste__implantation', context
86 @register.inclusion_tag('admin/filter.html', takes_context
=True)
87 def filter_implantation_contrat(context
):
89 'title': u
"implantation",
90 'choices': prepare_choices(
91 Implantation
.objects
.values_list('id', 'nom'),
92 'dossier__poste__implantation', context
97 @register.inclusion_tag('admin/filter.html', takes_context
=True)
98 def filter_type_contrat(context
):
100 'title': u
"type de contrat",
101 'choices': prepare_choices(
102 TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat',
108 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
109 def filter_echeance_contrat(context
):
110 today
= datetime
.date
.today()
111 three_months
= today
+ datetime
.timedelta(days
=3 * 30)
112 six_months
= today
+ datetime
.timedelta(days
=6 * 30)
113 twelve_months
= today
+ datetime
.timedelta(days
=12 * 30)
115 field_name
= 'date_fin'
116 return {'title': u
"échéance",
117 'choices': prepare_choices_date(field_name
, context
, links
=(
119 ('moins de 3 mois', {
120 '%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
121 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')
124 '%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
125 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')
128 '%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
129 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')
135 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
136 def filter_debut_contrat(context
):
137 year
= datetime
.date
.today().timetuple()[0]
138 this_year
= datetime
.date(year
, 1, 1)
139 next_year
= datetime
.date(year
+ 1, 1, 1)
140 last_year
= datetime
.date(year
- 1, 12, 31)
142 field_name
= 'date_debut'
143 return {'title': u
"date début",
144 'choices': prepare_choices_date(field_name
, context
, links
=(
147 '%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')
150 '%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
151 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')
154 '%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')
160 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
161 def filter_a_venir(context
):
162 today
= datetime
.date
.today()
163 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
164 mois_prochain
= datetime
.date(
165 year
+ (month
+ 1) / 13, (month
+ 1) % 12, 1
167 this_month
= datetime
.date(year
, month
, 1)
169 field_name
= 'date_debut'
170 return {'title': u
"à venir",
171 'choices': prepare_choices_date(field_name
, context
, links
=(
174 '%s__gt' % field_name
: today
.strftime('%Y-%m-%d')
176 ('à venir mois prochain', {
177 '%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
179 ('à venir ce mois', {
180 '%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
181 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
187 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
188 def filter_region_remun(context
):
191 'choices': prepare_choices(
192 Region
.objects
.values_list('id', 'nom'),
193 'dossiers__poste__implantation__region', context
,
194 remove
=['pays', 'nord_sud']
199 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
200 def filter_implantation_remun(context
):
202 'title': u
"implantation",
203 'choices': prepare_choices(
204 Implantation
.objects
.values_list('id', 'nom'),
205 'dossiers__poste__implantation', context
210 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
211 def table_header(context
, headers
):
212 return {'headers': headers
}
215 def get_query_string(request
, new_params
=None, remove
=None):
216 if new_params
is None:
220 p
= dict(request
.GET
.items())
225 for k
, v
in new_params
.items():
231 return '?%s' % urlencode(p
)
234 def prepare_choices(choices
, query_param
, context
, remove
=[]):
235 request
= context
['request']
236 query_val
= request
.GET
.get(query_param
)
238 'selected': query_val
is None,
239 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
244 'selected': smart_unicode(k
) == query_val
,
245 'query_string': get_query_string(
246 request
, {query_param
: k
}, remove
253 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
254 request
= context
['request']
256 field_generic
= '%s__' % field_name
258 (k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)
262 for title
, param_dict
in links
:
264 'selected': date_params
== param_dict
,
265 'query_string': get_query_string(
266 request
, param_dict
, [field_generic
]
274 def __init__(self
, request
, headers
, order_field_type
, order_field
,
275 default_order_type
="asc", not_sortable
=[]):
276 self
.request
= request
277 self
.order_field_type
= order_field_type
278 self
.order_field
= order_field
279 self
.header_defs
= headers
280 self
.params
= dict(request
.GET
)
281 if default_order_type
not in ('asc', 'desc'):
282 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
283 self
.default_order_type
= default_order_type
284 self
.current_order_field
= \
285 self
.params
[self
.order_field
][0] \
286 if self
.order_field
in self
.params
else None
287 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] \
288 if self
.order_field_type
in self
.params
else None
289 self
.not_sortable
= not_sortable
292 for h
in self
.header_defs
:
294 if h
[0] == self
.current_order_field
:
296 'asc': 'desc', 'desc': 'asc'
297 }[self
.current_order_field_type
]
298 class_order
= "%sending" % self
.current_order_field_type
300 order_type
= self
.default_order_type
302 params
[self
.order_field_type
] = [order_type
]
303 params
[self
.order_field
] = [h
[0]]
304 url
= "?%s" % "&".join([
305 '%s=%s' % (param
, value
[0]) for param
, value
in params
.items()
309 "sorted %s" % class_order
310 if self
.field_sorted(h
[0]) else ""
312 "sortable": self
.field_sorted(h
[0]),
315 "style_attr": h
[2] if len(h
) > 2 else "",
318 def field_sorted(self
, field
):
319 return True if field
not in self
.not_sortable
else False
323 def split(str, splitter
):
324 return str.split(splitter
)
332 raise Exception("%s does not exist" % key
)
337 if type(v
) == type(float()):
342 def contains(value
, arg
):