ztfy/utils/schema.py
branchZTK-1.1
changeset 140 d5e916191cf4
parent 135 864a0cee13c0
child 145 1c7e8bef0027
--- a/ztfy/utils/schema.py	Mon Apr 02 09:08:09 2012 +0200
+++ b/ztfy/utils/schema.py	Fri Apr 06 19:09:59 2012 +0200
@@ -15,16 +15,22 @@
 
 
 # import standard packages
+import string
 
 # import Zope3 interfaces
+from zope.schema.interfaces import ITextLine
 
 # import local interfaces
 
 # import Zope3 packages
+from zope.interface import implements
 from zope.schema import TextLine
+from zope.schema._bootstrapfields import InvalidValue
 
 # import local packages
 
+from ztfy.utils import _
+
 
 class StringLine(TextLine):
     """String line field"""
@@ -33,3 +39,24 @@
 
     def fromUnicode(self, value):
         return str(value)
+
+
+class IColorField(ITextLine):
+    """Marker interface for color fields"""
+
+
+class ColorField(TextLine):
+    """Color field"""
+
+    implements(IColorField)
+
+    def __init__(self, *args, **kw):
+        super(ColorField, self).__init__(max_length=6, *args, **kw)
+
+    def _validate(self, value):
+        if len(value) not in (3, 6):
+            raise InvalidValue, _("Color length must be 3 or 6 characters")
+        for v in value:
+            if v not in string.hexdigits:
+                raise InvalidValue, _("Color value must contain only valid color codes (numbers or letters between 'A' end 'F')")
+        super(ColorField, self)._validate(value)