src/pyams_utils/schema.py
changeset 1 3f89629b9e54
child 32 c784991b55a4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/schema.py	Thu Feb 19 00:46:48 2015 +0100
@@ -0,0 +1,120 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import string
+
+# import interfaces
+from zope.schema.interfaces import ITextLine, IDecimal, IList, ITuple, IPassword
+
+# import Zope3 packages
+from zope.interface import implementer
+from zope.schema import TextLine, Decimal, List, Tuple, Password, ValidationError
+
+# import local packages
+
+from pyams_utils import _
+
+
+#
+# Encoded password field
+#
+
+class IEncodedPassword(IPassword):
+    """Encoded password field interface"""
+
+
+@implementer(IEncodedPassword)
+class EncodedPassword(Password):
+    """Encoded password field"""
+
+    _type = None
+
+    def fromUnicode(self, str):
+        return str
+
+    def constraint(self, value):
+        return True
+
+
+#
+# Color field
+#
+
+class IColorField(ITextLine):
+    """Marker interface for color fields"""
+
+
+@implementer(IColorField)
+class ColorField(TextLine):
+    """Color field"""
+
+    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 ValidationError(_("Color length must be 3 or 6 characters"))
+        for v in value:
+            if v not in string.hexdigits:
+                raise ValidationError(_("Color value must contain only valid hexadecimal color codes (numbers or "
+                                        "letters between 'A' end 'F')"))
+        super(ColorField, self)._validate(value)
+
+
+#
+# Pointed decimal field
+#
+
+class IDottedDecimalField(IDecimal):
+    """Marker interface for dotted decimal fields"""
+
+
+@implementer(IDottedDecimalField)
+class DottedDecimalField(Decimal):
+    """Dotted decimal field"""
+
+
+#
+# Dates range field
+#
+
+class IDatesRangeField(ITuple):
+    """Marker interface for dates range fields"""
+
+
+@implementer(IDatesRangeField)
+class DatesRangeField(Tuple):
+    """Dates range field"""
+
+    def __init__(self, value_type=None, unique=False, **kw):
+        super(DatesRangeField, self).__init__(value_type=None, unique=False,
+                                              min_length=2, max_length=2, **kw)
+
+
+#
+# TextLine list field
+#
+
+class ITextLineListField(IList):
+    """Marker interface for textline list field"""
+
+
+@implementer(ITextLineListField)
+class TextLineListField(List):
+    """TextLine list field"""
+
+    def __init__(self, value_type=None, unique=False, **kw):
+        super(TextLineListField, self).__init__(value_type=TextLine(), unique=True, **kw)