From 3ace31e24eec1b49dfc71c98714651382dc95059 Mon Sep 17 00:00:00 2001 From: LEVY-FALK Hugo Date: Thu, 26 Oct 2017 23:29:26 +0200 Subject: [PATCH] =?UTF-8?q?Passage=20de=20la=20logique=20dans=20le=20mod?= =?UTF-8?q?=C3=A8le.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/forms.py | 2 +- topologie/models.py | 31 +++++++++++++++++++++++++++++++ topologie/views.py | 33 +++++++++------------------------ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/topologie/forms.py b/topologie/forms.py index 0119435d..39759695 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -34,7 +34,7 @@ from __future__ import unicode_literals from machines.models import Interface from django import forms -from django.forms import ModelForm +from django.forms import ModelForm, Form from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch diff --git a/topologie/models.py b/topologie/models.py index 0f277434..fca4a3be 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -37,10 +37,15 @@ la prise from __future__ import unicode_literals +import itertools + from django.db import models from django.db.models.signals import post_delete from django.dispatch import receiver 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): @@ -125,6 +130,32 @@ class Switch(models.Model): else: raise ValidationError({'stack_member_id': "L'id dans la stack\ 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): diff --git a/topologie/views.py b/topologie/views.py index 7bed18aa..64c4aeb6 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -35,14 +35,13 @@ coté models et forms de topologie """ from __future__ import unicode_literals -import itertools - from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required from django.db import IntegrityError from django.db import transaction from django.db.models import ProtectedError +from django.core.exceptions import ValidationError from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from reversion import revisions as reversion from reversion.models import Version @@ -491,42 +490,28 @@ def create_ports(request, switch_id): except Switch.DoesNotExist: messages.error(request, u"Switch inexistant") return redirect("/topologie/") - + s_begin = s_end = 0 nb_ports = switch.ports.count() if nb_ports > 0: ports = switch.ports.order_by('port').values('port') s_begin = ports.first().get('port') s_end = ports.last().get('port') - + port_form = CreatePortsForm( request.POST or None, initial={'begin': s_begin, 'end': s_end} ) + if port_form.is_valid(): begin = port_form.cleaned_data['begin'] 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)) + try: + switch.create_ports(begin, end) + messages.success(request, "Ports créés.") + except ValidationError as e: + messages.error(request, ''.join(e)) - 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: - with transaction.atomic(), reversion.create_revision(): - port.save() - reversion.set_user(request.user) - reversion.set_comment("Création") - 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 form({'topoform': port_form}, 'topologie/switch.html', request)