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_select.html', takes_context
=True)
26 def filter_region(context
):
27 return {'title': u
"région",
28 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'implantation__region', context
, remove
=['pays', 'nord_sud'])}
31 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
32 def filter_implantation(context
):
33 return {'title': u
"implantation",
34 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'implantation', context
)}
37 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
38 def filter_region_contrat(context
):
39 return {'title': u
"région",
40 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'dossier__poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
43 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
44 def filter_implantation_contrat(context
):
45 return {'title': u
"implantation",
46 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'dossier__poste__implantation', context
)}
49 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
50 def filter_type_contrat(context
):
51 return {'title': u
"type de contrat",
52 'choices': prepare_choices(TypeContrat
.objects
.values_list('id', 'nom'), 'type_contrat', context
)}
54 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
55 def filter_echeance_contrat(context
):
56 today
= datetime
.date
.today()
57 three_months
= today
+ datetime
.timedelta(days
=3*30)
58 six_months
= today
+ datetime
.timedelta(days
=6*30)
59 twelve_months
= today
+ datetime
.timedelta(days
=12*30)
61 field_name
= 'date_fin'
62 return {'title': u
"échéance",
63 'choices': prepare_choices_date(field_name
, context
, links
=(
65 ('moins de 3 mois', {'%s__gte' % field_name
: today
.strftime('%Y-%m-%d'),
66 '%s__lte' % field_name
: three_months
.strftime('%Y-%m-%d')}),
67 ('3 à 6 mois', {'%s__gte' % field_name
: three_months
.strftime('%Y-%m-%d'),
68 '%s__lte' % field_name
: six_months
.strftime('%Y-%m-%d')}),
69 ('6 à 12 mois', {'%s__gte' % field_name
: six_months
.strftime('%Y-%m-%d'),
70 '%s__lte' % field_name
: twelve_months
.strftime('%Y-%m-%d')}),
74 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
75 def filter_debut_contrat(context
):
76 year
= datetime
.date
.today().timetuple()[0]
77 this_year
= datetime
.date(year
, 1, 1)
78 next_year
= datetime
.date(year
+ 1, 1, 1)
79 last_year
= datetime
.date(year
- 1, 12,31)
81 field_name
= 'date_debut'
82 return {'title': u
"date début",
83 'choices': prepare_choices_date(field_name
, context
, links
=(
85 ('anées à venirs', {'%s__gte' % field_name
: next_year
.strftime('%Y-%m-%d')}),
86 ('cette anneée', {'%s__gte' % field_name
: this_year
.strftime('%Y-%m-%d'),
87 '%s__lt' % field_name
: next_year
.strftime('%Y-%m-%d')}),
88 ('ans passés', {'%s__lte' % field_name
: last_year
.strftime('%Y-%m-%d')}),
92 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
93 def filter_a_venir(context
):
94 today
= datetime
.date
.today()
95 year
, month
, day
= datetime
.date
.today().timetuple()[:3]
96 mois_prochain
= datetime
.date(year
+((month
+1)/13), (month
+1)%12, 1)
97 this_month
= datetime
.date(year
, month
, 1)
99 field_name
= 'date_debut'
100 return {'title': u
"à venir",
101 'choices': prepare_choices_date(field_name
, context
, links
=(
103 ('à venir', {'%s__gt' % field_name
: today
.strftime('%Y-%m-%d')}),
104 ('à venir mois prochain', {'%s__gte' % field_name
: mois_prochain
.strftime('%Y-%m-%d')}),
105 ('à venir ce mois', {'%s__gte' % field_name
: this_month
.strftime('%Y-%m-%d'),
106 '%s__lt' % field_name
: mois_prochain
.strftime('%Y-%m-%d')}),
113 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
114 def filter_region_remun(context
):
115 return {'title': u
"région",
116 'choices': prepare_choices(Region
.objects
.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context
, remove
=['pays', 'nord_sud'])}
119 @register.inclusion_tag('admin/filter_select.html', takes_context
=True)
120 def filter_implantation_remun(context
):
121 return {'title': u
"implantation",
122 'choices': prepare_choices(Implantation
.objects
.values_list('id', 'nom'), 'dossiers__poste__implantation', context
)}
125 def get_query_string(request
, new_params
=None, remove
=None):
126 if new_params
is None: new_params
= {}
127 if remove
is None: remove
= []
128 p
= dict(request
.GET
.items())
133 for k
, v
in new_params
.items():
139 return '?%s' % urlencode(p
)
142 def prepare_choices(choices
, query_param
, context
, remove
=[]):
143 request
= context
['request']
144 query_val
= request
.GET
.get(query_param
)
145 result
= [{'selected': query_val
is None,
146 'query_string': get_query_string(request
, {}, [query_param
] + remove
),
149 result
.append({'selected': smart_unicode(k
) == query_val
,
150 'query_string': get_query_string(request
, {query_param
: k
}, remove
),
155 def prepare_choices_date(field_name
, context
, links
, remove
=[]):
156 request
= context
['request']
158 field_generic
= '%s__' % field_name
159 date_params
= dict([(k
, v
) for k
, v
in params
.items() if k
.startswith(field_generic
)])
164 for title
, param_dict
in links
:
165 result
.append({'selected': date_params
== param_dict
,
166 'query_string': get_query_string(request
, param_dict
, [field_generic
]),