8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 11:53:12 +00:00

Merge branch 'sw_port_number' into 'dev'

Sw port number

See merge request federez/re2o!375
This commit is contained in:
klafyvel 2019-01-01 20:22:45 +01:00
commit de068f6c4c
3 changed files with 13 additions and 37 deletions

View file

@ -115,11 +115,6 @@ class EditOptionalTopologieForm(ModelForm):
prefix=prefix, prefix=prefix,
**kwargs **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') self.initial['automatic_provision_switchs'] = Switch.objects.filter(automatic_provision=True).order_by('interface__domain__name')

View file

@ -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 = 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
@ -317,7 +304,7 @@ class Switch(AclMixin, Machine):
@cached_property @cached_property
def get_name(self): 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 @cached_property
def get_radius_key(self): def get_radius_key(self):

View file

@ -533,19 +533,13 @@ def create_ports(request, switchid):
except Switch.DoesNotExist: except Switch.DoesNotExist:
messages.error(request, _("Nonexistent switch")) messages.error(request, _("Nonexistent switch"))
return redirect(reverse('topologie:index')) return redirect(reverse('topologie:index'))
s_begin = s_end = 0 first_port = getattr(switch.ports.order_by('port').first(), 'port', 1)
nb_ports = switch.ports.count() last_port = switch.number + first_port - 1
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( port_form = CreatePortsForm(
request.POST or None, request.POST or None,
initial={'begin': s_begin, 'end': s_end} initial={'begin': first_port, 'end': last_port}
) )
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']