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

Remove external macaddress package dependency

This commit is contained in:
Nick Peelman
2016-07-05 10:41:38 -04:00
parent 9da4c28cd5
commit a6d41c95b8
5 changed files with 74 additions and 6 deletions

43
netbox/dcim/fields.py Normal file
View File

@ -0,0 +1,43 @@
from netaddr import EUI, mac_unix_expanded
from django.core.exceptions import ValidationError
from django.db import models
from .formfields import MACAddressFormField
class mac_unix_expanded_uppercase(mac_unix_expanded):
word_fmt = '%.2X'
class MACAddressField(models.Field):
description = "PostgreSQL MAC Address field"
def python_type(self):
return EUI
def from_db_value(self, value, expression, connection, context):
return self.to_python(value)
def to_python(self, value):
if not value:
return value
try:
return EUI(value, dialect=mac_unix_expanded_uppercase)
except ValueError as e:
raise ValidationError(e)
def db_type(self, connection):
return 'macaddr'
def get_prep_value(self, value):
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)

26
netbox/dcim/formfields.py Normal file
View File

@ -0,0 +1,26 @@
from netaddr import EUI, AddrFormatError
from django import forms
from django.core.exceptions import ValidationError
#
# Form fields
#
class MACAddressFormField(forms.Field):
default_error_messages = {
'invalid': "Enter a valid MAC address.",
}
def to_python(self, value):
if not value:
return None
if isinstance(value, EUI):
return value
try:
return EUI(value)
except AddrFormatError:
raise ValidationError("Please specify a valid MAC address.")

View File

@ -1,9 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-01 13:53 # Generated by Django 1.9.7 on 2016-07-05 10:01
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations from django.db import migrations, models
import macaddress.fields from ..fields import MACAddressField
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -16,6 +16,6 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='interface', model_name='interface',
name='mac_address', name='mac_address',
field=macaddress.fields.MACAddressField(blank=True, integer=True, null=True), field=MACAddressField(blank=True, null=True, verbose_name=b'MAC Address'),
), ),
] ]

View File

@ -10,7 +10,7 @@ from extras.rpc import RPC_CLIENTS
from utilities.fields import NullableCharField from utilities.fields import NullableCharField
from utilities.models import CreatedUpdatedModel from utilities.models import CreatedUpdatedModel
from macaddress.fields import MACAddressField from .fields import MACAddressField
RACK_FACE_FRONT = 0 RACK_FACE_FRONT = 0
RACK_FACE_REAR = 1 RACK_FACE_REAR = 1

View File

@ -5,7 +5,6 @@ django-filter==0.13.0
django-rest-swagger==0.3.7 django-rest-swagger==0.3.7
django-tables2==1.2.1 django-tables2==1.2.1
djangorestframework==3.3.3 djangorestframework==3.3.3
django-macaddress==1.3.2
graphviz==0.4.10 graphviz==0.4.10
Markdown==2.6.6 Markdown==2.6.6
ncclient==0.4.7 ncclient==0.4.7