Commit 4ad27de2 authored by Georgios Dagkakis's avatar Georgios Dagkakis

Formulator: introduce 'structure' and 'no_translate' prefixes in TALES expression

to be able to disactivate html_escape and translation respectively
parent 9db00ea6
...@@ -4,7 +4,8 @@ import Widget, Validator ...@@ -4,7 +4,8 @@ import Widget, Validator
from Persistence import Persistent from Persistence import Persistent
import Acquisition import Acquisition
from Field import ZMIField from Field import ZMIField
from AccessControl import getSecurityManager from AccessControl import getSecurityManager, ClassSecurityInfo
from copy import deepcopy
class TALESWidget(Widget.TextWidget): class TALESWidget(Widget.TextWidget):
default = fields.MethodField('default', default = fields.MethodField('default',
...@@ -47,18 +48,55 @@ try: ...@@ -47,18 +48,55 @@ try:
class TALESMethod(Persistent, Acquisition.Implicit): class TALESMethod(Persistent, Acquisition.Implicit):
"""A method object; calls method name in acquisition context. """A method object; calls method name in acquisition context.
""" """
STANDARD_PREFIX_LIST = ['exists', 'nocall', 'not', 'string', 'python']
ADDITIONAL_PREFIX_LIST = ['structure', 'no_translate']
security = ClassSecurityInfo()
def __init__(self, text): def __init__(self, text):
self._text = text self._text = text
def __call__(self, **kw): def __call__(self, **kw):
expr = getattr(self, '_v_expr', None) expression = getattr(self, '_v_expr', None)
if expr is None: if expression is None:
self._v_expr = expr = getEngine().compile(self._text) expression = self._getExpression()
return getEngine().getContext(kw).evaluate(expr) return getEngine().getContext(kw).evaluate(expression)
# check if we have 'View' permission for this method def _getPrefixList(self):
# (raises error if not) '''
# getSecurityManager().checkPermission('View', method) user should give a TALES expression as
prefix1; prefix2; prefixN: expression
prefixN should be one (and the only one) of the standard
zope prefixes
'''
prefix_list = []
# XXX can we have a path expression (so no prefix)
# that has ':' within?
splitted_text = self._text.split(':')
if len(splitted_text) > 1:
prefix_list = splitted_text[0].split(';')
# XXX should we assert that the prefixes are correct?
stripped_prefix_list = [x.strip() for x in prefix_list]
for prefix in stripped_prefix_list:
if prefix == stripped_prefix_list[-1]:
assert prefix in self.STANDARD_PREFIX_LIST, \
'prefix %s is either not found or misplaced' % prefix
else:
assert prefix in self.ADDITIONAL_PREFIX_LIST, \
'prefix %s is either not found or misplaced' % prefix
return stripped_prefix_list
def _getExpression(self):
expression_text = deepcopy(self._text)
for prefix in self.ADDITIONAL_PREFIX_LIST:
if prefix in self._getPrefixList():
expression_text = expression_text.replace(prefix + ';', '', 1)
expression_text = expression_text.lstrip()
self._v_expr = expression = getEngine().compile(expression_text)
return expression
security.declarePublic('hasPrefix')
def hasPrefix(self, prefix):
return prefix in self._getPrefixList()
TALES_AVAILABLE = 1 TALES_AVAILABLE = 1
......
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