src/pyams_content/shared/form/handler.py
changeset 170 26aefef3d0aa
child 558 d9c6b1d7fefa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/form/handler.py	Tue Sep 19 11:11:30 2017 +0200
@@ -0,0 +1,91 @@
+#
+# 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_content.shared.form.interfaces import IFormHandler, IMailtoHandlerTarget, IMailtoHandlerInfo
+from zope.annotation.interfaces import IAnnotations
+
+# import packages
+from persistent import Persistent
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config
+from pyams_utils.request import check_request
+from pyams_utils.vocabulary import vocabulary_config
+from zope.componentvocabulary.vocabulary import UtilityTerm, UtilityVocabulary
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_content import _
+
+
+@vocabulary_config(name='PyAMS form handlers')
+class FormHandlersVocabulary(UtilityVocabulary):
+    """Form handlers vocabulary"""
+
+    interface = IFormHandler
+
+    def __init__(self, context, **kw):
+        request = check_request()
+        registry = request.registry
+        translate = request.localizer.translate
+        utils = [(None, translate(_("No selected handler...")))] + \
+                [(name, translate(util.label))
+                 for (name, util) in registry.getUtilitiesFor(self.interface)]
+        self._terms = dict((title, UtilityTerm(name, title)) for name, title in utils)
+
+    def __iter__(self):
+        return iter(self._terms.values())
+
+
+#
+# Mailto form handler
+#
+
+MAILTO_HANDLER_ANNOTATIONS_KEY = 'pyams_content.form.handler.mailto'
+
+
+@implementer(IMailtoHandlerInfo)
+class MailtoFormHandlerInfo(Persistent):
+    """Mailto form handler persistent info"""
+
+    source_address = FieldProperty(IMailtoHandlerInfo['source_address'])
+    source_name = FieldProperty(IMailtoHandlerInfo['source_name'])
+    target_address = FieldProperty(IMailtoHandlerInfo['target_address'])
+    target_name = FieldProperty(IMailtoHandlerInfo['target_name'])
+
+
+@adapter_config(context=IMailtoHandlerTarget, provides=IMailtoHandlerInfo)
+def mailto_form_handler_factory(context):
+    """Mailto form handler factory"""
+    annotations = IAnnotations(context)
+    info = annotations.get(MAILTO_HANDLER_ANNOTATIONS_KEY)
+    if info is None:
+        info = annotations[MAILTO_HANDLER_ANNOTATIONS_KEY] = MailtoFormHandlerInfo()
+    return info
+
+
+@utility_config(name='mailto', provides=IFormHandler)
+class MailtoFormHandler(object):
+    """Mailto form handler"""
+
+    label = _("Mailto form handler")
+    target_interface = IMailtoHandlerTarget
+    handler_info = IMailtoHandlerInfo
+
+    def handle(self, data):
+        # TODO: handle form data
+        pass