Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Browser view utilities.
00014 
00015 $Id: utils.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 from AccessControl.SecurityInfo import ClassSecurityInfo
00019 from Globals import InitializeClass
00020 from Products.Five import BrowserView
00021 from zope.component import getUtility
00022 
00023 from Products.CMFCore.interfaces import IPropertiesTool
00024 from Products.CMFCore.utils import getToolByName
00025 from Products.CMFDefault.permissions import View
00026 from Products.CMFDefault.utils import getBrowserCharset
00027 from Products.CMFDefault.utils import toUnicode
00028 
00029 
00030 def decode(meth):
00031     def decoded_meth(self, *args, **kw):
00032         return toUnicode(meth(self, *args, **kw), self._getDefaultCharset())
00033     return decoded_meth
00034 
00035 def memoize(meth):
00036     def memoized_meth(self, *args):
00037         if not hasattr(self, '__memo__'):
00038             self.__memo__ = {}
00039         sig = (meth, args)
00040         if sig not in self.__memo__:
00041             self.__memo__[sig] = meth(self, *args)
00042         return self.__memo__[sig]
00043     return memoized_meth
00044 
00045 
00046 class MacroView(BrowserView):
00047 
00048     """Allows to use macros from non-view templates.
00049     """
00050     
00051     # The following allows to traverse the view/class and reach
00052     # macros defined in page templates, e.g. in a use-macro.
00053     security = ClassSecurityInfo()
00054 
00055     def _macros(self):
00056         return self.index.macros
00057 
00058     security.declareProtected(View, 'macros')
00059     macros = property(_macros, None, None)
00060 
00061 InitializeClass(MacroView)
00062 
00063 
00064 class ViewBase(BrowserView):
00065 
00066     # helpers
00067 
00068     @memoize
00069     def _getTool(self, name):
00070         return getToolByName(self.context, name)
00071 
00072     @memoize
00073     def _checkPermission(self, permission):
00074         mtool = self._getTool('portal_membership')
00075         return mtool.checkPermission(permission, self.context)
00076 
00077     @memoize
00078     def _getPortalURL(self):
00079         utool = self._getTool('portal_url')
00080         return utool()
00081 
00082     @memoize
00083     def _getViewURL(self):
00084         return self.request['ACTUAL_URL']
00085 
00086     @memoize
00087     def _getDefaultCharset(self):
00088         ptool = getUtility(IPropertiesTool)
00089         return ptool.getProperty('default_charset', None)
00090 
00091     @memoize
00092     def _getBrowserCharset(self):
00093         return getBrowserCharset(self.request)
00094 
00095     # interface
00096 
00097     @memoize
00098     @decode
00099     def title(self):
00100         return self.context.Title()
00101 
00102     @memoize
00103     @decode
00104     def description(self):
00105         return self.context.Description()