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