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

35 lines
963 B
Python

from __future__ import unicode_literals
from django.core.validators import RegexValidator
from django.db import models
from .forms import ColorSelect
validate_color = RegexValidator('^[0-9a-f]{6}$', 'Enter a valid hexadecimal RGB color code.', 'invalid')
class NullableCharField(models.CharField):
description = "Stores empty values as NULL rather than ''"
def to_python(self, value):
if isinstance(value, models.CharField):
return value
return value or ''
def get_prep_value(self, value):
return value or None
class ColorField(models.CharField):
default_validators = [validate_color]
description = "A hexadecimal RGB color code"
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 6
super(ColorField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
kwargs['widget'] = ColorSelect
return super(ColorField, self).formfield(**kwargs)