Oublis
[auf_references_client.git] / auf_references_client / decorators.py
CommitLineData
1c827f00
CR
1#!/usr/bin/env python
2# -*- encoding: utf-8 -*-
3
4from django.http import HttpResponseForbidden
5from django.contrib.auth import logout
6from django.template.loader import get_template
7from django.template import Context
287a41be
CR
8from django.conf import settings
9
10from auf_references_modeles import Utilisateur, GroupesPersonne
11
12import sys, traceback
1c827f00
CR
13
14def has_group(group = ""):
15 def _dec(view_func):
16 def _view(request, *args, **kwargs):
17 allowed = False
287a41be 18 print getattr(settings, 'ROA_MODELS', False)
1c827f00
CR
19 if request.user.is_authenticated():
20 try:
287a41be
CR
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
1c827f00
CR
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