Here are the examples of the python api django.template.Origin taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
15 Examples
3
Source : dummy.py
with GNU General Public License v3.0
from Aghoreshwar
with GNU General Public License v3.0
from Aghoreshwar
def get_template(self, template_name):
tried = []
for template_file in self.iter_template_filenames(template_name):
try:
with open(template_file, encoding=settings.FILE_CHARSET) as fp:
template_code = fp.read()
except FileNotFoundError:
tried.append((
Origin(template_file, template_name, self),
'Source does not exist',
))
else:
return Template(template_code)
raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
class Template(string.Template):
3
Source : filesystem.py
with GNU General Public License v3.0
from Aghoreshwar
with GNU General Public License v3.0
from Aghoreshwar
def get_template_sources(self, template_name):
"""
Return an Origin object pointing to an absolute path in each directory
in template_dirs. For security reasons, if a path doesn't lie inside
one of the template_dirs it is excluded from the result set.
"""
for template_dir in self.get_dirs():
try:
name = safe_join(template_dir, template_name)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
continue
yield Origin(
name=name,
template_name=template_name,
loader=self,
)
3
Source : locmem.py
with GNU General Public License v3.0
from Aghoreshwar
with GNU General Public License v3.0
from Aghoreshwar
def get_template_sources(self, template_name):
yield Origin(
name=template_name,
template_name=template_name,
loader=self,
)
3
Source : dummy.py
with MIT License
from Air-999
with MIT License
from Air-999
def get_template(self, template_name):
tried = []
for template_file in self.iter_template_filenames(template_name):
try:
with open(template_file, encoding='utf-8') as fp:
template_code = fp.read()
except FileNotFoundError:
tried.append((
Origin(template_file, template_name, self),
'Source does not exist',
))
else:
return Template(template_code)
raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
class Template(string.Template):
3
Source : locmem.py
with MIT License
from chunky2808
with MIT License
from chunky2808
def get_template_sources(self, template_name):
yield Origin(
name=template_name,
template_name=template_name,
loader=self,
)
def load_template_source(self, template_name, template_dirs=None):
3
Source : snippet.py
with Apache License 2.0
from dockerizeme
with Apache License 2.0
from dockerizeme
def render(self, context):
# flatten the Django Context into a single dictionary.
context_dict = {}
for d in context.dicts:
context_dict.update(d)
if settings.TEMPLATE_DEBUG:
from django.test import signals
self.origin = Origin(self.filename)
signals.template_rendered.send(sender=self, template=self, context=context)
return super(Template, self).render(context_dict)
class Loader(BaseLoader):
3
Source : tests.py
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
def test_origin_not_shown_in_production(self):
template = Template(
'{% load origin %}{% origin %}',
origin=Origin('/fake/base/templates/my-template.html'),
)
with override_settings(SERVER_MODE='production'):
self.assertEqual(template.render(Context()), '')
def test_origin_shown_in_development_and_test(self):
3
Source : tests.py
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
def test_origin_shown_in_development_and_test(self):
template = Template(
'{% load origin %}{% origin %}',
origin=Origin('/fake/base/templates/my-template.html'),
)
for mode in ['development', 'test']:
with override_settings(SERVER_MODE=mode):
output = template.render(Context())
self.assertIn('templates/my-template.html', output)
for component in ['fake', 'base']:
self.assertNotIn(component, output, 'Reported path should be relative to BASE_DIR')
def test_origin_outside_base_dir(self):
3
Source : tests.py
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
with BSD 3-Clause "New" or "Revised" License
from ietf-tools
def test_origin_outside_base_dir(self):
template = Template(
'{% load origin %}{% origin %}',
origin=Origin('/different/templates/my-template.html'),
)
with override_settings(SERVER_MODE='development'):
for component in ['fake', 'base', 'different', 'templates']:
output = template.render(Context())
self.assertNotIn(component, output,
'Full path components should not be revealed in html')
0
Source : dummy.py
with MIT License
from chunky2808
with MIT License
from chunky2808
def get_template(self, template_name):
tried = []
for template_file in self.iter_template_filenames(template_name):
try:
with io.open(template_file, encoding=settings.FILE_CHARSET) as fp:
template_code = fp.read()
except IOError as e:
if e.errno == errno.ENOENT:
tried.append((
Origin(template_file, template_name, self),
'Source does not exist',
))
continue
raise
return Template(template_code)
else:
raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
class Template(string.Template):
0
Source : base.py
with MIT License
from chunky2808
with MIT License
from chunky2808
def load_template(self, template_name, template_dirs=None):
warnings.warn(
'The load_template() method is deprecated. Use get_template() '
'instead.', RemovedInDjango20Warning,
)
source, display_name = self.load_template_source(
template_name, template_dirs,
)
origin = Origin(
name=display_name,
template_name=template_name,
loader=self,
)
try:
template = Template(source, origin, template_name, self.engine)
except TemplateDoesNotExist:
# If compiling the template we found raises TemplateDoesNotExist,
# back off to returning the source and display name for the
# template we were asked to load. This allows for correct
# identification of the actual template that does not exist.
return source, display_name
else:
return template, None
def get_template_sources(self, template_name):
0
Source : cached.py
with MIT License
from chunky2808
with MIT License
from chunky2808
def find_template(self, name, dirs=None):
"""
RemovedInDjango20Warning: An internal method to lookup the template
name in all the configured loaders.
"""
key = self.cache_key(name, dirs)
try:
result = self.find_template_cache[key]
except KeyError:
result = None
for loader in self.loaders:
try:
template, display_name = loader(name, dirs)
except TemplateDoesNotExist:
pass
else:
origin = Origin(
name=display_name,
template_name=name,
loader=loader,
)
result = template, origin
break
self.find_template_cache[key] = result
if result:
return result
else:
self.template_cache[key] = TemplateDoesNotExist
raise TemplateDoesNotExist(name)
def load_template(self, template_name, template_dirs=None):
0
Source : filesystem.py
with MIT License
from chunky2808
with MIT License
from chunky2808
def get_template_sources(self, template_name, template_dirs=None):
"""
Return an Origin object pointing to an absolute path in each directory
in template_dirs. For security reasons, if a path doesn't lie inside
one of the template_dirs it is excluded from the result set.
"""
if not template_dirs:
template_dirs = self.get_dirs()
for template_dir in template_dirs:
try:
name = safe_join(template_dir, template_name)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
continue
yield Origin(
name=name,
template_name=template_name,
loader=self,
)
def load_template_source(self, template_name, template_dirs=None):
0
Source : views.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def template_source(request):
"""
Return the source of a template, syntax-highlighted by Pygments if
it's available.
"""
template_origin_name = request.GET.get('template_origin', None)
if template_origin_name is None:
return HttpResponseBadRequest('"template_origin" key is required')
try:
template_origin_name = signing.loads(template_origin_name)
except Exception:
return HttpResponseBadRequest('"template_origin" is invalid')
template_name = request.GET.get('template', template_origin_name)
final_loaders = []
loaders = Engine.get_default().template_loaders
for loader in loaders:
if loader is not None:
# When the loader has loaders associated with it,
# append those loaders to the list. This occurs with
# django.template.loaders.cached.Loader
if hasattr(loader, 'loaders'):
final_loaders += loader.loaders
else:
final_loaders.append(loader)
for loader in final_loaders:
if Origin: # django>=1.9
origin = Origin(template_origin_name)
try:
source = loader.get_contents(origin)
break
except TemplateDoesNotExist:
pass
else: # django < 1.9
try:
source, _ = loader.load_template_source(template_name)
break
except TemplateDoesNotExist:
pass
else:
source = "Template Does Not Exist: %s" % (template_origin_name,)
try:
from pygments import highlight
from pygments.lexers import HtmlDjangoLexer
from pygments.formatters import HtmlFormatter
source = highlight(source, HtmlDjangoLexer(), HtmlFormatter())
source = mark_safe(source)
source.pygmentized = True
except ImportError:
pass
# Using SimpleTemplateResponse avoids running global context processors.
return SimpleTemplateResponse('debug_toolbar/panels/template_source.html', {
'source': source,
'template_name': template_name
})
0
Source : loaders.py
with BSD 3-Clause "New" or "Revised" License
from rgs258
with BSD 3-Clause "New" or "Revised" License
from rgs258
def get_template_sources(self, template_name):
"""
Return an Origin object pointing to an absolute path in each directory
in template_dirs. For security reasons, if a path doesn't lie inside
one of the template_dirs it is excluded from the result set.
"""
if template_name.endswith('.md'):
template_split = template_name.split("/")
template_split.reverse()
template_app_dir = template_split.pop()
template_split.reverse()
for template_dir in self.get_dirs():
if template_dir.endswith(template_app_dir):
try:
name = safe_join(template_dir, *template_split)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
continue
yield Origin(
name=name,
template_name=template_name,
loader=self,
)