Skip to content

Commit

Permalink
Enhance vlan number test in order to remove filtered equipments
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Vinicius G. Cesario committed May 12, 2015
1 parent 02f537b commit b61b522
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions networkapi/vlan/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# -*- coding:utf-8 -*-

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import with_statement
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
Expand Down Expand Up @@ -565,6 +565,8 @@ def insert_vlan(self, authenticated_user):
@raise VlanError: Erro ao cadastrar Vlan
"""

from networkapi.equipamento.models import TipoEquipamento

try:
self.get_by_number_and_environment(self.num_vlan, self.ambiente)
ambiente = "%s - %s - %s" % (self.ambiente.divisao_dc.nome,
Expand All @@ -575,24 +577,34 @@ def insert_vlan(self, authenticated_user):
pass

ambiente = self.ambiente
filter = ambiente.filter
equipment_types = TipoEquipamento.objects.filter(filterequiptype__filter=filter)

equips = list()
envs = list()
envs_aux = list()

for env in ambiente.equipamentoambiente_set.all():
#Get all equipments from the environment being tested
#that are not supposed to be filtered
#(not the same type of the equipment type of a filter of the environment)
for env in ambiente.equipamentoambiente_set.all().exclude(equipamento__tipo_equipamento__in=equipment_types):
equips.append(env.equipamento)

#Get all environment that the equipments above are included
for equip in equips:
for env in equip.equipamentoambiente_set.all():
if not env in envs:
if not env.ambiente_id in envs_aux:
envs.append(env.ambiente)
envs_aux.append(env.ambiente_id)

#Check in all vlans from all environments above
#if there is a vlan with the same vlan number of the
#vlan being tested
for env in envs:
for vlan in env.vlan_set.all():
if int(vlan.num_vlan) == int(self.num_vlan):
if self.ambiente.filter_id == None or vlan.ambiente.filter_id == None or int(vlan.ambiente.filter_id) != int(self.ambiente.filter_id):
raise VlanNumberEnvironmentNotAvailableError(
None, "Já existe uma VLAN cadastrada com o número %s com um equipamento compartilhado nesse ambiente" % (self.num_vlan))
raise VlanNumberEnvironmentNotAvailableError(
None, "Já existe uma VLAN cadastrada com o número %s com um equipamento compartilhado nesse ambiente" % (self.num_vlan))

try:
self.get_by_name(self.nome)
Expand Down

0 comments on commit b61b522

Please sign in to comment.