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

Premier jet de décorateur can_edit

This commit is contained in:
Hugo LEVY-FALK 2017-11-30 00:28:33 +01:00
parent 541f630369
commit 3ef9035712

View file

@ -41,6 +41,7 @@ from django.utils import timezone
from django.db.models import Q from django.db.models import Q
from django.contrib import messages from django.contrib import messages
from django.shortcuts import redirect from django.shortcuts import redirect
from django.urls import reverse
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
@ -67,6 +68,34 @@ def can_create(model):
return decorator return decorator
def can_edit(model, *instance_id):
"""Decorator to check if an user can edit 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):
instances = {}
for i in instance_id:
try:
instances[i] = model.objects.get(pk=i)
except model.DoesNotExist:
messages.error(request, u"Entrée inexistante")
return redirect(reverse('users:index'))
kwargs['instances'] = instances
can = all(model.can_edit(request, instances[i]) for i in instances)
if not can:
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
qu'une seule requete sql qu'une seule requete sql