Back to index

plone3  3.1.7
manager.py
Go to the documentation of this file.
00001 import sys
00002 from ZODB.POSException import ConflictError
00003 
00004 from zope.interface import Interface
00005 from zope.component import adapts, getMultiAdapter
00006 
00007 from zope.publisher.interfaces.browser import IBrowserView
00008 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
00009 
00010 from Acquisition import Explicit, aq_inner, aq_parent, aq_acquire
00011 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00012 from Products.CMFCore.utils import getToolByName
00013 from Products.CMFPlone.utils import isDefaultPage
00014 
00015 from plone.portlets.interfaces import IPortletRenderer, ILocalPortletAssignable
00016 from plone.portlets.manager import PortletManagerRenderer as BasePortletManagerRenderer
00017 
00018 from plone.app.portlets.interfaces import IColumn
00019 from plone.app.portlets.interfaces import IDashboard
00020 
00021 import logging
00022 logger = logging.getLogger('portlets')
00023 
00024 class PortletManagerRenderer(BasePortletManagerRenderer, Explicit):
00025     """A Zope 2 implementation of the default PortletManagerRenderer
00026     """
00027 
00028     def _dataToPortlet(self, data):
00029         """Helper method to get the correct IPortletRenderer for the given
00030         data object.
00031         """
00032         portlet = getMultiAdapter((self.context, self.request, self.__parent__,
00033                                         self.manager, data,), IPortletRenderer)
00034         return portlet.__of__(self.context)
00035 
00036 class ColumnPortletManagerRenderer(PortletManagerRenderer):
00037     """A renderer for the column portlets
00038     """
00039     adapts(Interface, IDefaultBrowserLayer, IBrowserView, IColumn)
00040     template = ViewPageTemplateFile('browser/templates/column.pt')
00041     error_message = ViewPageTemplateFile('browser/templates/error_message.pt')
00042 
00043     def _context(self):
00044         context = aq_inner(self.context)
00045         if isDefaultPage(context, self.request):
00046             return aq_parent(context)
00047         else:
00048             return context
00049 
00050     def base_url(self):
00051         """If context is a default-page, return URL of folder, else
00052         return URL of context.
00053         """
00054         return str(getMultiAdapter((self._context(), self.request,), name=u'absolute_url'))
00055 
00056     def can_manage_portlets(self):
00057         context = self._context()
00058         if not ILocalPortletAssignable.providedBy(context):
00059             return False
00060         mtool = getToolByName(context, 'portal_membership')
00061         return mtool.checkPermission("Portlets: Manage portlets", context)
00062 
00063     def safe_render(self, portlet_renderer):
00064         try:
00065             return portlet_renderer.render()
00066         except ConflictError:
00067             raise
00068         except Exception:
00069             logger.exception('Error while rendering %r' % (self,))
00070             aq_acquire(self, 'error_log').raising(sys.exc_info())
00071             return self.error_message()
00072 
00073 class DashboardPortletManagerRenderer(ColumnPortletManagerRenderer):
00074     """Render a column of the dashboard
00075     """
00076     
00077     adapts(Interface, IDefaultBrowserLayer, IBrowserView, IDashboard)
00078     template = ViewPageTemplateFile('browser/templates/dashboard-column.pt')