1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #2444: Improve validation of interface MAC addresses

This commit is contained in:
Jeremy Stretch
2018-09-18 12:02:59 -04:00
parent 9df33cef8b
commit 6cdff955dc
5 changed files with 5 additions and 43 deletions

View File

@ -1,13 +1,11 @@
from __future__ import unicode_literals
from netaddr import EUI, mac_unix_expanded
from netaddr import AddrFormatError, EUI, mac_unix_expanded
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from .formfields import MACAddressFormField
class ASNField(models.BigIntegerField):
description = "32-bit ASN field"
@ -35,7 +33,7 @@ class MACAddressField(models.Field):
return value
try:
return EUI(value, version=48, dialect=mac_unix_expanded_uppercase)
except ValueError as e:
except AddrFormatError as e:
raise ValidationError(e)
def db_type(self, connection):
@ -45,11 +43,3 @@ class MACAddressField(models.Field):
if not value:
return None
return str(self.to_python(value))
def form_class(self):
return MACAddressFormField
def formfield(self, **kwargs):
defaults = {'form_class': self.form_class()}
defaults.update(kwargs)
return super(MACAddressField, self).formfield(**defaults)