src/pyams_content/shared/form/handler.py
changeset 170 26aefef3d0aa
child 558 d9c6b1d7fefa
equal deleted inserted replaced
169:483b0f16e9a6 170:26aefef3d0aa
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_content.shared.form.interfaces import IFormHandler, IMailtoHandlerTarget, IMailtoHandlerInfo
       
    20 from zope.annotation.interfaces import IAnnotations
       
    21 
       
    22 # import packages
       
    23 from persistent import Persistent
       
    24 from pyams_utils.adapter import adapter_config
       
    25 from pyams_utils.registry import utility_config
       
    26 from pyams_utils.request import check_request
       
    27 from pyams_utils.vocabulary import vocabulary_config
       
    28 from zope.componentvocabulary.vocabulary import UtilityTerm, UtilityVocabulary
       
    29 from zope.interface import implementer
       
    30 from zope.schema.fieldproperty import FieldProperty
       
    31 
       
    32 from pyams_content import _
       
    33 
       
    34 
       
    35 @vocabulary_config(name='PyAMS form handlers')
       
    36 class FormHandlersVocabulary(UtilityVocabulary):
       
    37     """Form handlers vocabulary"""
       
    38 
       
    39     interface = IFormHandler
       
    40 
       
    41     def __init__(self, context, **kw):
       
    42         request = check_request()
       
    43         registry = request.registry
       
    44         translate = request.localizer.translate
       
    45         utils = [(None, translate(_("No selected handler...")))] + \
       
    46                 [(name, translate(util.label))
       
    47                  for (name, util) in registry.getUtilitiesFor(self.interface)]
       
    48         self._terms = dict((title, UtilityTerm(name, title)) for name, title in utils)
       
    49 
       
    50     def __iter__(self):
       
    51         return iter(self._terms.values())
       
    52 
       
    53 
       
    54 #
       
    55 # Mailto form handler
       
    56 #
       
    57 
       
    58 MAILTO_HANDLER_ANNOTATIONS_KEY = 'pyams_content.form.handler.mailto'
       
    59 
       
    60 
       
    61 @implementer(IMailtoHandlerInfo)
       
    62 class MailtoFormHandlerInfo(Persistent):
       
    63     """Mailto form handler persistent info"""
       
    64 
       
    65     source_address = FieldProperty(IMailtoHandlerInfo['source_address'])
       
    66     source_name = FieldProperty(IMailtoHandlerInfo['source_name'])
       
    67     target_address = FieldProperty(IMailtoHandlerInfo['target_address'])
       
    68     target_name = FieldProperty(IMailtoHandlerInfo['target_name'])
       
    69 
       
    70 
       
    71 @adapter_config(context=IMailtoHandlerTarget, provides=IMailtoHandlerInfo)
       
    72 def mailto_form_handler_factory(context):
       
    73     """Mailto form handler factory"""
       
    74     annotations = IAnnotations(context)
       
    75     info = annotations.get(MAILTO_HANDLER_ANNOTATIONS_KEY)
       
    76     if info is None:
       
    77         info = annotations[MAILTO_HANDLER_ANNOTATIONS_KEY] = MailtoFormHandlerInfo()
       
    78     return info
       
    79 
       
    80 
       
    81 @utility_config(name='mailto', provides=IFormHandler)
       
    82 class MailtoFormHandler(object):
       
    83     """Mailto form handler"""
       
    84 
       
    85     label = _("Mailto form handler")
       
    86     target_interface = IMailtoHandlerTarget
       
    87     handler_info = IMailtoHandlerInfo
       
    88 
       
    89     def handle(self, data):
       
    90         # TODO: handle form data
       
    91         pass