2025-06-11 04:17:05 +00:00
|
|
|
from django.template import Template, TemplateDoesNotExist, TemplateSyntaxError
|
2011-04-11 17:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_loaders():
|
2016-09-20 08:31:18 +00:00
|
|
|
from django.template.loader import _engine_list
|
2025-06-11 04:17:05 +00:00
|
|
|
|
2016-09-20 08:31:18 +00:00
|
|
|
loaders = []
|
|
|
|
|
for engine in _engine_list():
|
|
|
|
|
loaders.extend(engine.engine.template_loaders)
|
|
|
|
|
return loaders
|
2015-04-28 09:16:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_template_source(name):
|
|
|
|
|
source = None
|
2025-06-11 04:16:03 +00:00
|
|
|
not_found = []
|
2015-04-28 09:16:06 +00:00
|
|
|
for loader in get_loaders():
|
2025-06-11 04:17:05 +00:00
|
|
|
if loader.__module__.startswith("dbtemplates."):
|
2015-04-28 09:16:06 +00:00
|
|
|
# Don't give a damn about dbtemplates' own loader.
|
|
|
|
|
continue
|
2017-12-09 18:38:42 +00:00
|
|
|
for origin in loader.get_template_sources(name):
|
|
|
|
|
try:
|
|
|
|
|
source = loader.get_contents(origin)
|
2025-06-11 04:16:03 +00:00
|
|
|
except (NotImplementedError, TemplateDoesNotExist) as exc:
|
|
|
|
|
if exc.args[0] not in not_found:
|
|
|
|
|
not_found.append(exc.args[0])
|
2017-12-09 18:38:42 +00:00
|
|
|
continue
|
2025-06-11 04:16:03 +00:00
|
|
|
else:
|
2015-04-28 09:16:06 +00:00
|
|
|
return source
|
2025-06-11 04:16:03 +00:00
|
|
|
raise TemplateDoesNotExist(name, chain=not_found)
|
2011-08-02 19:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_template_syntax(template):
|
|
|
|
|
try:
|
|
|
|
|
Template(template.content)
|
2015-08-19 09:42:39 +00:00
|
|
|
except TemplateSyntaxError as e:
|
2011-08-02 19:11:28 +00:00
|
|
|
return (False, e)
|
|
|
|
|
return (True, None)
|