src/pyams_utils/schema.py
changeset 1 3f89629b9e54
child 32 c784991b55a4
equal deleted inserted replaced
0:16d47bd81d84 1:3f89629b9e54
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 import string
       
    18 
       
    19 # import interfaces
       
    20 from zope.schema.interfaces import ITextLine, IDecimal, IList, ITuple, IPassword
       
    21 
       
    22 # import Zope3 packages
       
    23 from zope.interface import implementer
       
    24 from zope.schema import TextLine, Decimal, List, Tuple, Password, ValidationError
       
    25 
       
    26 # import local packages
       
    27 
       
    28 from pyams_utils import _
       
    29 
       
    30 
       
    31 #
       
    32 # Encoded password field
       
    33 #
       
    34 
       
    35 class IEncodedPassword(IPassword):
       
    36     """Encoded password field interface"""
       
    37 
       
    38 
       
    39 @implementer(IEncodedPassword)
       
    40 class EncodedPassword(Password):
       
    41     """Encoded password field"""
       
    42 
       
    43     _type = None
       
    44 
       
    45     def fromUnicode(self, str):
       
    46         return str
       
    47 
       
    48     def constraint(self, value):
       
    49         return True
       
    50 
       
    51 
       
    52 #
       
    53 # Color field
       
    54 #
       
    55 
       
    56 class IColorField(ITextLine):
       
    57     """Marker interface for color fields"""
       
    58 
       
    59 
       
    60 @implementer(IColorField)
       
    61 class ColorField(TextLine):
       
    62     """Color field"""
       
    63 
       
    64     def __init__(self, *args, **kw):
       
    65         super(ColorField, self).__init__(max_length=6, *args, **kw)
       
    66 
       
    67     def _validate(self, value):
       
    68         if len(value) not in (3, 6):
       
    69             raise ValidationError(_("Color length must be 3 or 6 characters"))
       
    70         for v in value:
       
    71             if v not in string.hexdigits:
       
    72                 raise ValidationError(_("Color value must contain only valid hexadecimal color codes (numbers or "
       
    73                                         "letters between 'A' end 'F')"))
       
    74         super(ColorField, self)._validate(value)
       
    75 
       
    76 
       
    77 #
       
    78 # Pointed decimal field
       
    79 #
       
    80 
       
    81 class IDottedDecimalField(IDecimal):
       
    82     """Marker interface for dotted decimal fields"""
       
    83 
       
    84 
       
    85 @implementer(IDottedDecimalField)
       
    86 class DottedDecimalField(Decimal):
       
    87     """Dotted decimal field"""
       
    88 
       
    89 
       
    90 #
       
    91 # Dates range field
       
    92 #
       
    93 
       
    94 class IDatesRangeField(ITuple):
       
    95     """Marker interface for dates range fields"""
       
    96 
       
    97 
       
    98 @implementer(IDatesRangeField)
       
    99 class DatesRangeField(Tuple):
       
   100     """Dates range field"""
       
   101 
       
   102     def __init__(self, value_type=None, unique=False, **kw):
       
   103         super(DatesRangeField, self).__init__(value_type=None, unique=False,
       
   104                                               min_length=2, max_length=2, **kw)
       
   105 
       
   106 
       
   107 #
       
   108 # TextLine list field
       
   109 #
       
   110 
       
   111 class ITextLineListField(IList):
       
   112     """Marker interface for textline list field"""
       
   113 
       
   114 
       
   115 @implementer(ITextLineListField)
       
   116 class TextLineListField(List):
       
   117     """TextLine list field"""
       
   118 
       
   119     def __init__(self, value_type=None, unique=False, **kw):
       
   120         super(TextLineListField, self).__init__(value_type=TextLine(), unique=True, **kw)