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):