feature complete.. now need a bit of testing
[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 ReadOnlyRemunFormSet,
13 PosteCompReadOnlyRemunFormSet,
14 DossierCompReadOnlyRemunFormSet,
15 )
16 from project.dae import models as dae
17 from project.rh import models as rh
18
19
20 register = template.Library()
21
22
23 @register.filter
24 def test_membre_drh(user):
25 grps = [g.name for g in user.groups.all()]
26 if groups.DRH_NIVEAU_1 in grps or groups.DRH_NIVEAU_2 in grps:
27 return True
28 else:
29 return False
30
31
32 @register.filter
33 def peut_ajouter(user):
34 grp_ok = (groups.ADMINISTRATEURS,
35 groups.CORRESPONDANT_RH,
36 groups.DRH_NIVEAU_1,
37 groups.DRH_NIVEAU_2)
38 for g in [g.name for g in user.groups.all()]:
39 if g in grp_ok:
40 return True
41 return False
42
43
44 @register.filter
45 def est_editable(obj, user):
46 klass = obj.__class__
47 groupes_users = [g.name for g in user.groups.all()]
48 if obj.etat in ETATS_EDITABLE and \
49 (obj in klass.objects.mes_choses_a_faire(user).all() or \
50 groups.DRH_NIVEAU_1 in groupes_users or \
51 groups.DRH_NIVEAU_2 in groupes_users):
52 return True
53 else:
54 return False
55
56
57 @register.filter
58 def basename(path):
59 return os.path.basename(path)
60
61
62 @register.inclusion_tag('dae/sort_header.html', takes_context=True)
63 def sort_header(context, field, title):
64 """
65 Génère une entête qu'on peut cliquer pour trier
66 la colonne correspondante dans une table.
67 """
68 qs = context['request'].GET.copy()
69 current = qs.get('tri', None)
70 if current == field:
71 cls = 'header headerSortUp'
72 qs['tri'] = '-' + field
73 elif current == '-' + field:
74 cls = 'header headerSortDown'
75 qs['tri'] = field
76 else:
77 cls = 'header'
78 qs['tri'] = field
79 return {'title': title, 'qs': qs.urlencode(), 'cls': cls}
80
81
82 @register.inclusion_tag('dae/pagination.html', takes_context=True)
83 def pagination(context, page):
84 """Génère la navigation permettant de se promener de page en page."""
85 qs = context['request'].GET
86 previous_qs = qs.copy()
87 previous_qs['page'] = page.previous_page_number()
88 next_qs = qs.copy()
89 next_qs['page'] = page.next_page_number()
90 return {'page': page,
91 'previous_qs': previous_qs.urlencode(),
92 'next_qs': next_qs.urlencode()
93 }
94
95
96 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
97 def remun_form(context, dossier):
98
99 return {
100 'remunForm': ReadOnlyRemunFormSet(instance=dossier)
101 }
102
103
104 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
105 def poste_cmp_remun_form(context, poste_cmp):
106 return {
107 'remunForm': PosteCompReadOnlyRemunFormSet(instance=poste_cmp)
108 }
109
110
111 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
112 def dossier_cmp_remun_form(context, dossier_cmp):
113 return {
114 'remunForm': DossierCompReadOnlyRemunFormSet(instance=dossier_cmp)
115 }