fixes #1310 and improves upon ideas shared on #1450

This commit is contained in:
niphlod
2016-09-22 00:01:22 +02:00
parent 075f493332
commit e5dcdb074f
2 changed files with 27 additions and 31 deletions
+14 -13
View File
@@ -24,6 +24,9 @@ try:
# have web2py
from gluon.restricted import RestrictedError
from gluon.globals import current
from gluon.cfs import getcfs
from gluon.fileutils import read_file
HAS_CFS = True
except ImportError:
# do not have web2py
current = None
@@ -426,7 +429,7 @@ class TemplateParser(object):
# Allow Views to include other views dynamically
context = self.context
if current and not "response" in context:
if current and "response" not in context:
context["response"] = getattr(current, 'response', None)
# Get the filename; filename looks like ``"template.html"``.
@@ -779,12 +782,15 @@ def parse_template(filename,
# First, if we have a str try to open the file
if isinstance(filename, str):
try:
fp = open(os.path.join(path, filename), 'rb')
text = fp.read()
fp.close()
except IOError:
raise RestrictedError(filename, '', 'Unable to find the file')
fname = os.path.join(path, filename)
if HAS_CFS:
text = getcfs(fname, fname, lambda: read_file(fname))
else:
try:
with open(fname, 'rb') as fp:
text = fp.read()
except IOError:
raise RestrictedError(filename, '', 'Unable to find the file')
else:
text = filename.read()
text = to_native(text)
@@ -890,7 +896,7 @@ def render(content="hello world",
Response = DummyResponse
# Add it to the context so we can use it.
if not 'NOESCAPE' in context:
if 'NOESCAPE' not in context:
context['NOESCAPE'] = NOESCAPE
if isinstance(content, unicodeT):
@@ -936,8 +942,3 @@ def render(content="hello world",
if old_response_body is not None:
context['response'].body = old_response_body
return text
if __name__ == '__main__':
import doctest
doctest.testmod()