Commit e81982f8 authored by Evan Simpson's avatar Evan Simpson

Fix caching

parent 832932e5
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
HTML- and XML-based template objects using TAL, TALES, and METAL. HTML- and XML-based template objects using TAL, TALES, and METAL.
""" """
__version__='$Revision: 1.12 $'[11:-2] __version__='$Revision: 1.13 $'[11:-2]
import os, sys, traceback, pprint import os, sys, traceback, pprint
from TAL.TALParser import TALParser from TAL.TALParser import TALParser
...@@ -117,7 +117,7 @@ class PageTemplate: ...@@ -117,7 +117,7 @@ class PageTemplate:
macros = MacroCollection() macros = MacroCollection()
def pt_edit(self, text, content_type): def pt_edit(self, text, content_type):
if content_type and content_type != self.content_type: if content_type:
self.content_type = str(content_type) self.content_type = str(content_type)
if hasattr(text, 'read'): if hasattr(text, 'read'):
text = text.read() text = text.read()
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
Zope object encapsulating a Page Template. Zope object encapsulating a Page Template.
""" """
__version__='$Revision: 1.12 $'[11:-2] __version__='$Revision: 1.13 $'[11:-2]
import os, AccessControl, Acquisition, sys import os, AccessControl, Acquisition, sys
from Globals import DTMLFile, MessageDialog, package_home from Globals import DTMLFile, MessageDialog, package_home
...@@ -101,6 +101,7 @@ from AccessControl import getSecurityManager ...@@ -101,6 +101,7 @@ from AccessControl import getSecurityManager
from OFS.History import Historical, html_diff from OFS.History import Historical, html_diff
from OFS.Cache import Cacheable from OFS.Cache import Cacheable
from OFS.Traversable import Traversable from OFS.Traversable import Traversable
from OFS.PropertyManager import PropertyManager
from PageTemplate import PageTemplate from PageTemplate import PageTemplate
try: try:
...@@ -111,7 +112,7 @@ except ImportError: ...@@ -111,7 +112,7 @@ except ImportError:
SUPPORTS_WEBDAV_LOCKS = 0 SUPPORTS_WEBDAV_LOCKS = 0
class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
Traversable): Traversable, PropertyManager):
"Zope wrapper for Page Template using TAL, TALES, and METAL" "Zope wrapper for Page Template using TAL, TALES, and METAL"
if SUPPORTS_WEBDAV_LOCKS: if SUPPORTS_WEBDAV_LOCKS:
...@@ -129,8 +130,15 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -129,8 +130,15 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
manage_options = ( manage_options = (
{'label':'Edit', 'action':'pt_editForm'}, {'label':'Edit', 'action':'pt_editForm'},
{'label':'Test', 'action':'ZScriptHTML_tryForm'}, {'label':'Test', 'action':'ZScriptHTML_tryForm'},
) + Historical.manage_options + SimpleItem.manage_options + \ ) + PropertyManager.manage_options \
Cacheable.manage_options + Historical.manage_options \
+ SimpleItem.manage_options \
+ Cacheable.manage_options
_properties=({'id':'title', 'type': 'string'},
{'id':'content_type', 'type':'string'},
{'id':'expand', 'type':'boolean'},
)
def __init__(self, id, text=None, content_type=None): def __init__(self, id, text=None, content_type=None):
self.id = str(id) self.id = str(id)
...@@ -139,6 +147,10 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -139,6 +147,10 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
text = open(self._default_content_fn).read() text = open(self._default_content_fn).read()
self.pt_edit(text, content_type) self.pt_edit(text, content_type)
def _setPropValue(self, id, value):
Cache._setPropValue(self, id, value)
self.ZCacheable_invalidate()
security = AccessControl.ClassSecurityInfo() security = AccessControl.ClassSecurityInfo()
security.declareObjectProtected('View') security.declareObjectProtected('View')
...@@ -167,10 +179,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -167,10 +179,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
return self.pt_editForm(manage_tabs_message=message) return self.pt_editForm(manage_tabs_message=message)
def pt_setTitle(self, title): def pt_setTitle(self, title):
title = str(title) self._setPropValue('title', str(title))
if self.title != title:
self.title = title
self.ZCacheable_invalidate()
def pt_upload(self, REQUEST, file=''): def pt_upload(self, REQUEST, file=''):
"""Replace the document with the text in file.""" """Replace the document with the text in file."""
...@@ -236,12 +245,28 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -236,12 +245,28 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
self.content_type) self.content_type)
except AttributeError: pass except AttributeError: pass
# Execute the template in a new security context.
security=getSecurityManager() security=getSecurityManager()
bound_names['user'] = security.getUser() bound_names['user'] = security.getUser()
# Retrieve the value from the cache.
keyset = None
if self.ZCacheable_isCachingEnabled():
# Prepare a cache key.
keyset = {'here': self._getContext(),
'bound_names': bound_names}
result = self.ZCacheable_get(keywords=keyset)
if result is not None:
# Got a cached value.
return result
# Execute the template in a new security context.
security.addContext(self) security.addContext(self)
try: try:
return self.pt_render(extra_context=bound_names) result = self.pt_render(extra_context=bound_names)
if keyset is not None:
# Store the result in the cache.
self.ZCacheable_set(result, keywords=keyset)
return result
finally: finally:
security.removeContext(self) security.removeContext(self)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment