Commit aceb6a9e authored by LEVY-FALK Hugo's avatar LEVY-FALK Hugo Committed by root

Déplacement du décorateur can_create dans re2o/utils.py

parent 90d45b73
...@@ -39,6 +39,8 @@ from __future__ import unicode_literals ...@@ -39,6 +39,8 @@ from __future__ import unicode_literals
from django.utils import timezone from django.utils import timezone
from django.db.models import Q 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 cotisations.models import Cotisation, Facture, Paiement, Vente
from machines.models import Domain, Interface, Machine from machines.models import Domain, Interface, Machine
...@@ -47,6 +49,23 @@ from preferences.models import Service ...@@ -47,6 +49,23 @@ from preferences.models import Service
DT_NOW = timezone.now() 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): def all_adherent(search_time=DT_NOW):
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est """ Fonction renvoyant tous les users adherents. Optimisee pour n'est
......
...@@ -92,7 +92,7 @@ from machines.models import Machine ...@@ -92,7 +92,7 @@ from machines.models import Machine
from preferences.models import OptionalUser, GeneralOption from preferences.models import OptionalUser, GeneralOption
from re2o.views import form 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): def password_change_action(u_form, user, request, req=False):
""" Fonction qui effectue le changeemnt de mdp bdd""" """ Fonction qui effectue le changeemnt de mdp bdd"""
...@@ -109,23 +109,6 @@ def password_change_action(u_form, user, request, req=False): ...@@ -109,23 +109,6 @@ def password_change_action(u_form, user, request, req=False):
kwargs={'userid':str(user.id)} 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 @login_required
@can_create(Adherent) @can_create(Adherent)
def new_user(request): def new_user(request):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment