Make changes to validation to account for M2M not being available under model in addition to not being able to check incoming vlans under same model.

This commit is contained in:
Daniel Sheppard 2024-08-26 23:57:55 -05:00
parent 4f830ce3cc
commit d159547758
3 changed files with 43 additions and 6 deletions

View File

@ -238,6 +238,27 @@ class InterfaceSerializer(NetBoxModelSerializer, CabledObjectSerializer, Connect
def validate(self, data):
if not self.nested:
# Validate 802.1q mode and vlan(s)
mode = None
tagged_vlans = []
if self.instance.pk and 'mode' in data.keys():
mode = data.get('mode') if 'mode' in self.data.keys() else self.instance.get('mode')
elif 'mode' in data.keys():
mode = data.get('mode')
if self.instance.pk and 'tagged_vlans' in data.keys():
tagged_vlans = data.get('tagged_vlans') if 'tagged_vlans' in data.keys() else \
self.instance.tagged_vlans.all()
elif 'tagged_vlans' in data.keys():
tagged_vlans = data.get('tagged_vlans')
if mode != InterfaceModeChoices.MODE_TAGGED and tagged_vlans:
raise serializers.ValidationError({
'tagged_vlans': "Interface mode does not support including tagged vlans"
})
# Validate many-to-many VLAN assignments
device = self.instance.device if self.instance else data.get('device')
for vlan in data.get('tagged_vlans', []):

View File

@ -1363,6 +1363,26 @@ class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
'mode': '802.1Q Mode',
}
def clean(self):
mode = None
tagged_vlans = []
if self.instance.pk and 'mode' in self.cleaned_data.keys():
mode = self.cleaned_data.get('mode') if 'mode' in self.cleaned_data.keys() else self.instance.get('mode')
elif 'mode' in self.cleaned_data.keys():
mode = self.cleaned_data.get('mode')
if self.instance.pk and 'tagged_vlans' in self.cleaned_data.keys():
tagged_vlans = self.cleaned_data.get('tagged_vlans') if 'tagged_vlans' in self.cleaned_data.keys() else\
self.instance.tagged_vlans.all()
elif 'tagged_vlans' in self.cleaned_data.keys():
tagged_vlans = self.cleaned_data.get('tagged_vlans')
if mode != InterfaceModeChoices.MODE_TAGGED and tagged_vlans:
raise forms.ValidationError({
'tagged_vlans': "Interface mode does not support including tagged vlans"
})
class FrontPortForm(ModularDeviceComponentForm):
rear_port = DynamicModelChoiceField(

View File

@ -894,6 +894,8 @@ class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEnd
raise ValidationError({'rf_channel_width': _("Cannot specify custom width with channel selected.")})
# VLAN validation
if self.mode is None and self.untagged_vlan:
raise ValidationError({'untagged_vlan': _("Interface mode does not support including an untagged vlan.")})
# Validate untagged VLAN
if self.untagged_vlan and self.untagged_vlan.site not in [self.device.site, None]:
@ -904,12 +906,6 @@ class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEnd
).format(untagged_vlan=self.untagged_vlan)
})
# Validate that tagged-all payload does not include tagged_vlans
if self.mode != InterfaceModeChoices.MODE_TAGGED and self.tagged_vlans:
raise ValidationError({
'tagged_vlans': "Interface mode does not support including tagged vlans"
})
def save(self, *args, **kwargs):
# Set absolute channel attributes from selected options