Added form help viewlet
authorThierry Florac <thierry.florac@onf.fr>
Thu, 08 Oct 2015 09:17:13 +0200
changeset 29 9ffccb764e97
parent 28 893e485046bc
child 30 0adccc351f22
Added form help viewlet
src/pyams_form/help.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_form/help.py	Thu Oct 08 09:17:13 2015 +0200
@@ -0,0 +1,69 @@
+#
+# 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 interfaces
+from pyams_form.interfaces.form import IFormHelp
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces.text import IHTMLRenderer
+
+# import packages
+from pyams_template.template import template_config
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.text import text_to_html
+from pyams_viewlet.viewlet import contentprovider_config
+from zope.interface import implementer, Interface
+from zope.schema.fieldproperty import FieldProperty
+
+
+@contentprovider_config(name='form_help', view=Interface, layer=IPyAMSLayer)
+@template_config(template='templates/help.pt', layer=IPyAMSLayer)
+class HelpContentProvider(object):
+    """Form help provider"""
+
+    help = None
+
+    def update(self):
+        registry = self.request.registry
+        help = self.help = registry.queryMultiAdapter((self.context, self.request, self.view), IFormHelp)
+        if help is not None:
+            if help.permission and not self.request.has_permission(help.permission, context=self.context):
+                self.help = None
+            elif help.mode and (self.view.mode != help.mode):
+                self.help = None
+
+
+@implementer(IFormHelp)
+class FormHelp(ContextRequestViewAdapter):
+    """Form help"""
+
+    permission = FieldProperty(IFormHelp['permission'])
+    mode = FieldProperty(IFormHelp['mode'])
+
+    outer_margin = FieldProperty(IFormHelp['outer_margin'])
+    status = FieldProperty(IFormHelp['status'])
+    header = FieldProperty(IFormHelp['header'])
+    message = FieldProperty(IFormHelp['message'])
+    message_format = FieldProperty(IFormHelp['message_format'])
+
+
+@adapter_config(context=(IFormHelp, IPyAMSLayer, Interface), provides=IHTMLRenderer)
+class HelpRenderer(ContextRequestViewAdapter):
+    """Help renderer"""
+
+    def render(self, **kwargs):
+        message = self.request.localizer.translate(self.context.message)
+        return text_to_html(message, self.context.message_format)