From 58ff2a2c8d8bd3dc50e1073bcf486ccbc042cbbb Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 27 Dec 2017 21:09:40 +0100 Subject: [PATCH] Nouvelle fonction pour check par field --- re2o/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/re2o/utils.py b/re2o/utils.py index 47a94ee7..1271fea4 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -96,6 +96,27 @@ def can_edit(model): return decorator +def can_change(model, field_list): + """Decorator to check if an user can edit a field of 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): + for field in field_list: + can_create = getattr(model, 'can_change_' + field) + can, msg = can_create(request.user, *args, **kwargs) + if not can: + messages.error(request, msg or "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 can_delete(model): """Decorator to check if an user can delete a model. It tries to get an instance of the model, using