From 88b23340408306299605a34a7b14561737b7403d Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Wed, 17 Nov 2010 11:05:28 -0500 Subject: [PATCH] Switched template loading from Template.loader to philo.loaders.database.Loader in keeping with Django's recommended way of implementing loaders. Note that the origin of the template is considered to be the actual instance instead of a path, as this seems more strictly accurate. The instance is converted to unicode by the loader to more correctly fit the expected "display_name". Removed origin and django_template properties from templates. Corrected a minor error in the node_url templatetag for contexts not containing 'node'. --- __init__.py | 15 +++++++++++++-- loaders/__init__.py | 0 loaders/database.py | 15 +++++++++++++++ models/pages.py | 24 ++++-------------------- templatetags/nodes.py | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 loaders/__init__.py create mode 100644 loaders/database.py diff --git a/__init__.py b/__init__.py index 52956f3..ba78dda 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,15 @@ -from philo.models.pages import Template +from philo.loaders.database import Loader -load_template_source = Template.loader +_loader = Loader() + + +def load_template_source(template_name, template_dirs=None): + # For backwards compatibility + import warnings + warnings.warn( + "'philo.load_template_source' is deprecated; use 'philo.loaders.database.Loader' instead.", + PendingDeprecationWarning + ) + return _loader.load_template_source(template_name, template_dirs) +load_template_source.is_usable = True diff --git a/loaders/__init__.py b/loaders/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/loaders/database.py b/loaders/database.py new file mode 100644 index 0000000..141aedd --- /dev/null +++ b/loaders/database.py @@ -0,0 +1,15 @@ +from django.template import TemplateDoesNotExist +from django.template.loader import BaseLoader +from django.utils.encoding import smart_unicode +from philo.models import Template + + +class Loader(BaseLoader): + is_usable=True + + def load_template_source(self, template_name, template_dirs=None): + try: + template = Template.objects.get_with_path(template_name) + except Template.DoesNotExist: + raise TemplateDoesNotExist(template_name) + return (template.code, smart_unicode(template)) \ No newline at end of file diff --git a/models/pages.py b/models/pages.py index fddb0ae..81b84c9 100644 --- a/models/pages.py +++ b/models/pages.py @@ -21,14 +21,6 @@ class Template(TreeModel): mimetype = models.CharField(max_length=255, default=getattr(settings, 'DEFAULT_CONTENT_TYPE', 'text/html')) code = TemplateField(secure=False, verbose_name='django template code') - @property - def origin(self): - return 'philo.models.Template: ' + self.path - - @property - def django_template(self): - return DjangoTemplate(self.code) - @property def containers(self): """ @@ -41,7 +33,7 @@ class Template(TreeModel): if isinstance(node, ContainerNode): nodes.append(node) - all_nodes = nodelist_crawl(self.django_template.nodelist, process_node) + all_nodes = nodelist_crawl(DjangoTemplate(self.code).nodelist, process_node) contentlet_node_names = set([node.name for node in all_nodes if not node.references]) contentreference_node_names = [] contentreference_node_specs = [] @@ -54,15 +46,6 @@ class Template(TreeModel): def __unicode__(self): return self.get_path(pathsep=u' › ', field='name') - @staticmethod - @fattr(is_usable=True) - def loader(template_name, template_dirs=None): # load_template_source - try: - template = Template.objects.get_with_path(template_name) - except Template.DoesNotExist: - raise TemplateDoesNotExist(template_name) - return (template.code, template.origin) - class Meta: app_label = 'philo' @@ -84,13 +67,14 @@ class Page(View): context = {} context.update(extra_context or {}) context.update({'page': self, 'attributes': self.attributes}) + template = DjangoTemplate(self.template.code) if request: context.update({'node': request.node, 'attributes': self.attributes_with_node(request.node)}) page_about_to_render_to_string.send(sender=self, request=request, extra_context=context) - string = self.template.django_template.render(RequestContext(request, context)) + string = template.render(RequestContext(request, context)) else: page_about_to_render_to_string.send(sender=self, request=request, extra_context=context) - string = self.template.django_template.render(Context(context)) + string = template.render(Context(context)) page_finished_rendering_to_string.send(sender=self, string=string) return string diff --git a/templatetags/nodes.py b/templatetags/nodes.py index 8a98630..338ac2d 100644 --- a/templatetags/nodes.py +++ b/templatetags/nodes.py @@ -25,7 +25,7 @@ class NodeURLNode(template.Node): if self.node: node = self.node.resolve(context) else: - node = context['node'] + node = context.get('node', None) if not node: return settings.TEMPLATE_STRING_IF_INVALID -- 2.20.1