72f3ccc3a3f9dbe77d85866f2182cb7f59da2582
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 datamaster_modeles
.models
import Implantation
, Region
10 from 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
):
21 return {'title': 'comblé',
22 'choices': prepare_choices(COMBLE_CHOICES
, 'comble', context
)}
25 @register.inclusion_tag('admin/filter.html', takes_context
=True)
26 def filter_region(context
, prefix
=None):
28 label
= "".join([prefix
or "", "implantation__region"])
29 return {'title': u
"région",
30 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), label
, context
, remove
=['pays', 'nord_sud'])}
33 @register.inclusion_tag('admin/filter.html', takes_context
=True)
34 def filter_implantation(context
, prefix
=None):
36 label
= "".join([prefix
or "", "implantation"])
37 return {'title': u
"implantation",
38 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), label
, context
)}
41 @register.inclusion_tag('admin/filter.html', takes_context
=True)
42 def filter_region_contrat(context
):
43 return {'title': u
"région",
44 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'dossier__poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
46 @register.inclusion_tag('admin/filter.html', takes_context
=True)
47 def filter_region_dossier(context
):
48 return {'title': u
"région",
49 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
51 @register.inclusion_tag('admin/filter.html', takes_context
=True)
52 def filter_implantation_dossier(context
):
53 return {'title': u
"implantation",
54 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'poste__implantation', context
)}
57 @register.inclusion_tag('admin/filter.html', takes_context
=True)
58 def filter_implantation_contrat(context
):
59 return {'title': u
"implantation",
60 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'dossier__poste__implantation', context
)}
63 @register.inclusion_tag('admin/filter.html', takes_context
=True)
64 def filter_type_contrat(context
):
65 return {'title': u
"type de contrat",
66 'choices': prepare_choices(TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat', context
)}
68 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
69 def filter_echeance_contrat(context
):
70 today
= datetime
.date
.today()
71 three_months
= today
+ datetime
.timedelta(days
=3*30)
72 six_months
= today
+ datetime
.timedelta(days
=6*30)
73 twelve_months
= today
+ datetime
.timedelta(days
=12*30)
75 field_name
= 'date_fin'
76 return {'title': u
"échéance",
77 'choices': prepare_choices_date(field_name
, context
, links
=(
79 ('échus', {'%s__lt' % field_name
: today
.strftime('%Y-%m-%d'),}),
80 ('moins de 3 mois', {'%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
81 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')}),
82 ('3 à 6 mois', {'%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
83 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')}),
84 ('6 à 12 mois', {'%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
85 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')}),
89 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
90 def filter_debut_contrat(context
):
91 year
= datetime
.date
.today().timetuple()[0]
92 this_year
= datetime
.date(year
, 1, 1)
93 next_year
= datetime
.date(year
+ 1, 1, 1)
94 last_year
= datetime
.date(year
- 1, 12,31)
96 field_name
= 'date_debut'
97 return {'title': u
"date début",
98 'choices': prepare_choices_date(field_name
, context
, links
=(
100 ('années à venir', {'%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')}),
101 ('cette anneée', {'%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
102 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')}),
103 ('années passées', {'%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')}),
107 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
108 def filter_a_venir(context
):
109 today
= datetime
.date
.today()
110 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
111 mois_prochain
= datetime
.date(year
+((month
+1)/13), (month
+1)%12, 1)
112 this_month
= datetime
.date(year
, month
, 1)
114 field_name
= 'date_debut'
115 return {'title': u
"à venir",
116 'choices': prepare_choices_date(field_name
, context
, links
=(
118 ('à venir', {'%s__gt' % field_name
: today
.strftime('%Y-%m-%d')}),
119 ('à venir mois prochain', {'%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')}),
120 ('à venir ce mois', {'%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
121 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')}),
128 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
129 def filter_region_remun(context
):
130 return {'title': u
"région",
131 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
134 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
135 def filter_implantation_remun(context
):
136 return {'title': u
"implantation",
137 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'dossiers__poste__implantation', context
)}
139 @register.inclusion_tag('rh/rapports/table_header.html', takes_context
=True)
140 def table_header(context
, headers
):
141 return {'headers': headers
}
143 def get_query_string(request
, new_params
=None, remove
=None):
144 if new_params
is None: new_params
= {}
145 if remove
is None: remove
= []
146 p
= dict(request
.GET
.items())
151 for k
, v
in new_params
.items():
157 return '?%s' % urlencode(p
)
160 def prepare_choices(choices
, query_param
, context
, remove
=[]):
161 request
= context
['request']
162 query_val
= request
.GET
.get(query_param
)
163 result
= [{'selected': query_val
is None,
164 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
167 result
.append({'selected': smart_unicode(k
) == query_val
,
168 'query_string': get_query_string(request
, {query_param
: k
}, remove
),
173 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
174 request
= context
['request']
176 field_generic
= '%s__' % field_name
177 date_params
= dict([(k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)])
182 for title
, param_dict
in links
:
183 result
.append({'selected': date_params
== param_dict
,
184 'query_string': get_query_string(request
, param_dict
, [field_generic
]),
189 def __init__(self
, request
, headers
, order_field_type
, order_field
, default_order_type
="asc", not_sortable
=[]):
190 self
.request
= request
191 self
.order_field_type
= order_field_type
192 self
.order_field
= order_field
193 self
.header_defs
= headers
194 self
.params
= dict(request
.GET
)
195 if default_order_type
not in ('asc', 'desc'):
196 raise AttributeError(u
"L'ordre par défaut n'est pas valide")
197 self
.default_order_type
= default_order_type
198 self
.current_order_field
= self
.params
[self
.order_field
][0] if self
.order_field
in self
.params
else None
199 self
.current_order_field_type
= self
.params
[self
.order_field_type
][0] if self
.order_field_type
in self
.params
else None
200 self
.not_sortable
= not_sortable
203 for h
in self
.header_defs
:
205 if h
[0] == self
.current_order_field
:
206 order_type
= {'asc': 'desc', 'desc': 'asc'}[self
.current_order_field_type
]
207 class_order
= "%sending" % self
.current_order_field_type
209 order_type
= self
.default_order_type
211 params
[self
.order_field_type
] = [order_type
]
212 params
[self
.order_field
] = [h
[0]]
213 url
= "?%s" % "&".join(['%s=%s' % (param
, value
[0]) for param
, value
in params
.items()])
215 "class_attr": "sorted %s" % class_order
if self
.field_sorted(h
[0]) else "",
216 "sortable": self
.field_sorted(h
[0]),
221 def field_sorted(self
, field
):
222 return True if field
not in self
.not_sortable
else False
225 def split(str, splitter
):
226 return str.split(splitter
)
233 raise Exception("%s does not exist" % key
)
237 if type(v
) == type(float()):