mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-27 07:02:26 +00:00
Fix #103
This commit is contained in:
parent
f27f952143
commit
e172923578
1 changed files with 8 additions and 21 deletions
|
@ -281,31 +281,18 @@ class Switch(AclMixin, Machine):
|
||||||
def create_ports(self, begin, end):
|
def create_ports(self, begin, end):
|
||||||
""" Crée les ports de begin à end si les valeurs données
|
""" Crée les ports de begin à end si les valeurs données
|
||||||
sont cohérentes. """
|
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:
|
if end < begin:
|
||||||
raise ValidationError(_("The end port is less than the start"
|
raise ValidationError(_("The end port is less than the start"
|
||||||
" port."))
|
" 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."))
|
raise ValidationError(_("This switch can't have that many ports."))
|
||||||
begin_range = range(begin, s_begin)
|
with transaction.atomic(), reversion.create_revision():
|
||||||
end_range = range(s_end+1, end+1)
|
reversion.set_comment(_("Creation"))
|
||||||
for i in itertools.chain(begin_range, end_range):
|
Port.objects.bulk_create([Port(switch=self.switch, port=port_id) for port_id in non_existing_ports])
|
||||||
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."))
|
|
||||||
|
|
||||||
def main_interface(self):
|
def main_interface(self):
|
||||||
""" Returns the 'main' interface of the switch
|
""" Returns the 'main' interface of the switch
|
||||||
|
|
Loading…
Reference in a new issue