Part 1 du bug fix, instable
[auf_rh_dae.git] / project / dae / templatetags / dae.py
CommitLineData
0b2edb6e
OL
1# -*- encoding: utf-8 -*-
2
bbf31587
EMS
3import os
4
5579d48d 5from django import template
be46739d 6# -*- coding: utf-8 -*-
75f0e87b 7
be46739d 8from project import groups
d41d8e47 9
286d0aa9 10from project.dae.workflow import ETATS_EDITABLE
b9098c33 11from project.dae.forms import (
6219cf26 12 remun_formset_factory_factory,
b9098c33
BS
13 ReadOnlyRemunFormSet,
14 PosteCompReadOnlyRemunFormSet,
15 DossierCompReadOnlyRemunFormSet,
16 )
17from project.dae import models as dae
18from project.rh import models as rh
d41d8e47 19
5579d48d
OL
20
21register = template.Library()
22
be46739d 23
5579d48d 24@register.filter
3a62d4fe 25def test_membre_drh(user):
3383b2d1
OL
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:
2c8d834d
OL
28 return True
29 else:
30 return False
3a62d4fe 31
be46739d 32
3a62d4fe 33@register.filter
5579d48d 34def peut_ajouter(user):
be46739d
OL
35 grp_ok = (groups.ADMINISTRATEURS,
36 groups.CORRESPONDANT_RH,
37 groups.DRH_NIVEAU_1,
38 groups.DRH_NIVEAU_2)
3383b2d1 39 for g in [g.name for g in user.groups.all()]:
be46739d 40 if g in grp_ok:
5579d48d
OL
41 return True
42 return False
43
67c10912
OL
44
45@register.filter
e7ee680f
OL
46def est_editable(obj, user):
47 klass = obj.__class__
3383b2d1 48 groupes_users = [g.name for g in user.groups.all()]
c511cd1f 49 if obj.etat in ETATS_EDITABLE and \
3383b2d1
OL
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):
286d0aa9
OL
53 return True
54 else:
55 return False
56
be46739d 57
0b2edb6e 58@register.filter
bbf31587
EMS
59def basename(path):
60 return os.path.basename(path)
428e3c0b 61
be46739d 62
428e3c0b
EMS
63@register.inclusion_tag('dae/sort_header.html', takes_context=True)
64def sort_header(context, field, title):
be46739d
OL
65 """
66 Génère une entête qu'on peut cliquer pour trier
67 la colonne correspondante dans une table.
68 """
428e3c0b
EMS
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
be46739d 82
428e3c0b
EMS
83@register.inclusion_tag('dae/pagination.html', takes_context=True)
84def 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()
be46739d
OL
91 return {'page': page,
92 'previous_qs': previous_qs.urlencode(),
93 'next_qs': next_qs.urlencode()
94 }
b9098c33
BS
95
96
97@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
98def 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)
6219cf26
BS
106def 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)
b9098c33
BS
114def 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)
121def dossier_cmp_remun_form(context, dossier_cmp):
122 return {
123 'remunForm': DossierCompReadOnlyRemunFormSet(instance=dossier_cmp)
124 }
6219cf26
BS
125
126
127@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
128def 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