Added JSON mapping schema field
authorThierry Florac <tflorac@ulthar.net>
Tue, 17 Sep 2019 11:48:08 +0200
changeset 394 62a57f51fc14
parent 393 225e6576d28d
child 395 5cb941e31c86
Added JSON mapping schema field
src/pyams_utils/schema.py
--- a/src/pyams_utils/schema.py	Tue Sep 17 11:47:20 2019 +0200
+++ b/src/pyams_utils/schema.py	Tue Sep 17 11:48:08 2019 +0200
@@ -10,23 +10,17 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
 import re
 import string
 
-# import interfaces
-from zope.schema.interfaces import IText, ITextLine, IDecimal, IList, IDict, ITuple, IPassword
-
-# import Zope3 packages
 from persistent.list import PersistentList as PersistentListType
 from persistent.mapping import PersistentMapping
-from zope.interface import implementer
-from zope.schema import Text, TextLine, Decimal, List, Dict, Tuple, Password, ValidationError
+from zope.interface import implementer, Interface
+from zope.schema import Decimal, Dict, List, Password, Text, TextLine, Tuple, ValidationError
+from zope.schema.interfaces import IDecimal, IDict, IList, IPassword, IText, ITextLine, ITuple
 
-# import local packages
+
+__docformat__ = 'restructuredtext'
 
 from pyams_utils import _
 
@@ -96,6 +90,34 @@
 
 
 #
+# JSON dict value field
+#
+
+class IJSONDictField(IDict):
+    """JSON dict value field interface"""
+
+
+class IJSONDictFieldsGetter(Interface):
+    """Adapter interface used to get JSON value fields list"""
+
+    def get_fields(self, data):
+        """Returns an iterator made of tuples
+
+        Each tuple may ocntain field name, field label and field value
+        """
+
+
+@implementer(IJSONDictField)
+class JSONDictField(Dict):
+    """JSON dict value field"""
+
+    def __init__(self, key_type=None, value_type=None, **kw):
+        super(JSONDictField, self).__init__(key_type=TextLine(),
+                                            value_type=TextLine(),
+                                            **kw)
+
+
+#
 # Color field
 #
 
@@ -115,8 +137,8 @@
             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')"))
+                raise ValidationError(_("Color value must contain only valid hexadecimal color "
+                                        "codes (numbers or letters between 'A' end 'F')"))
         super(ColorField, self)._validate(value)
 
 
@@ -178,7 +200,8 @@
 
 
 class InvalidEmail(ValidationError):
-    __doc__ = _("Email address must be entered as « name@domain.name », without '<' and '>' characters")
+    __doc__ = _(
+        "Email address must be entered as « name@domain.name », without '<' and '>' characters")
 
 
 @implementer(IMailAddressField)