src/pyams_portal/portlet.py
changeset 0 6f99128c6d48
child 5 670b7956c689
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlet.py	Wed Jun 17 09:58:33 2015 +0200
@@ -0,0 +1,160 @@
+#
+# 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 logging
+logger = logging.getLogger('PyAMS (portal)')
+
+import venusian
+
+# import interfaces
+from pyams_portal.interfaces import IPortlet, IPortletRenderer, IPortletConfiguration, \
+    IPortalPage, IPortletPreviewer
+from zope.schema.interfaces import IVocabularyFactory
+
+# import packages
+from persistent import Persistent
+from pyams_utils.request import check_request
+from pyams_viewlet.viewlet import ContentProvider
+from pyramid.exceptions import ConfigurationError
+from zope.container.contained import Contained
+from zope.interface import implementer, provider
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import getVocabularyRegistry, SimpleVocabulary, SimpleTerm
+
+
+@implementer(IPortletConfiguration)
+class PortletConfiguration(Persistent, Contained):
+    """Portlet configuration"""
+
+    template = None
+    portlet_name = None
+    slot_name = FieldProperty(IPortletConfiguration['slot_name'])
+    position = FieldProperty(IPortletConfiguration['position'])
+    visible = FieldProperty(IPortletConfiguration['visible'])
+    _inherit_parent = FieldProperty(IPortletConfiguration['inherit_parent'])
+
+    def __init__(self, portlet):
+        self.portlet_name = portlet.name
+
+    @property
+    def can_inherit(self):
+        return IPortalPage.providedBy(self.__parent__)
+
+    @property
+    def inherit_parent(self):
+        return self._inherit_parent if self.can_inherit else False
+
+    @inherit_parent.setter
+    def inherit_parent(self, value):
+        self._inherit_parent = value
+
+
+@implementer(IPortlet)
+class Portlet(object):
+    """Base portlet content provider"""
+
+    permission = FieldProperty(IPortlet['permission'])
+
+    toolbar_image = None
+    toolbar_css_class = 'fa fa-fw fa-2x fa-edit'
+
+
+@provider(IVocabularyFactory)
+class PortletVocabulary(SimpleVocabulary):
+    """Portlet vocabulary"""
+
+    def __init__(self, context):
+        request = check_request()
+        translate = request.localizer.translate
+        utils = request.registry.getUtilitiesFor(IPortlet)
+        terms = [SimpleTerm(name, title=translate(util.label))
+                 for name, util in sorted(utils, key=lambda x: translate(x[1].label))]
+        super(PortletVocabulary, self).__init__(terms)
+
+getVocabularyRegistry().register('PyAMS portal portlets', PortletVocabulary)
+
+
+class PortletContentProvider(ContentProvider):
+    """Bae portlet content provider"""
+
+    def __init__(self, context, request, view, portlet_config):
+        super(PortletContentProvider, self).__init__(context, request, view)
+        self.__parent__ = view
+        self.configuration = portlet_config
+        self.portlet = self.request.registry.getUtility(IPortlet, name=portlet_config.portlet_name)
+
+    def __call__(self):
+        if self.portlet.permission and not self.request.has_permission(self.portlet.permission):
+            return ''
+        self.update()
+        return self.render()
+
+
+@implementer(IPortletPreviewer)
+class PortletPreviewer(PortletContentProvider):
+    """Portlet previewer adapter"""
+
+
+@implementer(IPortletRenderer)
+class PortletRenderer(PortletContentProvider):
+    """Portlet renderer adapter"""
+
+
+class portlet_config(object):
+    """Class decorator used to declare a portlet"""
+
+    venusian = venusian  # for testing injection
+
+    def __init__(self, **settings):
+        self.__dict__.update(settings)
+
+    def __call__(self, wrapped):
+        settings = self.__dict__.copy()
+        depth = settings.pop('_depth', 0)
+
+        def callback(context, name, ob):
+            name = settings.get('name') or getattr(ob, 'name', None)
+            if name is None:
+                raise ConfigurationError("You must provide a name for a portlet")
+
+            permission = settings.get('permission')
+            if permission is not None:
+                ob.permission = permission
+
+            if type(ob) is type:
+                factory = ob
+                component = None
+            else:
+                factory = None
+                component = ob
+
+            config = context.config.with_package(info.module)
+            logger.debug("Registering portlet {0} named '{1}'".format(str(component) if component else str(factory),
+                                                                      name))
+            config.registry.registerUtility(component=component, factory=factory,
+                                            provided=IPortlet, name=name)
+
+        info = self.venusian.attach(wrapped, callback, category='pyams_portal',
+                                    depth=depth + 1)
+        if info.scope == 'class':
+            # if the decorator was attached to a method in a class, or
+            # otherwise executed at class scope, we need to set an
+            # 'attr' into the settings if one isn't already in there
+            if settings.get('attr') is None:
+                settings['attr'] = wrapped.__name__
+
+        settings['_info'] = info.codeinfo  # fbo "action_method"
+        return wrapped