Oublis
[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 from django.conf import settings
9
10 from auf_references_modeles import Utilisateur, GroupesPersonne
11
12 import sys, traceback
13
14 def has_group(group = ""):
15 def _dec(view_func):
16 def _view(request, *args, **kwargs):
17 allowed = False
18 print getattr(settings, 'ROA_MODELS', False)
19 if request.user.is_authenticated():
20 try:
21 p = Utilisateur.objects.get(courriel = request.user.email)
22 #g = Groupe.objects.get(nom = group)
23 for g in GroupesPersonne.objects.filter(personne__pk = p.pk):
24 if g.groupe.nom == group:
25 allowed = True
26 break
27 except:
28 print "Exception in user code:"
29 print '-'*60
30 traceback.print_exc(file=sys.stdout)
31 print '-'*60
32 else:
33 allowed = True
34
35 if allowed:
36 r = view_func(request, *args, **kwargs)
37 else:
38 logout(request)
39 t = get_template('references_403.html')
40 html = t.render(Context({}))
41 r = HttpResponseForbidden(html)
42
43 return r
44
45 _view.__name__ = view_func.__name__
46 _view.__dict__ = view_func.__dict__
47 _view.__doc__ = view_func.__doc__
48
49 return _view
50 return _dec