Commits

Tim Savage  committed 0e05b87

Added flag to support alpha blended values. Updated form, field, tests, docs

  • Participants
  • Parent commits f418905

Comments (0)

Files changed (6)

File django_extras/core/validators.py

 color_re = re.compile(
     r'(^#[a-f0-9]{3,6}$)' # Hash style
     r'|(^rgb\s*\(\s*((2[0-4][0-9]|25[0-5]|1?[0-9]{1,2}|100%|[0-9]{1,2}%)\s*,\s*){2}((2[0-4][0-9]|25[0-5]|1?[0-9]{1,2}|100%|[0-9]{1,2}%)\s*)\))' # rgb style
+    r'|(^hsl\s*\(\s*(360|3[0-5][0-9]|[0-2]?[0-9]{1,2})\s*,\s*(100%|[0-9]{1,2}%)\s*,\s*(100%|[0-9]{1,2}%)\s*\)$)', re.IGNORECASE) # hsl style
+validate_color = RegexValidator(color_re, _(u'Enter a valid color in CSS format.'), 'invalid')
+
+
+alpha_color_re = re.compile(
+    r'(^#[a-f0-9]{3,6}$)' # Hash style
+    r'|(^rgb\s*\(\s*((2[0-4][0-9]|25[0-5]|1?[0-9]{1,2}|100%|[0-9]{1,2}%)\s*,\s*){2}((2[0-4][0-9]|25[0-5]|1?[0-9]{1,2}|100%|[0-9]{1,2}%)\s*)\))' # rgb style
     r'|(^rgba\s*\(\s*((2[0-4][0-9]|25[0-5]|1?[0-9]{1,2}|100%|[0-9]{1,2}%)\s*,\s*){3}(((0?(\.[0-9]+)?)|1)\s*)\)$)' # rgba style
     r'|(^hsl\s*\(\s*(360|3[0-5][0-9]|[0-2]?[0-9]{1,2})\s*,\s*(100%|[0-9]{1,2}%)\s*,\s*(100%|[0-9]{1,2}%)\s*\)$)' # hsl style
     r'|(^hsla\s*\(\s*(360|3[0-5][0-9]|[0-2]?[0-9]{1,2})\s*,\s*((100%|[0-9]{1,2}%)\s*,\s*){2}(((0?(\.[0-9]+)?)|1)\s*)\)$)', re.IGNORECASE) # hsla style
-validate_color = RegexValidator(color_re, _(u'Enter a valid color in CSS format.'), 'invalid')
+validate_alpha_color = RegexValidator(alpha_color_re, _(u'Enter a valid color in CSS format.'), 'invalid')
 
 
 class JsonValidator(object):

File django_extras/db/models/fields.py

     """
     Database field that represents a color value.
     """
-    default_validators = [validators.validate_color]
     description = _("Color value")
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, verbose_name=None, name=None, allow_alpha=False, **kwargs):
         kwargs.setdefault('max_length', 40)
-        super(ColorField, self).__init__(*args, **kwargs)
+        super(ColorField, self).__init__(verbose_name, name, **kwargs)
+
+        self.allow_alpha = allow_alpha
+        if allow_alpha:
+            self.validators.append(validators.validate_alpha_color)
+        else:
+            self.validators.append(validators.validate_color)
 
     def formfield(self, **kwargs):
-        # As with CharField, this will cause color validation to be performed
-        # twice.
-        defaults = {'widget': forms.ColorPickerWidget}
+        defaults = {'form_class': forms.ColorField}
         defaults.update(kwargs)
+        if defaults['form_class'] is forms.ColorField:
+            defaults.setdefault('allow_alpha', self.allow_alpha)
         return super(ColorField, self).formfield(**defaults)
 
 
         return super(JsonField, self).formfield(**defaults)
 
 
+# Notice: This has been removed for the time being as there are not suitable
+#
+# Register special admin widgets if admin is in use.
+#if 'django.contrib.admin' in settings.INSTALLED_APPS:
+#    from django.contrib.admin import options
+#    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[ColorField] = { 'widget':  }
+
+# Register fields with south.
 try:
     from south.modelsinspector import add_introspection_rules
     add_introspection_rules([], ["^django_extras\.db\.models\.fields\.ColorField"])
     add_introspection_rules([], ["^django_extras\.db\.models\.fields\.JsonField"])
 except ImportError:
     pass
+

File django_extras/forms/fields.py

