mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fixes #444: Corrected prefix model validation
This commit is contained in:
@ -182,18 +182,14 @@ class PrefixForm(forms.ModelForm, BootstrapMixin):
|
|||||||
self.fields['vlan'].choices = []
|
self.fields['vlan'].choices = []
|
||||||
|
|
||||||
def clean_prefix(self):
|
def clean_prefix(self):
|
||||||
data = self.cleaned_data['prefix']
|
prefix = self.cleaned_data['prefix']
|
||||||
try:
|
|
||||||
prefix = IPNetwork(data)
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
if prefix.version == 4 and prefix.prefixlen == 32:
|
if prefix.version == 4 and prefix.prefixlen == 32:
|
||||||
raise forms.ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 "
|
raise forms.ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 "
|
||||||
"addresses instead.")
|
"addresses instead.")
|
||||||
elif prefix.version == 6 and prefix.prefixlen == 128:
|
elif prefix.version == 6 and prefix.prefixlen == 128:
|
||||||
raise forms.ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 "
|
raise forms.ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 "
|
||||||
"addresses instead.")
|
"addresses instead.")
|
||||||
return data
|
return prefix
|
||||||
|
|
||||||
|
|
||||||
class PrefixFromCSVForm(forms.ModelForm):
|
class PrefixFromCSVForm(forms.ModelForm):
|
||||||
|
@ -254,12 +254,13 @@ class Prefix(CreatedUpdatedModel):
|
|||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
# Disallow host masks
|
# Disallow host masks
|
||||||
if self.prefix.version == 4 and self.prefix.prefixlen == 32:
|
if self.prefix:
|
||||||
raise ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 addresses "
|
if self.prefix.version == 4 and self.prefix.prefixlen == 32:
|
||||||
"instead.")
|
raise ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 addresses "
|
||||||
elif self.prefix.version == 6 and self.prefix.prefixlen == 128:
|
"instead.")
|
||||||
raise ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 addresses "
|
elif self.prefix.version == 6 and self.prefix.prefixlen == 128:
|
||||||
"instead.")
|
raise ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 addresses "
|
||||||
|
"instead.")
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if self.prefix:
|
if self.prefix:
|
||||||
|
Reference in New Issue
Block a user