Skip to content

Commit

Permalink
Fix subscript access on Page Template macros attribute (#503)
Browse files Browse the repository at this point in the history
* Fix subscript access on Page Template ``macros`` attribute

* - adding a test that exercises the issue
  • Loading branch information
dataflake committed Feb 16, 2019
1 parent 9c8dd76 commit 12eb698
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -14,6 +14,9 @@ https://github.com/zopefoundation/Zope/blob/4.0a6/CHANGES.rst
Fixes
+++++

- Fix subscript access on Page Template ``macros`` attribute
(`#210 <https://github.com/zopefoundation/Zope/issues/210>`_)

- Fix ``OFS.interfaces`` attribute declarations to match reality
(`#498 <https://github.com/zopefoundation/Zope/issues/498`_)

Expand Down
5 changes: 5 additions & 0 deletions src/Products/PageTemplates/engine.py
Expand Up @@ -17,6 +17,7 @@
from chameleon.tales import StringExpr
from chameleon.tales import NotExpr
from chameleon.tal import RepeatDict
from chameleon.zpt.template import Macros

from z3c.pt.expressions import PythonExpr, ProviderExpr

Expand All @@ -33,6 +34,10 @@

InitializeClass(RepeatDict)

# Declare Chameleon Macros object accessible
# This makes subscripting work, as in template.macros['name']
Macros.__allow_access_to_unprotected_subobjects__ = True

re_match_pi = re.compile(r'<\?python([^\w].*?)\?>', re.DOTALL)
logger = logging.getLogger('Products.PageTemplates')

Expand Down
1 change: 1 addition & 0 deletions src/Products/PageTemplates/tests/macros.pt
@@ -0,0 +1 @@
<i metal:define-macro="foo">bar</i><i metal:use-macro="template/macros/foo" /><i metal:use-macro="python:template.macros['foo']" />
21 changes: 21 additions & 0 deletions src/Products/PageTemplates/tests/test_engine.py
Expand Up @@ -85,6 +85,27 @@ def test_zopepagetemplate_processing_instruction_skipped(self):
template.write(data)
self.assertIn('world', template())

def test_macros_access(self):
from Products.PageTemplates.ZopePageTemplate import \
manage_addPageTemplate
from zExceptions import Unauthorized
template = manage_addPageTemplate(self.folder, 'test')

# aq-wrap before we proceed
template = template.__of__(self.folder)

# test rendering engine
with open(os.path.join(path, "macros.pt")) as fd:
data = fd.read()
template.write(data)
try:
output = template()
raised = False
except Unauthorized:
raised = True
self.assertFalse(raised, 'Unauthorized exception raised')
self.assertIn('<i>bar</i><i>bar</i><i>bar</i>', output)


def test_suite():
return unittest.TestSuite((
Expand Down

0 comments on commit 12eb698

Please sign in to comment.