Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
refactored loader and wrap normal loaders to not clash between file t…
Browse files Browse the repository at this point in the history
…emplates and submitted templates

Sign-Off: Rolando Espinoza La fuente
  • Loading branch information
rmax committed Aug 15, 2010
1 parent 042c802 commit 9cfdcd1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
7 changes: 4 additions & 3 deletions django-templator/templator/conf/settings.py
Expand Up @@ -35,9 +35,10 @@
)

TEMPLATE_LOADERS = (
'templator.plantillas.loader.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
('templator.plantillas.loader.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)),
)

INSTALLED_APPS = (
Expand Down
54 changes: 33 additions & 21 deletions django-templator/templator/plantillas/loader.py
@@ -1,10 +1,10 @@
import contextlib
import threading

from django.db.models import ObjectDoesNotExist
from django.template import TemplateDoesNotExist
from django.template.loader import (BaseLoader,
get_template_from_string,
find_template_loader,
make_origin)

from templator.plantillas.models import Template as TemplateModel
Expand All @@ -24,32 +24,44 @@ def set_context(**kwargs):
class Loader(BaseLoader):
is_usable = True

def load_template(self, template_name, template_dirs=None):
source, path = self.load_template_source(template_name, template_dirs)
def __init__(self, loaders):
self._loaders = loaders
self._cached_loaders = []

origin = make_origin(path, self.load_template_source, path, None)
return get_template_from_string(source, origin, path), None
@property
def loaders(self):
if not self._cached_loaders:
for loader in self._loaders:
self._cached_loaders.append(find_template_loader(loader))
return self._cached_loaders

def load_template_source(self, template_name, template_dirs=None):
# fetch template by uuid or content
template_uuid = self.get_template_uuid()
template_content = self.get_template_content()
def load_template(self, template_name, template_dirs=None):
uuid = self.get_template_uuid()
if uuid:
# load from db only
try:
tpl = TemplateModel.objects.get(group_uuid=uuid, path=template_name)
except TemplateModel.DoesNotExist:
raise TemplateDoesNotExist(template_name)
else:
origin = make_origin(tpl.path, self, template_name,
template_dirs)
return tpl.content, origin
else:
# use normal loaders
return self.find_template(template_name, template_dirs)

if template_uuid:
def find_template(self, template_name, template_dirs=None):
for loader in self.loaders:
try:
tpl = TemplateModel.objects.get(group_uuid=template_uuid,
path=template_name)
return tpl.content, tpl.path
except ObjectDoesNotExist:
template, display_name = loader(template_name, template_dirs)
origin = make_origin(display_name, loader,
template_name, template_dirs)
return template, origin
except TemplateDoesNotExist:
pass
elif template_content:
return template_content, None

raise TemplateDoesNotExist
raise TemplateDoesNotExist(template_name)

def get_template_uuid(self):
return getattr(thread_context, 'template_uuid', None)

def get_template_content(self):
return getattr(thread_context, 'template_content', None)

0 comments on commit 9cfdcd1

Please sign in to comment.