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
import groups
13 from project
.rh
.models
import TypeContrat
18 COMBLE_CHOICES
= (('c', 'Comblé'), ('n', 'Vacant'))
21 @register.inclusion_tag('admin/filter.html', takes_context
=True)
22 def filter_comble(context
):
25 'choices': prepare_choices(COMBLE_CHOICES
, 'comble', context
)
29 @register.inclusion_tag('admin/filter.html', takes_context
=True)
30 def filter_region(context
, prefix
=None):
31 label
= "".join([prefix
or "", "implantation__region"])
34 'choices': prepare_choices(
35 Region
.objects
.values_list('id', 'nom'), label
, context
,
36 remove
=['pays', 'nord_sud']
41 @register.inclusion_tag('admin/filter.html', takes_context
=True)
42 def filter_implantation(context
, prefix
=None):
43 label
= "".join([prefix
or "", "implantation"])
45 'title': u
"implantation",
46 'choices': prepare_choices(
47 Implantation
.objects
.values_list('id', 'nom'), label
, context
52 @register.inclusion_tag('admin/filter.html', takes_context
=True)
53 def filter_region_contrat(context
):
54 request
= context
['request']
55 user_groups
= [g
.name
for g
in request
.user
.groups
.all()]
56 if groups
.CORRESPONDANT_RH
in user_groups
or\
57 groups
.ADMINISTRATEURS
in user_groups
or\
58 groups
.DIRECTEUR_DE_BUREAU
in user_groups
:
59 employe
= groups
.get_employe_from_user(request
.user
)
60 regions
= Region
.objects
.filter(id=employe
.implantation
.region
.id)
62 regions
= Region
.objects
.all()
63 return {'title': u
"région",
64 'choices': prepare_choices(regions
.values_list('id', 'nom'), 'dossier__poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
67 @register.inclusion_tag('admin/filter.html', takes_context
=True)
68 def filter_region_dossier(context
):
71 'choices': prepare_choices(
72 Region
.objects
.values_list('id', 'nom'),
73 'poste__implantation__region', context
,
74 remove
=['pays', 'nord_sud']
79 @register.inclusion_tag('admin/filter.html', takes_context
=True)
80 def filter_implantation_dossier(context
):
82 'title': u
"implantation",
83 'choices': prepare_choices(
84 Implantation
.objects
.values_list('id', 'nom'),
85 'poste__implantation', context
90 @register.inclusion_tag('admin/filter.html', takes_context
=True)
91 def filter_implantation_contrat(context
):
92 request
= context
['request']
93 user_groups
= [g
.name
for g
in request
.user
.groups
.all()]
94 if groups
.CORRESPONDANT_RH
in user_groups
or\
95 groups
.ADMINISTRATEURS
in user_groups
or\
96 groups
.DIRECTEUR_DE_BUREAU
in user_groups
:
97 employe
= groups
.get_employe_from_user(request
.user
)
98 implantations
= Implantation
.objects
.filter(region
=employe
.implantation
.region
)
100 implantations
= Implantation
.objects
.all()
101 return {'title': u
"implantation",
102 'choices': prepare_choices(implantations
.values_list('id', 'nom'), 'dossier__poste__implantation', context
)}
105 @register.inclusion_tag('admin/filter.html', takes_context
=True)
106 def filter_type_contrat(context
):
108 'title': u
"type de contrat",
109 'choices': prepare_choices(
110 TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat',
116 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
117 def filter_echeance_contrat(context
):
118 today
= datetime
.date
.today()
119 three_months
= today
+ datetime
.timedelta(days
=3 * 30)
120 six_months
= today
+ datetime
.timedelta(days
=6 * 30)
121 twelve_months
= today
+ datetime
.timedelta(days
=12 * 30)
123 field_name
= 'date_fin'
124 return {'title': u
"échéance",
125 'choices': prepare_choices_date(field_name
, context
, links
=(
127 ('moins de 3 mois', {
128 '%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
129 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')
132 '%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
133 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')
136 '%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
137 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')
143 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
144 def filter_debut_contrat(context
):
145 year
= datetime
.date
.today().timetuple()[0]
146 this_year
= datetime
.date(year
, 1, 1)
147 next_year
= datetime
.date(year
+ 1, 1, 1)
148 last_year
= datetime
.date(year
- 1, 12, 31)
150 field_name
= 'date_debut'
151 return {'title': u
"date début",
152 'choices': prepare_choices_date(field_name
, context
, links
=(
155 '%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')
158 '%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
159 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')
162 '%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')
168 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
169 def filter_a_venir(context
):
170 today
= datetime
.date
.today()
171 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
172 mois_prochain
= datetime
.date(
173 year
+ (month
+ 1) / 13, (month
+ 1) % 12, 1
175 this_month
= datetime
.date(year
, month
, 1)
177 field_name
= 'date_debut'
178 return {'title': u
"à venir",
179 'choices': prepare_choices_date(field_name
, context
, links
=(
182 '%s__gt' % field_name
: today
.strftime('%Y-%m-%d')
184 ('à venir mois prochain', {
185 '%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
187 ('à venir ce mois', {
188 '%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
189 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')
195 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
196 def filter_region_remun(context
):
199 'choices': prepare_choices(
200 Region
.objects
.values_list('id', 'nom'),
201 'dossiers__poste__implantation__region', context
,
202 remove
=['pays', 'nord_sud']
207 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
208 def filter_implantation_remun(context
):
210 'title': u
"implantation",
211 'choices': prepare_choices(
212 Implantation
.objects
.values_list('id', 'nom'),
213 'dossiers__poste__implantation', context
218 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
219 def table_header(context
, headers
):
220 return {'headers': headers
}
223 def get_query_string(request
, new_params
=None, remove
=None):
224 if new_params
is None:
228 p
= dict(request
.GET
.items())
233 for k
, v
in new_params
.items():
239 return '?%s' % urlencode(p
)
242 def prepare_choices(choices
, query_param
, context
, remove
=[]):
243 request
= context
['request']
244 query_val
= request
.GET
.get(query_param
)
246 'selected': query_val
is None,
247 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
252 'selected': smart_unicode(k
) == query_val
,
253 'query_string': get_query_string(
254 request
, {query_param
: k
}, remove
261 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
262 request
= context
['request']
264 field_generic
= '%s__' % field_name
266 (k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)
270 for title
, param_dict
in links
:
272 'selected': date_params
== param_dict
,
273 'query_string': get_query_string(
274 request
, param_dict
, [field_generic
]
282 def __init__(self
, request
, headers
, order_field_type
, order_field
,
283 default_order_type
="asc", not_sortable
=[]):
284 self
.request
= request
285 self
.order_field_type
= order_field_type
286 self
.order_field
= order_field
287 self
.header_defs
= headers
288 self
.params
= dict(request
.GET
)
289 if default_order_type
not in ('asc', 'desc'):
290 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
291 self
.default_order_type
= default_order_type
292 self
.current_order_field
= \
293 self
.params
[self
.order_field
][0] \
294 if self
.order_field
in self
.params
else None
295 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] \
296 if self
.order_field_type
in self
.params
else None
297 self
.not_sortable
= not_sortable
300 for h
in self
.header_defs
:
302 if h
[0] == self
.current_order_field
:
304 'asc': 'desc', 'desc': 'asc'
305 }[self
.current_order_field_type
]
306 class_order
= "%sending" % self
.current_order_field_type
308 order_type
= self
.default_order_type
310 params
[self
.order_field_type
] = [order_type
]
311 params
[self
.order_field
] = [h
[0]]
312 url
= "?%s" % "&".join([
313 '%s=%s' % (param
, value
[0]) for param
, value
in params
.items()
317 "sorted %s" % class_order
318 if self
.field_sorted(h
[0]) else ""
320 "sortable": self
.field_sorted(h
[0]),
323 "style_attr": h
[2] if len(h
) > 2 else "",
326 def field_sorted(self
, field
):
327 return True if field
not in self
.not_sortable
else False
331 def split(str, splitter
):
332 return str.split(splitter
)
340 raise Exception("%s does not exist" % key
)
345 if type(v
) == type(float()):
350 def contains(value
, arg
):