Ajout du decorateur has_group
[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
8
9def 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