2016-07-05 10:41:38 -04:00
|
|
|
from netaddr import EUI, mac_unix_expanded
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from .formfields import MACAddressFormField
|
|
|
|
|
2016-07-05 10:48:36 -04:00
|
|
|
|
2016-07-05 10:41:38 -04:00
|
|
|
class mac_unix_expanded_uppercase(mac_unix_expanded):
|
|
|
|
word_fmt = '%.2X'
|
|
|
|
|
2016-07-05 10:58:28 -04:00
|
|
|
|
2016-07-05 10:41:38 -04:00
|
|
|
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):
|
2016-07-06 14:22:34 -04:00
|
|
|
if value is None:
|
2016-07-05 10:41:38 -04:00
|
|
|
return value
|
|
|
|
try:
|
2016-07-06 14:22:34 -04:00
|
|
|
return EUI(value, version=48, dialect=mac_unix_expanded_uppercase)
|
2016-07-05 10:41:38 -04:00
|
|
|
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)
|