Back to index

plone3  3.1.7
plonekssview.py
Go to the documentation of this file.
00001 # -*- coding: UTF-8 -*-
00002 
00003 from zope.interface import implements
00004 
00005 from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
00006 
00007 from Products.statusmessages import STATUSMESSAGEKEY
00008 from Products.statusmessages.adapter import _decodeCookieValue
00009 
00010 from kss.core import KSSView as base
00011 from kss.core import force_unicode
00012 
00013 from interfaces import IPloneKSSView
00014 
00015 class PloneKSSView(base):
00016     '''The base view that contains helpers, to be imported
00017     be other plone products
00018     '''
00019 
00020     implements(IPloneKSSView)
00021     
00022     header_macros = ZopeTwoPageTemplateFile('browser/macro_wrapper.pt')
00023 
00024     def macroContent(self, macropath, **kw):
00025         'Renders a macro and returns its text'
00026         path = macropath.split('/')
00027         if len(path) < 2 or path[-2] != 'macros':
00028             raise RuntimeError, 'Path must end with macros/name_of_macro (%s)' % (repr(macropath), )
00029         # needs string, do not tolerate unicode (causes but at traverse)
00030         jointpath = '/'.join(path[:-2]).encode('ascii')
00031         macroobj = self.context.restrictedTraverse(jointpath)
00032         try:
00033             the_macro = macroobj.macros[path[-1]]
00034         except AttributeError, IndexError:
00035             raise RuntimeError, 'Macro not found'
00036         #  
00037         # put parameters on the request, by saving the original context
00038         self.request.form, orig_form = kw, self.request.form
00039         content = self.header_macros.__of__(macroobj.aq_parent)(the_macro=the_macro)
00040         self.request.form = orig_form
00041         # Always encoded as utf-8
00042         content = force_unicode(content, 'utf')
00043         return content
00044 
00045     def issueAllPortalMessages(self):
00046         if hasattr(self.request.RESPONSE, 'cookies'):
00047             cookie = self.request.RESPONSE.cookies.get(STATUSMESSAGEKEY)
00048             if cookie:
00049                 encodedstatusmessages = cookie['value']
00050                 statusmessages = _decodeCookieValue(encodedstatusmessages)
00051             else:
00052                 statusmessages = []
00053             for msg in statusmessages:
00054                 self.getCommandSet('plone').issuePortalMessage(msg)
00055             self.request.RESPONSE.expireCookie(STATUSMESSAGEKEY, path='/')