Back to index

plone3  3.1.7
refreshportlet.py
Go to the documentation of this file.
00001 from zope.deprecation import deprecate
00002 
00003 from kss.core.BeautifulSoup import BeautifulSoup
00004 from kss.core import CommandSet
00005 from plone.app.portlets.utils import assignment_from_key
00006 from plone.portlets.utils import unhashPortletInfo
00007 from plone.portlets.interfaces import IPortletManager, IPortletRenderer
00008 from plone.app.portlets.interfaces import IDeferredPortletRenderer
00009 from zope.component import getMultiAdapter, getUtility
00010 
00011 # XXX: This is deprecated and will be removed in Plone 4.0. Use the 'plone'
00012 # command set instead.
00013 class RefreshPortletCommand(CommandSet):
00014     
00015     @deprecate("The 'refreshportlet' command set is deprecated and will be removed in Plone 4.0. "
00016                "Please use the 'plone' command set instead.")
00017     def refreshPortletLegacy(self, name, nodeid=None, **kw):
00018         'Refresh portlet by name (old portlets)'
00019         if name.startswith('portlet-'):
00020             name = name[8:]
00021         if nodeid is None:
00022             nodeid = name
00023         # render it
00024         portlet_body = self.view.macroContent('portlet_%s/macros/portlet' % (name, ), **kw)
00025         # Good. Now, unfortunately we don't have any marker on the outside div.
00026         # So we just select the <dl> for insertion.
00027         # This could be spared with smarter templating.
00028         soup = BeautifulSoup(portlet_body)
00029         tag = soup.find('dl', id=nodeid)
00030         result = unicode(tag)
00031         # Command the replacement
00032         ksscore = self.getCommandSet('core')
00033         ksscore.replaceHTML(ksscore.getHtmlIdSelector(nodeid), result)
00034 
00035     @deprecate("The 'refreshportlet' command set is deprecated and will be removed in Plone 4.0. "
00036                "Please use the 'plone' command set instead.")
00037     def refreshPortlet(self, portlethash, **kw):
00038         'Refresh portlet by its hash (new portlets)'
00039         # put parameters on the request, by saving the original context
00040         self.request.form, orig_form = kw, self.request.form
00041         # Prepare the portlet and render the data
00042         info = unhashPortletInfo(portlethash) 
00043         manager = getUtility(IPortletManager, info['manager'])
00044         assignment = assignment_from_key(context = self.context, 
00045                                          manager_name = info['manager'], 
00046                                          category = info['category'],
00047                                          key = info['key'],
00048                                          name = info['name'])
00049         renderer = getMultiAdapter(
00050                 (self.context, self.request, self.view, manager, assignment.data),
00051                 IPortletRenderer
00052             )
00053         renderer = renderer.__of__(self.context)
00054         renderer.update()
00055         if IDeferredPortletRenderer.providedBy(renderer):
00056             # if this is a deferred load, prepare now the data
00057             renderer.deferred_update()
00058         result = renderer.render()
00059         # Revert the original request
00060         self.request.form = orig_form
00061         # Command the replacement
00062         wrapper_id = 'portletwrapper-%s' % portlethash
00063         ksscore = self.getCommandSet('core')
00064         ksscore.replaceInnerHTML(ksscore.getHtmlIdSelector(wrapper_id), result)