Back to index

plone3  3.1.7
plonecommands.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter, getUtility
00003 from zope.i18n import translate
00004 from zope.i18nmessageid.message import Message as i18nmessage
00005 
00006 from Products.statusmessages.message import Message
00007 
00008 from plone.portlets.interfaces import IPortletManager, IPortletRenderer
00009 from plone.portlets.utils import unhashPortletInfo
00010 
00011 from plone.app.portlets.interfaces import IDeferredPortletRenderer
00012 from plone.app.portlets.utils import assignment_from_key
00013 
00014 from kss.core import CommandSet
00015 from interfaces import IPloneCommands
00016 
00017 class PloneCommands(CommandSet):
00018     implements(IPloneCommands)
00019     
00020     def issuePortalMessage(self, message, msgtype='info'):
00021         if message is None:
00022             message = ''
00023 
00024         if isinstance(message, Message):
00025             msgtype = message.type
00026             # The translation domain of the message is not known.  We
00027             # can only assume that it is 'plone'.
00028             message = translate(message.message, domain='plone',
00029                                 context=self.request)
00030         elif isinstance(message, i18nmessage):
00031             # Here the message has a domain itself, which is good.
00032             message = translate(message, context=self.request)
00033 
00034         # The 'dt' of the definition list we generate should contain
00035         # something like Info, Warning or Error.  Those messages are
00036         # available in the plone domain.
00037         msgtype_name = translate(msgtype.capitalize(), domain='plone',
00038                                  context=self.request)
00039 
00040         # XXX The macro has to take in account that there might be more than
00041         # one status message.
00042         ksscore = self.getCommandSet('core')
00043         selector = ksscore.getHtmlIdSelector('kssPortalMessage')
00044 
00045         # We hide the standard Plone Portal Message
00046         standard_portal_message_selector = ksscore.getCssSelector('.portalMessage')
00047         ksscore.setStyle(standard_portal_message_selector, 'display','none')
00048 
00049         # Now there is always a portal message but it has to be
00050         # rendered visible or invisible, accordingly
00051         html = '<dt>%s</dt><dd>%s</dd>' % (msgtype_name, message)
00052         ksscore.replaceInnerHTML(selector, html)
00053         ksscore.setAttribute(selector, 'class', "portalMessage %s" % msgtype)
00054         ksscore.setStyle(selector, 'display', message and 'block' or 'none')
00055 
00056     def refreshPortlet(self, portlethash, **kw):
00057         # put parameters on the request, by saving the original context
00058         self.request.form, orig_form = kw, self.request.form
00059         
00060         # Prepare the portlet and render the data
00061         info = unhashPortletInfo(portlethash) 
00062         manager = getUtility(IPortletManager, info['manager'])
00063         
00064         assignment = assignment_from_key(context = self.context, 
00065                                          manager_name = info['manager'], 
00066                                          category = info['category'],
00067                                          key = info['key'],
00068                                          name = info['name'])
00069         renderer = getMultiAdapter(
00070                 (self.context, self.request, self.view, manager, assignment.data),
00071                 IPortletRenderer
00072             )
00073         renderer = renderer.__of__(self.context)
00074         
00075         renderer.update()
00076         if IDeferredPortletRenderer.providedBy(renderer):
00077             # if this is a deferred load, prepare now the data
00078             renderer.deferred_update()
00079         result = renderer.render()
00080         
00081         # Revert the original request
00082         self.request.form = orig_form
00083         
00084         # Command the replacement
00085         wrapper_id = 'portletwrapper-%s' % portlethash
00086         ksscore = self.getCommandSet('core')
00087         ksscore.replaceInnerHTML(ksscore.getHtmlIdSelector(wrapper_id), result)
00088 
00089     def refreshContentMenu(self):
00090         ksscore = self.getCommandSet('core')
00091         selector = ksscore.getHtmlIdSelector('contentActionMenus')
00092         self.getCommandSet('zope').refreshProvider(selector, 'plone.contentmenu')