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