From 876021ae12135791d5aaf319c05e2fd38c8e1c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Fri, 8 Dec 2017 23:41:16 +0000 Subject: [PATCH] Define can_create, can_edit, can_delete and can_view and use can_create and can_edit decorators for preferences.models --- preferences/models.py | 121 ++++++++++++++++++ .../templates/preferences/aff_service.html | 2 +- .../preferences/display_preferences.html | 2 +- preferences/urls.py | 8 +- preferences/views.py | 34 +++-- 5 files changed, 142 insertions(+), 25 deletions(-) diff --git a/preferences/models.py b/preferences/models.py index 3124683d..a24cf9de 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -47,6 +47,23 @@ class OptionalUser(models.Model): help_text="Tous les users peuvent en créer d'autres", ) + def get_instance(*args, **kwargs): + return OptionalUser.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences concernant les users" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences concernant les utilisateurs" + def clean(self): """Creation du mode de paiement par solde""" if self.user_solde: @@ -63,6 +80,23 @@ class OptionalMachine(models.Model): max_lambdauser_aliases = models.IntegerField(default=10) ipv6 = models.BooleanField(default=False) + def get_instance(*args, **kwargs): + return OptionalMachine.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences concernant les machines" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences concernant les machines" + class OptionalTopologie(models.Model): """Reglages pour la topologie : mode d'accès radius, vlan où placer @@ -96,6 +130,23 @@ class OptionalTopologie(models.Model): null=True ) + def get_instance(*args, **kwargs): + return OptionalTopologie.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences concernant la topologie" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences concernant la topologie" + class GeneralOption(models.Model): """Options générales : nombre de resultats par page, nom du site, @@ -114,6 +165,23 @@ class GeneralOption(models.Model): site_name = models.CharField(max_length=32, default="Re2o") email_from = models.EmailField(default="www-data@serveur.net") + def get_instance(*args, **kwargs): + return GeneralOption.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences générales" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences générales" + class Service(models.Model): """Liste des services affichés sur la page d'accueil : url, description, @@ -123,6 +191,25 @@ class Service(models.Model): description = models.TextField() image = models.ImageField(upload_to='logo', blank=True) + def get_instance(serviceid, *args, **kwargs): + return Service.objects.get(pk=serviceid) + + def can_create(user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + de créer un service pour la page d'accueil" + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les services pour la page d'accueil" + + def can_delete(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + de supprimer les services pour la page d'accueil" + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les services pour la page d'accueil" + def __str__(self): return str(self.name) @@ -148,6 +235,23 @@ class AssoOption(models.Model): null=True ) + def get_instance(*args, **kwargs): + return AssoOption.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences concernant l'association" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences concernant l'association" + class MailMessageOption(models.Model): """Reglages, mail de bienvenue et autre""" @@ -155,3 +259,20 @@ class MailMessageOption(models.Model): welcome_mail_fr = models.TextField(default="") welcome_mail_en = models.TextField(default="") + + def get_instance(*args, **kwargs): + return MailMessageOption.objects.get_or_create() + + def can_create(user_request, *args, **kwargs): + return True, None + + def can_edit(self, user_request, *args, **kwargs): + return user_request.has_perms(('admin',)), u"Vous n'avez pas le droit\ + d'éditer les préférences concernant les mails" + + def can_delete(self, user_request, *args, **kwargs): + return True, None + + def can_view(self, user_request, *args, **kwargs): + return user_request.has_perms(('cableur',)), u"Vous n'avez pas le droit\ + de voir les préférences concernant les mails" diff --git a/preferences/templates/preferences/aff_service.html b/preferences/templates/preferences/aff_service.html index e5f8aecf..c788c616 100644 --- a/preferences/templates/preferences/aff_service.html +++ b/preferences/templates/preferences/aff_service.html @@ -41,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ service.image }} {% if is_admin %} - {% include 'buttons/edit.html' with href='preferences:edit-services' id=service.id %} + {% include 'buttons/edit.html' with href='preferences:edit-service' id=service.id %} {% endif %} {% include 'buttons/history.html' with href='preferences:history' name='service' id=service.id %} diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index ad807155..6e687d71 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -191,7 +191,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

Liste des services page d'accueil

{% if is_infra %} - Ajouter un service + Ajouter un service Supprimer un ou plusieurs service {% endif %} {% include "preferences/aff_service.html" with service_list=service_list %} diff --git a/preferences/urls.py b/preferences/urls.py index f10d25a0..3f6718de 100644 --- a/preferences/urls.py +++ b/preferences/urls.py @@ -61,11 +61,11 @@ urlpatterns = [ views.edit_options, name='edit-options' ), - url(r'^add_services/$', views.add_services, name='add-services'), + url(r'^add_service/$', views.add_service, name='add-service'), url( - r'^edit_services/(?P[0-9]+)$', - views.edit_services, - name='edit-services' + r'^edit_service/(?P[0-9]+)$', + views.edit_service, + name='edit-service' ), url(r'^del_services/$', views.del_services, name='del-services'), url( diff --git a/preferences/views.py b/preferences/views.py index 493c1d14..c568a8e9 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -42,6 +42,7 @@ from reversion.models import Version from reversion import revisions as reversion from re2o.views import form +from re2o.utils import can_create, can_edit from .forms import ServiceForm, DelServiceForm from .models import Service, OptionalUser, OptionalMachine, AssoOption from .models import MailMessageOption, GeneralOption, OptionalTopologie @@ -106,47 +107,42 @@ def edit_options(request, section): @login_required -@permission_required('admin') -def add_services(request): +@can_create(Service) +def add_service(request): """Ajout d'un service de la page d'accueil""" - services = ServiceForm(request.POST or None) - if services.is_valid(): + service = ServiceForm(request.POST or None) + if service.is_valid(): with transaction.atomic(), reversion.create_revision(): - services.save() + service.save() reversion.set_user(request.user) reversion.set_comment("Création") messages.success(request, "Ce service a été ajouté") return redirect(reverse('preferences:display-options')) return form( - {'preferenceform': services}, + {'preferenceform': service}, 'preferences/preferences.html', request ) @login_required -@permission_required('admin') -def edit_services(request, servicesid): +@can_edit(Service) +def edit_service(request, service_instance, serviceid): """Edition des services affichés sur la page d'accueil""" - try: - services_instance = Service.objects.get(pk=servicesid) - except Service.DoesNotExist: - messages.error(request, u"Entrée inexistante") - return redirect(reverse('preferences:display-options')) - services = ServiceForm(request.POST or None, instance=services_instance) - if services.is_valid(): + service = ServiceForm(request.POST or None, instance=service_instance) + if service.is_valid(): with transaction.atomic(), reversion.create_revision(): - services.save() + service.save() reversion.set_user(request.user) reversion.set_comment( "Champs modifié(s) : %s" % ', '.join( - field for field in services.changed_data + field for field in service.changed_data ) ) messages.success(request, "Service modifié") return redirect(reverse('preferences:display-options')) return form( - {'preferenceform': services}, + {'preferenceform': service}, 'preferences/preferences.html', request ) @@ -164,7 +160,7 @@ def del_services(request): with transaction.atomic(), reversion.create_revision(): services_del.delete() reversion.set_user(request.user) - messages.success(request, "Le services a été supprimée") + messages.success(request, "Le service a été supprimée") except ProtectedError: messages.error(request, "Erreur le service\ suivant %s ne peut être supprimé" % services_del)