From e1729235780fd9a4523a2534ec39a1a038082370 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sun, 30 Dec 2018 00:18:35 +0100 Subject: [PATCH 1/6] Fix #103 --- topologie/models.py | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/topologie/models.py b/topologie/models.py index cd191d7e..25a7bda6 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -281,31 +281,18 @@ class Switch(AclMixin, Machine): 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(_("The end port is less than the start" " port.")) - if end - begin > self.number: + ports_to_create = list(range(begin, end + 1)) + existing_ports = Port.objects.filter(switch=self.switch).values_list('port', flat=True) + non_existing_ports = list(set(ports_to_create) - set(existing_ports)) + + if len(non_existing_ports) + existing_ports.count() > self.number: raise ValidationError(_("This switch can't have that many 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(_("Creation")) - except IntegrityError: - ValidationError(_("Creation of an existing port.")) + with transaction.atomic(), reversion.create_revision(): + reversion.set_comment(_("Creation")) + Port.objects.bulk_create([Port(switch=self.switch, port=port_id) for port_id in non_existing_ports]) def main_interface(self): """ Returns the 'main' interface of the switch From 92e6ae45ad22413f370008f5fb5a83b81a77e9ee Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sun, 30 Dec 2018 00:34:17 +0100 Subject: [PATCH 2/6] Refactor aussi la fonction du views pour la route --- topologie/views.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index a4db2dc6..d852177d 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -533,19 +533,14 @@ def create_ports(request, switchid): except Switch.DoesNotExist: messages.error(request, _("Nonexistent switch")) return redirect(reverse('topologie:index')) - - 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') - + + first_port = getattr(switch.ports.order_by('port').first(), 'port', 1) + s_begin = first_port + s_end = switch.number + first_port - 1 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'] From 7ddb627b6b0f780334712146e34dc8cdd73efcea Mon Sep 17 00:00:00 2001 From: klafyvel Date: Sun, 30 Dec 2018 14:27:41 +0100 Subject: [PATCH 3/6] Do not create an useless list since `set` can be created from any iterable. --- topologie/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/models.py b/topologie/models.py index 25a7bda6..269516ff 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -284,7 +284,7 @@ class Switch(AclMixin, Machine): if end < begin: raise ValidationError(_("The end port is less than the start" " port.")) - ports_to_create = list(range(begin, end + 1)) + ports_to_create = range(begin, end + 1) existing_ports = Port.objects.filter(switch=self.switch).values_list('port', flat=True) non_existing_ports = list(set(ports_to_create) - set(existing_ports)) From c238a9a2fa2dac6febc8bd5a5b01ac3bd25732cd Mon Sep 17 00:00:00 2001 From: klafyvel Date: Sun, 30 Dec 2018 14:31:36 +0100 Subject: [PATCH 4/6] Remove useless variable in create_ports and give explicit name to `s_end`. --- topologie/views.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index d852177d..89d48b0b 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -535,11 +535,10 @@ def create_ports(request, switchid): return redirect(reverse('topologie:index')) first_port = getattr(switch.ports.order_by('port').first(), 'port', 1) - s_begin = first_port - s_end = switch.number + first_port - 1 + last_port = switch.number + first_port - 1 port_form = CreatePortsForm( request.POST or None, - initial={'begin': s_begin, 'end': s_end} + initial={'begin': first_port, 'end': last_port} ) if port_form.is_valid(): begin = port_form.cleaned_data['begin'] From 71dc9c9bd1ec3468bc49f712723267b09ef65e7d Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Tue, 1 Jan 2019 19:32:40 +0100 Subject: [PATCH 5/6] Remove code about radius in OptionalTopology edit form. --- preferences/forms.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/preferences/forms.py b/preferences/forms.py index 1126b399..fd052edd 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -115,11 +115,6 @@ class EditOptionalTopologieForm(ModelForm): prefix=prefix, **kwargs ) - self.fields['radius_general_policy'].label = _("RADIUS general policy") - self.fields['vlan_decision_ok'].label = _("VLAN for machines accepted" - " by RADIUS") - self.fields['vlan_decision_nok'].label = _("VLAN for machines rejected" - " by RADIUS") self.initial['automatic_provision_switchs'] = Switch.objects.filter(automatic_provision=True).order_by('interface__domain__name') From 21f54486b20e8413153e53d00fa5cab3132307c9 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Tue, 1 Jan 2019 20:17:27 +0100 Subject: [PATCH 6/6] Fix switch creation --- topologie/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/models.py b/topologie/models.py index 269516ff..e08a1b21 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -304,7 +304,7 @@ class Switch(AclMixin, Machine): @cached_property def get_name(self): - return self.name or self.main_interface().domain.name + return self.name or getattr(self.main_interface(), 'domain', 'Unknown') @cached_property def get_radius_key(self):