Skip to content

Commit

Permalink
Fixes netbox-community#444: Corrected prefix model validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Aug 9, 2016
1 parent 6a21fd8 commit ab889c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
8 changes: 2 additions & 6 deletions netbox/ipam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,14 @@ def __init__(self, *args, **kwargs):
self.fields['vlan'].choices = []

def clean_prefix(self):
data = self.cleaned_data['prefix']
try:
prefix = IPNetwork(data)
except:
raise
prefix = self.cleaned_data['prefix']
if prefix.version == 4 and prefix.prefixlen == 32:
raise forms.ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 "
"addresses instead.")
elif prefix.version == 6 and prefix.prefixlen == 128:
raise forms.ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 "
"addresses instead.")
return data
return prefix


class PrefixFromCSVForm(forms.ModelForm):
Expand Down
13 changes: 7 additions & 6 deletions netbox/ipam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,13 @@ def get_absolute_url(self):

def clean(self):
# Disallow host masks
if self.prefix.version == 4 and self.prefix.prefixlen == 32:
raise ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 addresses "
"instead.")
elif self.prefix.version == 6 and self.prefix.prefixlen == 128:
raise ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 addresses "
"instead.")
if self.prefix:
if self.prefix.version == 4 and self.prefix.prefixlen == 32:
raise ValidationError("Cannot create host addresses (/32) as prefixes. These should be IPv4 addresses "
"instead.")
elif self.prefix.version == 6 and self.prefix.prefixlen == 128:
raise ValidationError("Cannot create host addresses (/128) as prefixes. These should be IPv6 addresses "
"instead.")

def save(self, *args, **kwargs):
if self.prefix:
Expand Down

0 comments on commit ab889c7

Please sign in to comment.