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