Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 from AccessControl import getSecurityManager
00002 from AccessControl import Unauthorized
00003 from Products.PageTemplates.PageTemplateFile import PageTemplateFile as Z2PTF
00004 from zope.pagetemplate.pagetemplatefile import PageTemplateFile as Z3PTF
00005 from zope.app.pagetemplate.viewpagetemplatefile import BoundPageTemplate
00006 
00007 
00008 def isTemplate(obj):
00009     """ check if the given object is a or is derived from a template class """
00010     # TODO: we should really check via interfaces, i.e. `providedBy` here,
00011     #       but the only class using interfaces atm is Z3PTF :(
00012     return isinstance(obj, Z2PTF) or isinstance(obj, Z3PTF) or \
00013         isinstance(obj, BoundPageTemplate)
00014 
00015 
00016 def findViewletTemplate(viewlet):
00017     """ try to find the attribute holding the template within a viewlet """
00018     for attr in 'index', 'template', '_template', '__call__', 'render':
00019         item = getattr(viewlet, attr, None)
00020         if isTemplate(item):
00021             return attr, item
00022     attrs = [ attr for attr in dir(viewlet) if isTemplate(getattr(viewlet, attr, None)) ]
00023     if len(attrs) == 1:
00024         return attrs[0], getattr(viewlet, attrs[0])
00025     else:
00026         # TODO: we should pass on the message if we find multiple templates
00027         pass
00028     return None, None
00029 
00030 
00031 def checkPermission(permission, context):
00032     sm = getSecurityManager()
00033     if permission is not None:
00034         if not sm.checkPermission(permission, context):
00035             raise Unauthorized('The current user does not have the '
00036                                'required "%s" permission' % permission)
00037