Use custom user data converters for date and decimal form fields
authorThierry Florac <tflorac@ulthar.net>
Mon, 07 Oct 2019 13:45:40 +0200
changeset 477 bf471dbff9b0
parent 476 43aeb0631fd0
child 478 77e33a53d9d1
Use custom user data converters for date and decimal form fields
src/pyams_default_theme/shared/form/widget.py
--- a/src/pyams_default_theme/shared/form/widget.py	Mon Oct 07 13:44:25 2019 +0200
+++ b/src/pyams_default_theme/shared/form/widget.py	Mon Oct 07 13:45:40 2019 +0200
@@ -13,11 +13,12 @@
 from z3c.form.browser.checkbox import SingleCheckBoxWidget
 from z3c.form.interfaces import IFieldWidget
 from z3c.form.widget import FieldWidget
-from zope.schema.interfaces import IBool, IList
+from zope.schema.interfaces import IBool, IDate, IDecimal, IList
 
 from pyams_content.shared.form.interfaces import IFormFieldDataConverter
 from pyams_skin.layer import IPyAMSUserLayer
-from pyams_utils.adapter import adapter_config, ContextRequestAdapter
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config
+from pyams_utils.date import SH_DATE_FORMAT, format_date
 
 
 __docformat__ = 'restructuredtext'
@@ -38,6 +39,22 @@
         return self.request.localizer.translate(_("yes") if value else _("no"))
 
 
+@adapter_config(context=(IDate, IPyAMSUserLayer), provides=IFormFieldDataConverter)
+class DateFieldDataConverter(ContextRequestAdapter):
+    """Date field user data converter"""
+
+    def convert(self, value):
+        return format_date(value, SH_DATE_FORMAT)
+
+
+@adapter_config(context=(IDecimal, IPyAMSUserLayer), provides=IFormFieldDataConverter)
+class DecimalFieldDataConverter(ContextRequestAdapter):
+    """Decimal field user data converter"""
+
+    def convert(self, value):
+        return str(value)
+
+
 @adapter_config(context=(IList, IPyAMSUserLayer), provides=IFormFieldDataConverter)
 class ListFieldDataConverter(ContextRequestAdapter):
     """List field user data converter"""