ztfy/utils/schema.py
branchZTK-1.1
changeset 140 d5e916191cf4
parent 135 864a0cee13c0
child 145 1c7e8bef0027
equal deleted inserted replaced
139:6478aac3b2bd 140:d5e916191cf4
    13 #
    13 #
    14 ##############################################################################
    14 ##############################################################################
    15 
    15 
    16 
    16 
    17 # import standard packages
    17 # import standard packages
       
    18 import string
    18 
    19 
    19 # import Zope3 interfaces
    20 # import Zope3 interfaces
       
    21 from zope.schema.interfaces import ITextLine
    20 
    22 
    21 # import local interfaces
    23 # import local interfaces
    22 
    24 
    23 # import Zope3 packages
    25 # import Zope3 packages
       
    26 from zope.interface import implements
    24 from zope.schema import TextLine
    27 from zope.schema import TextLine
       
    28 from zope.schema._bootstrapfields import InvalidValue
    25 
    29 
    26 # import local packages
    30 # import local packages
       
    31 
       
    32 from ztfy.utils import _
    27 
    33 
    28 
    34 
    29 class StringLine(TextLine):
    35 class StringLine(TextLine):
    30     """String line field"""
    36     """String line field"""
    31 
    37 
    32     _type = str
    38     _type = str
    33 
    39 
    34     def fromUnicode(self, value):
    40     def fromUnicode(self, value):
    35         return str(value)
    41         return str(value)
       
    42 
       
    43 
       
    44 class IColorField(ITextLine):
       
    45     """Marker interface for color fields"""
       
    46 
       
    47 
       
    48 class ColorField(TextLine):
       
    49     """Color field"""
       
    50 
       
    51     implements(IColorField)
       
    52 
       
    53     def __init__(self, *args, **kw):
       
    54         super(ColorField, self).__init__(max_length=6, *args, **kw)
       
    55 
       
    56     def _validate(self, value):
       
    57         if len(value) not in (3, 6):
       
    58             raise InvalidValue, _("Color length must be 3 or 6 characters")
       
    59         for v in value:
       
    60             if v not in string.hexdigits:
       
    61                 raise InvalidValue, _("Color value must contain only valid color codes (numbers or letters between 'A' end 'F')")
       
    62         super(ColorField, self)._validate(value)