From e19e5d8139b7244310872b20d6802fc920477895 Mon Sep 17 00:00:00 2001 From: Caique Coelho Date: Thu, 26 Oct 2023 00:38:24 -0300 Subject: [PATCH] Translation to english and exeception refactoring --- networkapi/api_channel/facade.py | 18 +++++++++--------- networkapi/api_interface/facade.py | 10 ++++------ .../resource/InterfaceChannelResource.py | 18 +++++++++--------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/networkapi/api_channel/facade.py b/networkapi/api_channel/facade.py index 14fbba264..4bce29a4a 100644 --- a/networkapi/api_channel/facade.py +++ b/networkapi/api_channel/facade.py @@ -60,7 +60,7 @@ def create(self, data): vlan_nativa = data.get('vlan') envs_vlans = data.get('envs_vlans') - api_interface_facade.verificar_vlan_nativa(vlan_nativa) + api_interface_facade.verify_native_vlan(vlan_nativa) # Checks if Port Channel name already exists on equipment api_interface_facade.check_channel_name_on_equipment(nome, interfaces) @@ -79,7 +79,7 @@ def create(self, data): if iface.channel: raise InterfaceError( 'Interface %s is already a Channel' % iface.interface - ) + ) if iface.equipamento.id not in ifaces_on_channel: ifaces_on_channel.append(int(iface.equipamento.id)) @@ -107,7 +107,7 @@ def _update_interfaces_from_a_channel(self, iface, vlan_nativa, if iface.channel: raise InterfaceError( 'Interface %s is already a Channel' % iface.interface - ) + ) if iface.equipamento.id not in ifaces_on_channel: ifaces_on_channel.append(int(iface.equipamento.id)) @@ -190,12 +190,12 @@ def update(self, data): raise InvalidValueError(None, 'Channel number', 'must be integer.') - api_interface_facade.verificar_vlan_nativa(vlan_nativa) + api_interface_facade.verify_native_vlan(vlan_nativa) # Dissociate old interfaces interfaces_old = Interface.objects.filter( channel__id=int(id_channel) - ) + ) log.debug(interfaces_old) server = None for i in interfaces_old: @@ -229,14 +229,14 @@ def update(self, data): raise InterfaceError( 'Interface %s is already in a Channel' % iface.interface - ) + ) if iface.equipamento.id not in ifaces_on_channel: ifaces_on_channel.append(int(iface.equipamento.id)) if len(ifaces_on_channel) > 2: raise InterfaceError( 'More than one equipment selected.' - ) + ) iface.channel = self.channel iface.tipo = type_obj @@ -327,7 +327,7 @@ def _get_equipment_dict(self, interfaces): for equip_id in [i.equipamento.id for i in interfaces]: equip_dict[str(equip_id)] = interfaces.filter( - equipamento__id=equip_id) + equipamento__id=equip_id) return equip_dict @@ -356,7 +356,7 @@ def _update_equipments(self, equip_dict, iface_type, user, channel): ligacao_back_id=back, tipo=iface_type, vlan_nativa='1' - ) + ) api_interface_facade.delete_channel( self.user, equip_id, ifaces, channel) diff --git a/networkapi/api_interface/facade.py b/networkapi/api_interface/facade.py index ee149ab61..40a2e2a1b 100644 --- a/networkapi/api_interface/facade.py +++ b/networkapi/api_interface/facade.py @@ -714,12 +714,10 @@ def verificar_vlan_range(amb, vlans): 'definido para o ambiente') -def verificar_vlan_nativa(vlan_nativa): - log.info("verificar_vlan_nativa") - - if vlan_nativa is not None: - if int(vlan_nativa) < 1 or 3967 < int(vlan_nativa) < 4048 or int(vlan_nativa) >= 4096: - raise InvalidValueError(None, 'Vlan Nativa', 'Range valido: 1-3967, 4048-4095.') +def verify_native_vlan(native_vlan): + if native_vlan is not None: + if int(native_vlan) < 1 or int(native_vlan) > 3967 or int(native_vlan) < 4048 or int(native_vlan) >= 4096: + raise InvalidValueError(None, 'Vlan Nativa', 'Valid range: 1-3967, 4048-4095.') def check_channel_name_on_equipment(nome, interfaces): diff --git a/networkapi/interface/resource/InterfaceChannelResource.py b/networkapi/interface/resource/InterfaceChannelResource.py index c6498a7d1..f1154de93 100644 --- a/networkapi/interface/resource/InterfaceChannelResource.py +++ b/networkapi/interface/resource/InterfaceChannelResource.py @@ -137,7 +137,7 @@ def handle_post(self, request, user, *args, **kwargs): amb = Ambiente() cont = [] - api_interface_facade.verificar_vlan_nativa(vlan_nativa) + api_interface_facade.verify_native_vlan(vlan_nativa) # verifica se o nome do port channel já existe no equipamento interfaces = str(interfaces).split('-') @@ -405,7 +405,7 @@ def handle_put(self, request, user, *args, **kwargs): interface = Interface() amb = Ambiente() - api_interface_facade.verificar_vlan_nativa(vlan_nativa) + api_interface_facade.verify_native_vlan(vlan_nativa) # verifica se o nome do port channel já existe no equipamento channel = port_channel.get_by_pk(int(id_channel)) @@ -487,10 +487,10 @@ def handle_put(self, request, user, *args, **kwargs): return self.response(dumps_networkapi({'port_channel': port_channel_map})) - except InvalidValueError, e: - return self.response_error(269, e.param, e.value) - except XMLError, x: - self.log.error(u'Erro ao ler o XML da requisição.') - return self.response_error(3, x) - except InterfaceError, e: - return self.response_error(406, e) + except InvalidValueError as invalid_value_error: + return self.response_error(269, invalid_value_error.param, invalid_value_error.value) + except XMLError as xml_error: + self.log.error('Error reading the request XML.') + return self.response_error(3, xml_error) + except InterfaceError as interface_error: + return self.response_error(406, interface_error)