mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-23 03:43:12 +00:00
Passage de la logique dans le modèle.
This commit is contained in:
parent
bb0db9ef13
commit
181f70176b
3 changed files with 41 additions and 25 deletions
|
@ -34,7 +34,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from machines.models import Interface
|
from machines.models import Interface
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.forms import ModelForm
|
from django.forms import ModelForm, Form
|
||||||
from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch
|
from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,10 +37,15 @@ la prise
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.signals import post_delete
|
from django.db.models.signals import post_delete
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.db import IntegrityError
|
||||||
|
from django.db import transaction
|
||||||
|
from reversion import revisions as reversion
|
||||||
|
|
||||||
|
|
||||||
class Stack(models.Model):
|
class Stack(models.Model):
|
||||||
|
@ -125,6 +130,32 @@ class Switch(models.Model):
|
||||||
else:
|
else:
|
||||||
raise ValidationError({'stack_member_id': "L'id dans la stack\
|
raise ValidationError({'stack_member_id': "L'id dans la stack\
|
||||||
ne peut être nul"})
|
ne peut être nul"})
|
||||||
|
def create_ports(self, begin, end):
|
||||||
|
""" Crée les ports de begin à end si les valeurs données sont cohérentes. """
|
||||||
|
|
||||||
|
s_begin = s_end = 0
|
||||||
|
nb_ports = self.ports.count()
|
||||||
|
if nb_ports > 0:
|
||||||
|
ports = self.ports.order_by('port').values('port')
|
||||||
|
s_begin = ports.first().get('port')
|
||||||
|
s_end = ports.last().get('port')
|
||||||
|
|
||||||
|
if end < begin:
|
||||||
|
raise ValidationError("Port de fin inférieur au port de début !")
|
||||||
|
if end - begin > self.number:
|
||||||
|
raise ValidationError("Ce switch ne peut avoir autant de ports.")
|
||||||
|
begin_range = range(begin, s_begin)
|
||||||
|
end_range = range(s_end+1, end+1)
|
||||||
|
for i in itertools.chain(begin_range, end_range):
|
||||||
|
port = Port()
|
||||||
|
port.switch = self
|
||||||
|
port.port = i
|
||||||
|
try:
|
||||||
|
with transaction.atomic(), reversion.create_revision():
|
||||||
|
port.save()
|
||||||
|
reversion.set_comment("Création")
|
||||||
|
except IntegrityError:
|
||||||
|
ValidationError("Création d'un port existant.")
|
||||||
|
|
||||||
|
|
||||||
class ModelSwitch(models.Model):
|
class ModelSwitch(models.Model):
|
||||||
|
|
|
@ -35,14 +35,13 @@ coté models et forms de topologie
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import itertools
|
|
||||||
|
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
from django.contrib.auth.decorators import login_required, permission_required
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import ProtectedError
|
from django.db.models import ProtectedError
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||||
from reversion import revisions as reversion
|
from reversion import revisions as reversion
|
||||||
from reversion.models import Version
|
from reversion.models import Version
|
||||||
|
@ -503,30 +502,16 @@ def create_ports(request, switch_id):
|
||||||
request.POST or None,
|
request.POST or None,
|
||||||
initial={'begin': s_begin, 'end': s_end}
|
initial={'begin': s_begin, 'end': s_end}
|
||||||
)
|
)
|
||||||
|
|
||||||
if port_form.is_valid():
|
if port_form.is_valid():
|
||||||
begin = port_form.cleaned_data['begin']
|
begin = port_form.cleaned_data['begin']
|
||||||
end = port_form.cleaned_data['end']
|
end = port_form.cleaned_data['end']
|
||||||
if end < begin:
|
|
||||||
messages.error(request, "Port de fin inférieur au port de début !")
|
|
||||||
return redirect("/topologie/switch/" + str(switch.id))
|
|
||||||
if end - begin > switch.number:
|
|
||||||
messages.error(request, "Ce switch ne peut avoir autant de ports.")
|
|
||||||
return redirect("/topologie/switch/" + str(switch.id))
|
|
||||||
|
|
||||||
begin_range = range(begin, s_begin)
|
|
||||||
end_range = range(s_end+1, end+1)
|
|
||||||
for i in itertools.chain(begin_range, end_range):
|
|
||||||
port = Port()
|
|
||||||
port.switch = switch
|
|
||||||
port.port = i
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic(), reversion.create_revision():
|
switch.create_ports(begin, end)
|
||||||
port.save()
|
messages.success(request, "Ports créés.")
|
||||||
reversion.set_user(request.user)
|
except ValidationError as e:
|
||||||
reversion.set_comment("Création")
|
messages.error(request, ''.join(e))
|
||||||
messages.success(request, "Création du port %d" % i)
|
|
||||||
except IntegrityError:
|
|
||||||
messages.error(request, "Création d'un port existant.")
|
|
||||||
return redirect("/topologie/switch/" + str(switch.id))
|
return redirect("/topologie/switch/" + str(switch.id))
|
||||||
|
|
||||||
return form({'topoform': port_form}, 'topologie/switch.html', request)
|
return form({'topoform': port_form}, 'topologie/switch.html', request)
|
||||||
|
|
Loading…
Reference in a new issue