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