From 541f6303698db1c5c72ceda5e45e0e2891e967ff Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Wed, 29 Nov 2017 11:00:05 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9placement=20du=20d=C3=A9corateur=20can?= =?UTF-8?q?=5Fcreate=20dans=20re2o/utils.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2o/utils.py | 19 +++++++++++++++++++ users/views.py | 19 +------------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/re2o/utils.py b/re2o/utils.py index a6e5c851..71f87462 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -39,6 +39,8 @@ from __future__ import unicode_literals from django.utils import timezone from django.db.models import Q +from django.contrib import messages +from django.shortcuts import redirect from cotisations.models import Cotisation, Facture, Paiement, Vente from machines.models import Domain, Interface, Machine @@ -47,6 +49,23 @@ from preferences.models import Service DT_NOW = timezone.now() +def can_create(model): + """Decorator to check if an user can create a model. + It assumes that a valid user exists in the request and that the model has a + method can_create(user) which returns true if the user can create this kind + of models. + """ + def decorator(view): + def wrapper(request,*args, **kwargs): + if not model.can_create(request.user): + messages.error(request, "Vous ne pouvez pas accéder à ce menu") + return redirect(reverse('users:profil', + kwargs={'userid':str(request.user.id)} + )) + return view(request, *args, **kwargs) + return wrapper + return decorator + def all_adherent(search_time=DT_NOW): """ Fonction renvoyant tous les users adherents. Optimisee pour n'est diff --git a/users/views.py b/users/views.py index 1ccdd980..def886d1 100644 --- a/users/views.py +++ b/users/views.py @@ -92,7 +92,7 @@ from machines.models import Machine from preferences.models import OptionalUser, GeneralOption from re2o.views import form -from re2o.utils import all_has_access, SortTable +from re2o.utils import all_has_access, SortTable, can_create def password_change_action(u_form, user, request, req=False): """ Fonction qui effectue le changeemnt de mdp bdd""" @@ -109,23 +109,6 @@ def password_change_action(u_form, user, request, req=False): kwargs={'userid':str(user.id)} )) -def can_create(model): - """Decorator to check if an user can create a model. - It assumes that a valid user exists in the request and that the model has a - method can_create(user) which returns true if the user can create this kind - of models. - """ - def decorator(view): - def wrapper(request,*args, **kwargs): - if not model.can_create(request.user): - messages.error(request, "Vous ne pouvez pas accéder à ce menu") - return redirect(reverse('users:profil', - kwargs={'userid':str(request.user.id)} - )) - return view(request, *args, **kwargs) - return wrapper - return decorator - @login_required @can_create(Adherent) def new_user(request):