d3ef466a0acdb1ca7e0d91d3b77e5bef3140a501
[auf_references_client.git] / auf_references_client / decorators.py
1 #!/usr/bin/env python
2 # -*- encoding: utf-8 -*-
3
4 from django.http import HttpResponseForbidden
5 from django.contrib.auth import logout
6 from django.template.loader import get_template
7 from django.template import Context
8
9 def has_group(group = ""):
10 def _dec(view_func):
11 def _view(request, *args, **kwargs):
12 allowed = False
13 if request.user.is_authenticated():
14 try:
15 p = personne(request.user)
16 g = Groupe.objects.get(nom = group)
17 if g in p.groupes:
18 allowed = True
19 except: pass
20 else:
21 allowed = True
22
23 if allowed:
24 r = view_func(request, *args, **kwargs)
25 else:
26 logout(request)
27 t = get_template('references_403.html')
28 html = t.render(Context({}))
29 r = HttpResponseForbidden(html)
30
31 return r
32
33 _view.__name__ = view_func.__name__
34 _view.__dict__ = view_func.__dict__
35 _view.__doc__ = view_func.__doc__
36
37 return _view
38 return _dec