Fixes #672: Expanded color selection for rack and device roles

This commit is contained in:
Jeremy Stretch
2016-12-06 12:28:29 -05:00
parent 300ee820fa
commit eb4cd0e723
7 changed files with 139 additions and 51 deletions

View File

@@ -1,5 +1,11 @@
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 ''"
@@ -11,3 +17,16 @@ class NullableCharField(models.CharField):
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)