2007-07-20 14:30:48 +00:00
|
|
|
from django.conf import settings
|
2008-12-26 17:39:48 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2007-07-20 14:30:48 +00:00
|
|
|
from django.template import TemplateDoesNotExist
|
|
|
|
|
|
2008-11-02 00:05:38 +00:00
|
|
|
from dbtemplates.models import Template, backend
|
2007-07-20 14:52:00 +00:00
|
|
|
|
2007-07-20 14:30:48 +00:00
|
|
|
def load_template_source(template_name, template_dirs=None):
|
|
|
|
|
"""
|
2008-11-02 00:05:48 +00:00
|
|
|
Tries to load the template from the dbtemplates cache backend specified
|
|
|
|
|
by the DBTEMPLATES_CACHE_BACKEND setting. If it does not find a template
|
|
|
|
|
it falls back to query the database field ``name`` with the template path
|
|
|
|
|
and ``sites`` with the current site.
|
2008-04-17 10:44:20 +00:00
|
|
|
"""
|
2008-12-26 17:39:48 +00:00
|
|
|
site = Site.objects.get_current()
|
|
|
|
|
display_name = 'db:%s:%s:%s' % (settings.DATABASE_ENGINE,
|
|
|
|
|
template_name, site.domain)
|
2008-11-02 00:05:38 +00:00
|
|
|
if backend:
|
2008-04-17 10:44:20 +00:00
|
|
|
try:
|
2008-11-02 00:05:38 +00:00
|
|
|
backend_template = backend.load(template_name)
|
2008-12-26 17:39:48 +00:00
|
|
|
if backend_template:
|
2008-11-02 00:05:38 +00:00
|
|
|
return backend_template, template_name
|
|
|
|
|
except:
|
2008-04-17 10:44:20 +00:00
|
|
|
pass
|
2008-11-02 00:05:38 +00:00
|
|
|
try:
|
2008-12-26 17:39:48 +00:00
|
|
|
template = Template.on_site.get(name__exact=template_name)
|
|
|
|
|
# Save in cache backend explicitly if manually deleted or invalidated
|
|
|
|
|
if backend:
|
|
|
|
|
backend.save(template_name, template.content)
|
2008-11-02 00:05:38 +00:00
|
|
|
return (template.content, display_name)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
raise TemplateDoesNotExist, template_name
|
|
|
|
|
load_template_source.is_usable = True
|