Skip to content

Commit

Permalink
Refactored loader to be class based only.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Jul 1, 2011
1 parent a40204d commit 2804cd5
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions dbtemplates/loader.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import warnings
from django import VERSION
from django.conf import settings
from django.contrib.sites.models import Site
from django.template import TemplateDoesNotExist

from dbtemplates.conf import settings
from dbtemplates.models import Template
from dbtemplates.utils import cache, get_cache_key
from dbtemplates.utils.cache import cache, get_cache_key, set_and_return
from django.template.loader import BaseLoader


def load_template_source(template_name, template_dirs=None, annoy=True):
class Loader(BaseLoader):
"""
A custom template loader to load templates from the database.
Expand All @@ -17,43 +16,28 @@ def load_template_source(template_name, template_dirs=None, annoy=True):
it falls back to query the database field ``name`` with the template path
and ``sites`` with the current site.
"""
if VERSION[:2] >= (1, 2) and annoy:
# For backward compatibility
warnings.warn(
"`dbtemplates.loader.load_template_source` is deprecated; "
"use `dbtemplates.loader.Loader` instead.", DeprecationWarning)
site = Site.objects.get_current()
display_name = 'db:%s:%s:%s' % (settings.DATABASE_ENGINE,
template_name, site.domain)
cache_key = get_cache_key(template_name)
if cache:
try:
backend_template = cache.get(cache_key)
if backend_template:
return backend_template, template_name
except:
pass
try:
template = Template.on_site.filter(name__exact=template_name)[0]
# Save in cache backend explicitly if manually deleted or invalidated
if cache:
cache.set(cache_key, template.content)
return (template.content, display_name)
except:
pass
raise TemplateDoesNotExist(template_name)
load_template_source.is_usable = True


if VERSION[:2] >= (1, 2):
# providing a class based loader for Django >= 1.2, yay!
from django.template.loader import BaseLoader
is_usable = True

class Loader(BaseLoader):
__doc__ = load_template_source.__doc__

is_usable = True

def load_template_source(self, template_name, template_dirs=None):
return load_template_source(
template_name, template_dirs, annoy=False)
def load_template_source(self, template_name, template_dirs=None):
site = Site.objects.get_current()
display_name = 'dbtemplates:%s:%s:%s' % (settings.DATABASE_ENGINE,
template_name, site.domain)
cache_key = get_cache_key(template_name)
if cache:
try:
backend_template = cache.get(cache_key)
if backend_template:
return backend_template, template_name
except:
pass
try:
template = Template.objects.get(name__exact=template_name)
return set_and_return(template.content, display_name)
except (Template.MultipleObjectsReturned, Template.DoesNotExist):
try:
template = Template.objects.get(
name__exact=template_name, sites__in=[site.id])
return set_and_return(template.content, display_name)
except Template.DoesNotExist:
pass
raise TemplateDoesNotExist(template_name)

0 comments on commit 2804cd5

Please sign in to comment.