8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

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

This commit is contained in:
LEVY-FALK Hugo 2017-11-29 11:00:05 +01:00 committed by root
parent 90d45b73c4
commit aceb6a9e4a
2 changed files with 20 additions and 18 deletions

View file

@ -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

View file

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