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

Define can_create, can_edit, can_delete and can_view and use can_create and can_edit decorators for preferences.models

This commit is contained in:
Maël Kervella 2017-12-08 23:41:16 +00:00 committed by root
parent f548d48447
commit 876021ae12
5 changed files with 142 additions and 25 deletions

View file

@ -47,6 +47,23 @@ class OptionalUser(models.Model):
help_text="Tous les users peuvent en créer d'autres", 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): def clean(self):
"""Creation du mode de paiement par solde""" """Creation du mode de paiement par solde"""
if self.user_solde: if self.user_solde:
@ -63,6 +80,23 @@ class OptionalMachine(models.Model):
max_lambdauser_aliases = models.IntegerField(default=10) max_lambdauser_aliases = models.IntegerField(default=10)
ipv6 = models.BooleanField(default=False) 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): class OptionalTopologie(models.Model):
"""Reglages pour la topologie : mode d'accès radius, vlan où placer """Reglages pour la topologie : mode d'accès radius, vlan où placer
@ -96,6 +130,23 @@ class OptionalTopologie(models.Model):
null=True 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): class GeneralOption(models.Model):
"""Options générales : nombre de resultats par page, nom du site, """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") site_name = models.CharField(max_length=32, default="Re2o")
email_from = models.EmailField(default="www-data@serveur.net") 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): class Service(models.Model):
"""Liste des services affichés sur la page d'accueil : url, description, """Liste des services affichés sur la page d'accueil : url, description,
@ -123,6 +191,25 @@ class Service(models.Model):
description = models.TextField() description = models.TextField()
image = models.ImageField(upload_to='logo', blank=True) 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): def __str__(self):
return str(self.name) return str(self.name)
@ -148,6 +235,23 @@ class AssoOption(models.Model):
null=True 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): class MailMessageOption(models.Model):
"""Reglages, mail de bienvenue et autre""" """Reglages, mail de bienvenue et autre"""
@ -155,3 +259,20 @@ class MailMessageOption(models.Model):
welcome_mail_fr = models.TextField(default="") welcome_mail_fr = models.TextField(default="")
welcome_mail_en = 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"

View file

@ -41,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{ service.image }}</td> <td>{{ service.image }}</td>
<td class="text-right"> <td class="text-right">
{% if is_admin %} {% 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 %} {% endif %}
{% include 'buttons/history.html' with href='preferences:history' name='service' id=service.id %} {% include 'buttons/history.html' with href='preferences:history' name='service' id=service.id %}
</td> </td>

View file

@ -191,7 +191,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
</table> </table>
<h2>Liste des services page d'accueil</h2> <h2>Liste des services page d'accueil</h2>
{% if is_infra %} {% if is_infra %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'preferences:add-services' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un service</a> <a class="btn btn-primary btn-sm" role="button" href="{% url 'preferences:add-service' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un service</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'preferences:del-services' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un ou plusieurs service</a> <a class="btn btn-danger btn-sm" role="button" href="{% url 'preferences:del-services' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un ou plusieurs service</a>
{% endif %} {% endif %}
{% include "preferences/aff_service.html" with service_list=service_list %} {% include "preferences/aff_service.html" with service_list=service_list %}

View file

@ -61,11 +61,11 @@ urlpatterns = [
views.edit_options, views.edit_options,
name='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( url(
r'^edit_services/(?P<servicesid>[0-9]+)$', r'^edit_service/(?P<serviceid>[0-9]+)$',
views.edit_services, views.edit_service,
name='edit-services' name='edit-service'
), ),
url(r'^del_services/$', views.del_services, name='del-services'), url(r'^del_services/$', views.del_services, name='del-services'),
url( url(

View file

@ -42,6 +42,7 @@ from reversion.models import Version
from reversion import revisions as reversion from reversion import revisions as reversion
from re2o.views import form from re2o.views import form
from re2o.utils import can_create, can_edit
from .forms import ServiceForm, DelServiceForm from .forms import ServiceForm, DelServiceForm
from .models import Service, OptionalUser, OptionalMachine, AssoOption from .models import Service, OptionalUser, OptionalMachine, AssoOption
from .models import MailMessageOption, GeneralOption, OptionalTopologie from .models import MailMessageOption, GeneralOption, OptionalTopologie
@ -106,47 +107,42 @@ def edit_options(request, section):
@login_required @login_required
@permission_required('admin') @can_create(Service)
def add_services(request): def add_service(request):
"""Ajout d'un service de la page d'accueil""" """Ajout d'un service de la page d'accueil"""
services = ServiceForm(request.POST or None) service = ServiceForm(request.POST or None)
if services.is_valid(): if service.is_valid():
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
services.save() service.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") reversion.set_comment("Création")
messages.success(request, "Ce service a été ajouté") messages.success(request, "Ce service a été ajouté")
return redirect(reverse('preferences:display-options')) return redirect(reverse('preferences:display-options'))
return form( return form(
{'preferenceform': services}, {'preferenceform': service},
'preferences/preferences.html', 'preferences/preferences.html',
request request
) )
@login_required @login_required
@permission_required('admin') @can_edit(Service)
def edit_services(request, servicesid): def edit_service(request, service_instance, serviceid):
"""Edition des services affichés sur la page d'accueil""" """Edition des services affichés sur la page d'accueil"""
try: service = ServiceForm(request.POST or None, instance=service_instance)
services_instance = Service.objects.get(pk=servicesid) if service.is_valid():
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():
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
services.save() service.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment( reversion.set_comment(
"Champs modifié(s) : %s" % ', '.join( "Champs modifié(s) : %s" % ', '.join(
field for field in services.changed_data field for field in service.changed_data
) )
) )
messages.success(request, "Service modifié") messages.success(request, "Service modifié")
return redirect(reverse('preferences:display-options')) return redirect(reverse('preferences:display-options'))
return form( return form(
{'preferenceform': services}, {'preferenceform': service},
'preferences/preferences.html', 'preferences/preferences.html',
request request
) )
@ -164,7 +160,7 @@ def del_services(request):
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
services_del.delete() services_del.delete()
reversion.set_user(request.user) 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: except ProtectedError:
messages.error(request, "Erreur le service\ messages.error(request, "Erreur le service\
suivant %s ne peut être supprimé" % services_del) suivant %s ne peut être supprimé" % services_del)