-from django.utils import simplejson
 from django.forms.fields import *
-from django.forms.widgets import Textarea
+from django.forms import widgets
 from django_extras.core import validators
 
 
+class ColorField(CharField):
+    """
+    Form field that provides input for color picker
+    """
+    default_validators = [ validators.validate_color ]
+
+    def __init__(self, allow_alpha=False, *args, **kwargs):
+        super(ColorField, self).__init__(*args, **kwargs)
+        self.allow_alpha = allow_alpha
+        if allow_alpha:
+            self.validators.append(validators.validate_alpha_color)
+        else:
+            self.validators.append(validators.validate_color)
+
+
 class JsonField(Field):
     """
     Form field that validates that valid JSON is supplied.
     """
-    widget = Textarea
+    widget = widgets.Textarea
     default_validators = [ validators.validate_json ]

File django_extras/tests/core/validators.py

         validators.validate_color('rgb(191,234,156)')
         validators.validate_color('rgb(255,255,255)')
 
-    def test_rgba(self):
-        validators.validate_color('rgba(0, 1, 2, 0)')
-        validators.validate_color('rgba(34, 56, 78, 0.1)')
-        validators.validate_color('rgba(191, 234, 156, .2)')
-        validators.validate_color('rgba(255, 255, 255, 1)')
-
     def test_hsl(self):
         validators.validate_color('hsl(1,2%,3%)')
         validators.validate_color('hsl(23,45%,67%)')
         validators.validate_color('hsl(345,89%,90%)')
         validators.validate_color('hsl(360,100%,100%)')
 
+
+class AlphaColorValidatorTestCase(test.TestCase):
+    def test_hash(self):
+        validators.validate_alpha_color('#123')
+        validators.validate_alpha_color('#abc')
+        validators.validate_alpha_color('#45d')
+        validators.validate_alpha_color('#6789ef')
+        validators.validate_alpha_color('#abcdef')
+        validators.validate_alpha_color('#0a1b2c')
+
+    def test_rgb(self):
+        validators.validate_alpha_color('rgb(0,1,2)')
+        validators.validate_alpha_color('rgb(34,56,78)')
+        validators.validate_alpha_color('rgb(191,234,156)')
+        validators.validate_alpha_color('rgb(255,255,255)')
+
+    def test_rgba(self):
+        validators.validate_alpha_color('rgba(0, 1, 2, 0)')
+        validators.validate_alpha_color('rgba(34, 56, 78, 0.1)')
+        validators.validate_alpha_color('rgba(191, 234, 156, .2)')
+        validators.validate_alpha_color('rgba(255, 255, 255, 1)')
+
+    def test_hsl(self):
+        validators.validate_alpha_color('hsl(1,2%,3%)')
+        validators.validate_alpha_color('hsl(23,45%,67%)')
+        validators.validate_alpha_color('hsl(345,89%,90%)')
+        validators.validate_alpha_color('hsl(360,100%,100%)')
+
     def test_hsla(self):
-        validators.validate_color('hsla(1,2%,3%,0)')
-        validators.validate_color('hsla(23,45%,67%,0.1)')
-        validators.validate_color('hsla(345,89%,90%,.2)')
-        validators.validate_color('hsla(360,100%,100%,1)')
-
-
+        validators.validate_alpha_color('hsla(1,2%,3%,0)')
+        validators.validate_alpha_color('hsla(23,45%,67%,0.1)')
+        validators.validate_alpha_color('hsla(345,89%,90%,.2)')
+        validators.validate_alpha_color('hsla(360,100%,100%,1)')
 
 
 class JsonValidatorTestCase(test.TestCase):

File docs/ref/models/fields.txt

 ``ColorField``
 --------------
 
-.. class:: ColorField([max_length=40, **options])
+.. class:: ColorField([allow_alpha=False, max_length=40, **options])
 
 A :class:`CharField` that checks that the value is a valid CSS color value.
+``allow_alpha`` controls if colors can support alpha values.
 
 ``MoneyField``
 --------------

File docs/ref/validators.txt

     A :class:`RegexValidator` instance that ensures a value looks like a CSS
     color value.
 
+``validate_alpha_color``
+------------------
+.. data:: validate_alpha_color
+
+    A :class:`RegexValidator` instance that ensures a value looks like a CSS
+    color value. Supports color definitions with alpha blending.
+
 ``validate_json``
 ------------------
 .. data:: validate_json