Back to index

plone3  3.1.7
portlets.py
Go to the documentation of this file.
00001 from zope.component import adapter
00002 from zope.component import getAllUtilitiesRegisteredFor
00003 
00004 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
00005 
00006 from plone.portlets.interfaces import IPortletManager
00007 from plone.app.portlets.portlets.navigation import INavigationPortlet
00008 from plone.app.portlets.portlets.recent import IRecentPortlet
00009 from plone.app.portlets.interfaces import IDeferredPortletRenderer
00010 
00011 from kss.core.interfaces import IKSSView
00012 
00013 from Products.DCWorkflow.interfaces import IAfterTransitionEvent
00014 
00015 @adapter(None, IKSSView, IObjectModifiedEvent)
00016 def attributesTriggerNavigationPortletReload(obj, view, event):
00017     triggeringAttributes = ('title', 'description')
00018     if attributesModified(triggeringAttributes, event):
00019         portletReloader = PortletReloader(view)
00020         portletReloader.reloadPortletsByInterface(INavigationPortlet)
00021 
00022 @adapter(None, IKSSView, IObjectModifiedEvent)
00023 def attributesTriggerRecentPortletReload(obj, view, event):
00024     triggeringAttributes = ('title', 'description')
00025     if attributesModified(triggeringAttributes, event):
00026         portletReloader = PortletReloader(view)
00027         portletReloader.reloadPortletsByInterface(IRecentPortlet)
00028 
00029 def attributesModified(triggeringAttributes, event):
00030     for description in event.descriptions:
00031         for attr in triggeringAttributes:
00032             if attr in description.attributes:
00033                 return True
00034     return False
00035 
00036 @adapter(None, IKSSView, IAfterTransitionEvent)
00037 def workflowTriggersNavigationPortletReload(obj, view, event):
00038     if not (event.old_state is event.new_state):
00039         obj.reindexObject()
00040         portletReloader = PortletReloader(view)
00041         portletReloader.reloadPortletsByInterface(INavigationPortlet)
00042 
00043 @adapter(None, IKSSView, IAfterTransitionEvent)
00044 def workflowTriggersRecentPortletReload(obj, view, event):
00045     if not (event.old_state is event.new_state):
00046         obj.reindexObject()
00047         portletReloader = PortletReloader(view)
00048         portletReloader.reloadPortletsByInterface(IRecentPortlet)
00049 
00050 class PortletReloader(object):
00051     def __init__(self, view):
00052         self.view = view
00053         self.context = view.context
00054         self.request = view.request
00055 
00056     def reloadPortletsByInterface(self, interface):
00057         for info in self.getPortletsByInterface(interface):
00058             self.reloadPortletByInfo(info)
00059 
00060     def getPortletsByInterface(self, interface):
00061         return [info for info in self.getAllPortletInfos() 
00062                 if interface.providedBy(info['assignment'])]
00063     
00064     def reloadPortletByInfo(self, info):
00065         renderer = info['renderer']
00066         renderer.update()
00067         if IDeferredPortletRenderer.providedBy(renderer):
00068             # if this is a deferred load, prepare now the data
00069             renderer.deferred_update()
00070         result = renderer.render()
00071         portlethash = info['hash']
00072         wrapper_id = 'portletwrapper-%s' % portlethash
00073         ksscore = self.view.getCommandSet('core')
00074         ksscore.replaceInnerHTML(ksscore.getHtmlIdSelector(wrapper_id), result, withKssSetup='False')
00075 
00076     def getAllPortletInfos(self):
00077         portletInfos = []
00078         for manager in getAllUtilitiesRegisteredFor(IPortletManager):
00079             managerRenderer = manager(self.context, self.request, self.view)
00080             if managerRenderer.visible:
00081                 portletInfos += managerRenderer.portletsToShow()
00082         return portletInfos