mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-27 15:12:25 +00:00
can_create for Machine
This commit is contained in:
parent
d4d3ec3131
commit
4a57a604d9
3 changed files with 49 additions and 28 deletions
|
@ -37,6 +37,9 @@ from django.core.validators import MaxValueValidator
|
||||||
|
|
||||||
from macaddress.fields import MACAddressField
|
from macaddress.fields import MACAddressField
|
||||||
|
|
||||||
|
import users.models
|
||||||
|
import preferences.models
|
||||||
|
|
||||||
|
|
||||||
class Machine(models.Model):
|
class Machine(models.Model):
|
||||||
""" Class définissant une machine, object parent user, objets fils
|
""" Class définissant une machine, object parent user, objets fils
|
||||||
|
@ -52,6 +55,23 @@ class Machine(models.Model):
|
||||||
)
|
)
|
||||||
active = models.BooleanField(default=True)
|
active = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
def can_create(user_request, userid_dest):
|
||||||
|
try:
|
||||||
|
user = users.models.User.objects.get(pk=userid_dest)
|
||||||
|
except users.models.User.DoesNotExist:
|
||||||
|
return False, u"Utilisateur inexistant"
|
||||||
|
options, created = preferences.models.OptionalMachine.objects.get_or_create()
|
||||||
|
max_lambdauser_interfaces = options.max_lambdauser_interfaces
|
||||||
|
if not user_request.has_perms(('cableur',)):
|
||||||
|
if user != user_request:
|
||||||
|
return False, u"Vous ne pouvez pas ajouter une machine à un\
|
||||||
|
autre user que vous sans droit"
|
||||||
|
if user.user_interfaces().count() >= max_lambdauser_interfaces:
|
||||||
|
return False, u"Vous avez atteint le maximum d'interfaces\
|
||||||
|
autorisées que vous pouvez créer vous même (%s) "\
|
||||||
|
% max_lambdauser_interfaces
|
||||||
|
return True, None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
|
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
|
||||||
|
|
||||||
|
|
|
@ -214,30 +214,22 @@ def new_machine(request, userid):
|
||||||
""" Fonction de creation d'une machine. Cree l'objet machine,
|
""" Fonction de creation d'une machine. Cree l'objet machine,
|
||||||
le sous objet interface et l'objet domain à partir de model forms.
|
le sous objet interface et l'objet domain à partir de model forms.
|
||||||
Trop complexe, devrait être simplifié"""
|
Trop complexe, devrait être simplifié"""
|
||||||
try:
|
|
||||||
|
can, reason = Machine.can_create(request.user, userid)
|
||||||
|
if not can:
|
||||||
|
messages.error(request, reason)
|
||||||
|
return redirect(reverse(
|
||||||
|
'users:profil',
|
||||||
|
kwargs={'userid':str(request.user.id)}
|
||||||
|
))
|
||||||
|
|
||||||
|
# No need to check if userid exist, already done in can_create
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
|
||||||
return redirect(reverse('machines:index'))
|
|
||||||
options, created = OptionalMachine.objects.get_or_create()
|
|
||||||
max_lambdauser_interfaces = options.max_lambdauser_interfaces
|
|
||||||
if not request.user.has_perms(('cableur',)):
|
|
||||||
if user != request.user:
|
|
||||||
messages.error(
|
|
||||||
request,
|
|
||||||
"Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit")
|
|
||||||
return redirect(reverse(
|
|
||||||
'users:profil',
|
|
||||||
kwargs={'userid':str(request.user.id)}
|
|
||||||
))
|
|
||||||
if user.user_interfaces().count() >= max_lambdauser_interfaces:
|
|
||||||
messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
|
|
||||||
return redirect(reverse(
|
|
||||||
'users:profil',
|
|
||||||
kwargs={'userid':str(request.user.id)}
|
|
||||||
))
|
|
||||||
machine = NewMachineForm(request.POST or None)
|
machine = NewMachineForm(request.POST or None)
|
||||||
interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
|
interface = AddInterfaceForm(
|
||||||
|
request.POST or None,
|
||||||
|
infra=request.user.has_perms(('infra',))
|
||||||
|
)
|
||||||
domain = DomainForm(request.POST or None, user=user)
|
domain = DomainForm(request.POST or None, user=user)
|
||||||
if machine.is_valid() and interface.is_valid():
|
if machine.is_valid() and interface.is_valid():
|
||||||
new_machine = machine.save(commit=False)
|
new_machine = machine.save(commit=False)
|
||||||
|
@ -266,7 +258,16 @@ def new_machine(request, userid):
|
||||||
kwargs={'userid':str(user.id)}
|
kwargs={'userid':str(user.id)}
|
||||||
))
|
))
|
||||||
i_mbf_param = generate_ipv4_mbf_param(interface, False)
|
i_mbf_param = generate_ipv4_mbf_param(interface, False)
|
||||||
return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain, 'i_mbf_param': i_mbf_param}, 'machines/machine.html', request)
|
return form(
|
||||||
|
{
|
||||||
|
'machineform': machine,
|
||||||
|
'interfaceform': interface,
|
||||||
|
'domainform': domain,
|
||||||
|
'i_mbf_param': i_mbf_param
|
||||||
|
},
|
||||||
|
'machines/machine.html',
|
||||||
|
request
|
||||||
|
)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def edit_interface(request, interfaceid):
|
def edit_interface(request, interfaceid):
|
||||||
|
|
|
@ -26,7 +26,7 @@ Reglages généraux, machines, utilisateurs, mail, general pour l'application.
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from cotisations.models import Paiement
|
import cotisations.models
|
||||||
|
|
||||||
|
|
||||||
class OptionalUser(models.Model):
|
class OptionalUser(models.Model):
|
||||||
|
@ -50,7 +50,7 @@ class OptionalUser(models.Model):
|
||||||
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:
|
||||||
Paiement.objects.get_or_create(moyen="Solde")
|
cotisations.models.Paiement.objects.get_or_create(moyen="Solde")
|
||||||
|
|
||||||
|
|
||||||
class OptionalMachine(models.Model):
|
class OptionalMachine(models.Model):
|
||||||
|
|
Loading…
Reference in a new issue