[#2372] Réorganisation champs Poste
[auf_rh_dae.git] / project / decorators.py
CommitLineData
e993f3dc 1# -*- encoding: utf-8 -*-
2"""
3Décorateurs AUF
4"""
3411ac33 5#from django.contrib.auth.decorators import user_passes_test
6#from django.core.exceptions import PermissionDenied
7#from django.core.urlresolvers import reverse
e993f3dc 8from django.http import HttpResponseRedirect
3411ac33 9from django.shortcuts import redirect
e993f3dc 10from permissions import is_employe, is_admin
11
3411ac33 12#from datamaster_modeles import models as ref
13from dae.utils import get_employe_from_user
14from rh import models as rh
15
e993f3dc 16# Décorateurs des fonctions dans views (pour tester permissions dans views)
17
18def admin_required(fn):
19 def inner(request, *args, **kwargs):
20 user = request.user
21 if is_admin(user):
22 return fn(request, *args, **kwargs)
23 else :
f8daa167
OL
24 from django.conf import settings
25 from django.contrib.auth import REDIRECT_FIELD_NAME
26 from django.utils.http import urlquote
27 login_url = settings.LOGIN_URL
28 path = urlquote(request.get_full_path())
29 tup = login_url, REDIRECT_FIELD_NAME, path
30 return HttpResponseRedirect('%s?%s=%s' % tup)
e993f3dc 31 return inner
3411ac33 32
33def user_is_this_employe(fn):
34 """
35 Décorateur qui vérifie si le user est l'employé demandé.
36 Succes = execution de la vue.
37 Echec = page d'erreur de permission
38 """
39 def inner(request, id, *args, **kwargs):
40 id = int(id)
41 user = request.user
42 employe = get_employe_from_user(user)
43 if user.is_authenticated() and employe and id==employe.id:
44 return fn(request, id, *args, **kwargs)
45# raise PermissionDenied
46 return redirect('550')
47 return inner