Skip to content

Commit

Permalink
clarify naming and make sure that TTW viewlets and portlets get the c…
Browse files Browse the repository at this point in the history
…orrect view parameter
  • Loading branch information
davisagli committed Apr 30, 2010
1 parent 872b25b commit 94dfe3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,8 @@ CHANGES
1.0b1 - Unreleased
------------------

Make sure TTW viewlet and portlet classes get the containing view as the
view parameter on initialization, rather than getting themselves.

1.0a1 - 2009-11-14
------------------
Expand Down
69 changes: 34 additions & 35 deletions src/five/customerize/zpt.py
Expand Up @@ -28,7 +28,7 @@ def __init__(self, id, text=None, content_type='text/html', strict=True,
super(TTWViewTemplate, self).__init__(id, text, content_type, encoding,
strict)

def __call__(self, context, request, viewlet=None, manager=None, data=None):
def __call__(self, context, request, view=None, manager=None, data=None):
#XXX raise a sensible exception if context and request are
# omitted, IOW, if someone tries to render the template not as
# a view.
Expand All @@ -37,13 +37,13 @@ def __call__(self, context, request, viewlet=None, manager=None, data=None):
# is actually called, because it may be looked up during traversal,
# in which case there's no proper security context yet
if IPortletManager.providedBy(manager):
return TTWPortletRenderer(context, request, self, self.view,
return TTWPortletRenderer(context, request, self, view,
manager, data, self.permission)
if IViewletManager.providedBy(manager):
return TTWViewletRenderer(context, request, self, self.view,
viewlet, manager, self.permission)
return TTWViewletRenderer(context, request, self, view,
manager, self.permission)
else:
return TTWViewTemplateRenderer(context, request, self, self.view, self.permission)
return TTWViewTemplateRenderer(context, request, self, self.permission)

# overwrite Shared.DC.Scripts.Binding.Binding's before traversal
# hook that would prevent to look up views for instances of this
Expand All @@ -61,11 +61,11 @@ class is instantiated. It holds a reference to the
(__call__).
"""

def __init__(self, context, request, template, view, permission=None):
def __init__(self, context, request, template, permission=None):
self.context = context
self.request = request
self.template = template
self.view = view
self.view = None
self.permission = permission

def __call__(self, *args, **kwargs):
Expand All @@ -84,17 +84,19 @@ def __call__(self, *args, **kwargs):

def _getView(self):
checkPermission(self.permission, self.context)
view = self.view
if view is not None:
if self.view is not None:
return self.view
view_class = self.template.view
if view_class is not None:
# Filesystem-based view templates are trusted code and
# have unrestricted access to the view class. We simulate
# that for TTW templates (which are trusted code) by
# creating a subclass with unrestricted access to all
# subobjects.
class TTWView(view):
class TTWView(view_class):
__allow_access_to_unprotected_subobjects__ = 1
view = TTWView(self.context, self.request)
return view
self.view = TTWView(self.context, self.request)
return self.view

# Zope 2 wants to acquisition-wrap every view object (via __of__).
# We don't need this as the TTWViewTemplate object is already
Expand All @@ -110,49 +112,47 @@ class TTWViewletRenderer(object):

__allow_access_to_unprotected_subobjects__ = True

def __init__(self, context, request, template, view, viewlet=None, manager=None, permission=None):
def __init__(self, context, request, template, view, manager=None, permission=None):
self.context = context
self.request = request
self.template = template
self.view = view
self.viewlet = viewlet
self.manager = manager
self.ttwviewlet = None
self.viewlet = None
self.permission = permission

def update(self):
""" update the viewlet before `render` is called """
view = self._getViewlet().update()
self._getViewlet().update()

def render(self, *args, **kwargs):
""" render the viewlet using the customized template """
view = self._getViewlet()
viewlet = self._getViewlet()
# we need to override the template's context and request as
# they generally point to the wrong objects (a template's
# context usually is what it was acquired from, which isn't
# what the context is for a view template).
bound_names = {'context': self.context,
'request': self.request,
'view': view}
'view': viewlet}
template = self.template.__of__(self.context)
return template._exec(bound_names, args, kwargs)

def _getViewlet(self):
checkPermission(self.permission, self.context)
if self.ttwviewlet is not None:
return self.ttwviewlet
view = self.view
if view is not None:
if self.viewlet is not None:
return self.viewlet
view_class = self.template.view
if view_class is not None:
# Filesystem-based view templates are trusted code and
# have unrestricted access to the view class. We simulate
# that for TTW templates (which are trusted code) by
# creating a subclass with unrestricted access to all
# subobjects.
class TTWViewlet(view, ViewletBase):
class TTWViewlet(view_class, ViewletBase):
__allow_access_to_unprotected_subobjects__ = 1
view = TTWViewlet(self.context, self.request, self.viewlet, self.manager)
self.ttwviewlet = view
return view
self.viewlet = TTWViewlet(self.context, self.request, self.view, self.manager)
return self.viewlet

# Zope 2 wants to acquisition-wrap every view object (via __of__).
# We don't need this as the TTWViewTemplate object is already
Expand Down Expand Up @@ -180,37 +180,36 @@ def __init__(self, context, request, template, view, manager=None, data=None, pe

def update(self):
""" update the portlet before `render` is called """
view = self._getRenderer().update()
self._getRenderer().update()

def render(self, *args, **kwargs):
""" render the portlet using the customized template """
view = self._getRenderer()
renderer = self._getRenderer()
# we need to override the template's context and request as
# they generally point to the wrong objects (a template's
# context usually is what it was acquired from, which isn't
# what the context is for a view template).
bound_names = {'context': self.context,
'request': self.request,
'view': view}
'view': renderer}
template = self.template.__of__(self.context)
return template._exec(bound_names, args, kwargs)

def _getRenderer(self):
checkPermission(self.permission, self.context)
if self.renderer is not None:
return self.renderer
view = self.view
if view is not None:
view_class = self.template.view
if view_class is not None:
# Filesystem-based view templates are trusted code and
# have unrestricted access to the view class. We simulate
# that for TTW templates (which are trusted code) by
# creating a subclass with unrestricted access to all
# subobjects.
class TTWPortlet(view):
class TTWPortlet(view_class):
__allow_access_to_unprotected_subobjects__ = 1
view = TTWPortlet(self.context, self.request, self.view, self.manager, self.data)
self.renderer = view
return view
self.renderer = TTWPortlet(self.context, self.request, self.view, self.manager, self.data)
return self.renderer

@property
def available(self):
Expand Down

0 comments on commit 94dfe3d

Please sign in to comment.