mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-24 07:53:47 +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:
parent
7b3ece0d29
commit
c74370086e
5 changed files with 142 additions and 25 deletions
|
@ -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"
|
||||
|
|
|
@ -41,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
<td>{{ service.image }}</td>
|
||||
<td class="text-right">
|
||||
{% 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 %}
|
||||
</td>
|
||||
|
|
|
@ -191,7 +191,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
</table>
|
||||
<h2>Liste des services page d'accueil</h2>
|
||||
{% 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>
|
||||
{% endif %}
|
||||
{% include "preferences/aff_service.html" with service_list=service_list %}
|
||||
|
|
|
@ -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<servicesid>[0-9]+)$',
|
||||
views.edit_services,
|
||||
name='edit-services'
|
||||
r'^edit_service/(?P<serviceid>[0-9]+)$',
|
||||
views.edit_service,
|
||||
name='edit-service'
|
||||
),
|
||||
url(r'^del_services/$', views.del_services, name='del-services'),
|
||||
url(
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue