mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-05-15 02:55:00 +00:00
A much improved solution to prevent dbtemplates from accidently using dbtemplates when autoloading templates.
This commit is contained in:
parent
aba39a1da6
commit
9b7cd2c16c
2 changed files with 19 additions and 30 deletions
|
|
@ -14,7 +14,12 @@ class DbTemplatesConf(AppConf):
|
|||
AUTO_POPULATE_CONTENT = True
|
||||
MEDIA_PREFIX = None
|
||||
CACHE_BACKEND = None
|
||||
SUBLOADER_NAME = 'loaders'
|
||||
TEMPLATE_LOADERS = None
|
||||
|
||||
def configure_template_loaders(self, value):
|
||||
if value is None:
|
||||
value = getattr(settings, "TEMPLATE_LOADERS", [])
|
||||
return value
|
||||
|
||||
def configure_media_prefix(self, value):
|
||||
if value is None:
|
||||
|
|
|
|||
|
|
@ -1,44 +1,28 @@
|
|||
from django import VERSION
|
||||
from django.template import (Template, TemplateDoesNotExist,
|
||||
TemplateSyntaxError)
|
||||
from django.template.loader import find_template_loader
|
||||
from django.utils.importlib import import_module
|
||||
from dbtemplates.conf import settings
|
||||
|
||||
SUBLOADER_NAME = settings.DBTEMPLATES_SUBLOADER_NAME
|
||||
|
||||
DBTEMPLATES_TEMPLATE_LOADERS = settings.DBTEMPLATES_TEMPLATE_LOADERS
|
||||
loader_cache = None
|
||||
|
||||
def get_loaders():
|
||||
from django.template.loader import template_source_loaders
|
||||
if template_source_loaders is None:
|
||||
try:
|
||||
from django.template.loader import find_template as finder
|
||||
except ImportError:
|
||||
from django.template.loader import find_template_source as finder # noqa
|
||||
try:
|
||||
source, name = finder('test')
|
||||
except TemplateDoesNotExist:
|
||||
pass
|
||||
from django.template.loader import template_source_loaders
|
||||
return template_source_loaders or []
|
||||
|
||||
|
||||
def skip_loader(loader, pattern):
|
||||
if loader.__module__.startswith(pattern):
|
||||
return True
|
||||
if hasattr(loader, SUBLOADER_NAME):
|
||||
subloaders = getattr(loader, SUBLOADER_NAME)
|
||||
if type(subloaders) is not list:
|
||||
subloaders = [subloaders]
|
||||
for subloader in subloaders:
|
||||
return skip_loader(subloader, pattern)
|
||||
return False
|
||||
return False
|
||||
|
||||
global loader_cache
|
||||
if loader_cache is not None:
|
||||
return loader_cache
|
||||
loader_cache = []
|
||||
for loader_name in DBTEMPLATES_TEMPLATE_LOADERS:
|
||||
loader = find_template_loader(loader_name)
|
||||
if loader is not None:
|
||||
loader_cache.append(loader)
|
||||
return loader_cache
|
||||
|
||||
def get_template_source(name):
|
||||
source = None
|
||||
for loader in get_loaders():
|
||||
if skip_loader(loader, 'dbtemplates.'):
|
||||
if loader.__module__.startswith('dbtemplates.'):
|
||||
# Don't give a damn about dbtemplates' own loader or loaders
|
||||
# that use the dbtemplates loader
|
||||
continue
|
||||
|
|
|
|||
Loading…
Reference in a new issue