dd87a5de94ad373b9a5a5abe0e0e36555d43e27f
[auf_rh_dae.git] / project / dae / templatetags / dae.py
1 # -*- encoding: utf-8 -*-
2
3 import os
4
5 from django import template
6 # -*- coding: utf-8 -*-
7
8 from project import groups
9
10 from project.dae.workflow import ETATS_EDITABLE
11 from project.dae.forms import (
12 remun_formset_factory_factory,
13 ReadOnlyRemunFormSet,
14 PosteCompReadOnlyRemunFormSet,
15 DossierCompReadOnlyRemunFormSet,
16 )
17 from project.dae import models as dae
18 from project.rh import models as rh
19
20
21 register = template.Library()
22
23
24 @register.filter
25 def test_membre_drh(user):
26 grps = [g.name for g in user.groups.all()]
27 if groups.DRH_NIVEAU_1 in grps or groups.DRH_NIVEAU_2 in grps:
28 return True
29 else:
30 return False
31
32
33 @register.filter
34 def peut_ajouter(user):
35 grp_ok = (groups.ADMINISTRATEURS,
36 groups.CORRESPONDANT_RH,
37 groups.DRH_NIVEAU_1,
38 groups.DRH_NIVEAU_2)
39 for g in [g.name for g in user.groups.all()]:
40 if g in grp_ok:
41 return True
42 return False
43
44
45 @register.filter
46 def est_editable(obj, user):
47 klass = obj.__class__
48 groupes_users = [g.name for g in user.groups.all()]
49 if obj.etat in ETATS_EDITABLE and \
50 (obj in klass.objects.mes_choses_a_faire(user).all() or \
51 groups.DRH_NIVEAU_1 in groupes_users or \
52 groups.DRH_NIVEAU_2 in groupes_users):
53 return True
54 else:
55 return False
56
57
58 @register.filter
59 def basename(path):
60 return os.path.basename(path)
61
62
63 @register.inclusion_tag('dae/sort_header.html', takes_context=True)
64 def sort_header(context, field, title):
65 """
66 Génère une entête qu'on peut cliquer pour trier
67 la colonne correspondante dans une table.
68 """
69 qs = context['request'].GET.copy()
70 current = qs.get('tri', None)
71 if current == field:
72 cls = 'header headerSortUp'
73 qs['tri'] = '-' + field
74 elif current == '-' + field:
75 cls = 'header headerSortDown'
76 qs['tri'] = field
77 else:
78 cls = 'header'
79 qs['tri'] = field
80 return {'title': title, 'qs': qs.urlencode(), 'cls': cls}
81
82
83 @register.inclusion_tag('dae/pagination.html', takes_context=True)
84 def pagination(context, page):
85 """Génère la navigation permettant de se promener de page en page."""
86 qs = context['request'].GET
87 previous_qs = qs.copy()
88 previous_qs['page'] = page.previous_page_number()
89 next_qs = qs.copy()
90 next_qs['page'] = page.next_page_number()
91 return {'page': page,
92 'previous_qs': previous_qs.urlencode(),
93 'next_qs': next_qs.urlencode()
94 }
95
96
97 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
98 def remun_form(context, dossier):
99
100 return {
101 'remunForm': ReadOnlyRemunFormSet(instance=dossier)
102 }
103
104
105 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
106 def rh_remun_form(context, dossier):
107
108 return {
109 'remunForm': RHReadOnlyRemunFormSet(instance=dossier)
110 }
111
112
113 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
114 def poste_cmp_remun_form(context, poste_cmp):
115 return {
116 'remunForm': PosteCompReadOnlyRemunFormSet(instance=poste_cmp)
117 }
118
119
120 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
121 def dossier_cmp_remun_form(context, dossier_cmp):
122 return {
123 'remunForm': DossierCompReadOnlyRemunFormSet(instance=dossier_cmp)
124 }
125
126
127 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
128 def rh_remun_form_for_year(context, dossier, year):
129
130 fs = remun_formset_factory_factory(
131 read_only=True,
132 parent_model=rh.Dossier,
133 model=rh.Remuneration,
134 )
135
136 def _get_qs(inst):
137 return inst.model.objects.filter(
138 dossier=inst.instance,
139 date_debut__year=year,
140 )
141
142 fs.get_queryset = _get_qs
143
144 return {
145 'remunForm': fs(instance=dossier)
146 }
147
148