fe3027e567bbbf57af5e3d80fdc12c26d397b3bc
[auf_rh_dae.git] / project / dae / templatetags / dae.py
1 # -*- encoding: utf-8 -*-
2
3 import os
4
5 from django import template
6
7 from project.groups import grp_correspondants_rh, grp_administrateurs, \
8 grp_drh, grp_drh2
9
10 from project.dae.workflow import ETATS_EDITABLE
11
12
13 register = template.Library()
14
15 @register.filter
16 def test_membre_drh(user):
17 grps = user.groups.all()
18 if grp_drh in grps or grp_drh2 in grps:
19 return True
20 else:
21 return False
22
23 @register.filter
24 def peut_ajouter(user):
25 for g in user.groups.all():
26 if g in (grp_administrateurs, grp_correspondants_rh, grp_drh):
27 return True
28 return False
29
30 @register.filter
31 def peut_importer(user):
32 return grp_drh in user.groups.all()
33
34 @register.filter
35 def est_editable(obj, user):
36 klass = obj.__class__
37 groupes_users = user.groups.all()
38 if obj.etat in ETATS_EDITABLE and \
39 (obj in klass.objects.mes_choses_a_faire(user).all() or grp_drh in groupes_users):
40 return True
41 else:
42 return False
43
44 @register.filter
45 def region_ou_service(implantation):
46 if implantation.id == 15:
47 return u"Services centraux de Montréal (SCM)"
48 if implantation.id == 19:
49 return u"Services centraux de Paris (SCP)"
50 return implantation.region
51
52 @register.filter
53 def basename(path):
54 return os.path.basename(path)
55
56 @register.inclusion_tag('dae/sort_header.html', takes_context=True)
57 def sort_header(context, field, title):
58 """Génère une entête qu'on peut cliquer pour trier la colonne correspondante dans une table."""
59 qs = context['request'].GET.copy()
60 current = qs.get('tri', None)
61 if current == field:
62 cls = 'header headerSortUp'
63 qs['tri'] = '-' + field
64 elif current == '-' + field:
65 cls = 'header headerSortDown'
66 qs['tri'] = field
67 else:
68 cls = 'header'
69 qs['tri'] = field
70 return {'title': title, 'qs': qs.urlencode(), 'cls': cls}
71
72 @register.inclusion_tag('dae/pagination.html', takes_context=True)
73 def pagination(context, page):
74 """Génère la navigation permettant de se promener de page en page."""
75 qs = context['request'].GET
76 previous_qs = qs.copy()
77 previous_qs['page'] = page.previous_page_number()
78 next_qs = qs.copy()
79 next_qs['page'] = page.next_page_number()
80 return {'page': page, 'previous_qs': previous_qs.urlencode(), 'next_qs': next_qs.urlencode()}