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

Closes #1337: Add WWN field to interfaces

This commit is contained in:
jeremystretch
2021-10-07 15:09:42 -04:00
parent 6463fd902c
commit 18c3bb673f
16 changed files with 107 additions and 19 deletions

View File

@ -2,11 +2,30 @@ from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from netaddr import AddrFormatError, EUI, mac_unix_expanded
from netaddr import AddrFormatError, EUI, eui64_unix_expanded, mac_unix_expanded
from ipam.constants import BGP_ASN_MAX, BGP_ASN_MIN
from .lookups import PathContains
__all__ = (
'ASNField',
'MACAddressField',
'PathField',
'WWNField',
)
class mac_unix_expanded_uppercase(mac_unix_expanded):
word_fmt = '%.2X'
class eui64_unix_expanded_uppercase(eui64_unix_expanded):
word_fmt = '%.2X'
#
# Fields
#
class ASNField(models.BigIntegerField):
description = "32-bit ASN field"
@ -24,10 +43,6 @@ class ASNField(models.BigIntegerField):
return super().formfield(**defaults)
class mac_unix_expanded_uppercase(mac_unix_expanded):
word_fmt = '%.2X'
class MACAddressField(models.Field):
description = "PostgreSQL MAC Address field"
@ -42,8 +57,8 @@ class MACAddressField(models.Field):
return value
try:
return EUI(value, version=48, dialect=mac_unix_expanded_uppercase)
except AddrFormatError as e:
raise ValidationError("Invalid MAC address format: {}".format(value))
except AddrFormatError:
raise ValidationError(f"Invalid MAC address format: {value}")
def db_type(self, connection):
return 'macaddr'
@ -54,6 +69,32 @@ class MACAddressField(models.Field):
return str(self.to_python(value))
class WWNField(models.Field):
description = "World Wide Name field"
def python_type(self):
return EUI
def from_db_value(self, value, expression, connection):
return self.to_python(value)
def to_python(self, value):
if value is None:
return value
try:
return EUI(value, version=64, dialect=eui64_unix_expanded_uppercase)
except AddrFormatError:
raise ValidationError(f"Invalid WWN format: {value}")
def db_type(self, connection):
return 'macaddr8'
def get_prep_value(self, value):
if not value:
return None
return str(self.to_python(value))
class PathField(ArrayField):
"""
An ArrayField which holds a set of objects, each identified by a (type, ID) tuple.