mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-05-13 01:53:13 +00:00
added filesystem-based caching of dbtemplates (configured via settings.DBTEMPLATES_CACHE_DIR). docs and tests still missing.
git-svn-id: https://django-dbtemplates.googlecode.com/svn/trunk@22 cfb8ba98-e953-0410-9cff-959ffddf5974 committer: arne@rcs4u.de <arne@rcs4u.de@cfb8ba98-e953-0410-9cff-959ffddf5974> --HG-- extra : convert_revision : 633aadf3de216f42398f8c57a93d68c28946da3a
This commit is contained in:
parent
d4a634ecb3
commit
dae109748b
1 changed files with 37 additions and 5 deletions
|
|
@ -1,4 +1,7 @@
|
|||
import os
|
||||
from django.conf import settings
|
||||
from django.dispatch import dispatcher
|
||||
from django.db.models import signals
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
|
|
@ -9,16 +12,45 @@ try:
|
|||
except:
|
||||
site = None
|
||||
|
||||
try:
|
||||
cache_dir = os.path.normpath(getattr(settings, 'DBTEMPLATES_CACHE_DIR', None))
|
||||
if not os.path.isdir(cache_dir):
|
||||
raise
|
||||
except:
|
||||
cache_dir = None
|
||||
|
||||
def load_template_source(template_name, template_dirs=None):
|
||||
"""
|
||||
Loads templates from the database by querying the database field ``name``
|
||||
with a template path and ``sites`` with the current site.
|
||||
"""
|
||||
if site is not None:
|
||||
if cache_dir is not None:
|
||||
filepath = os.path.join(cache_dir, template_name)
|
||||
try:
|
||||
t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
|
||||
return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
|
||||
except:
|
||||
pass
|
||||
return (open(filepath).read(), filepath)
|
||||
except IOError:
|
||||
try:
|
||||
t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
|
||||
try:
|
||||
f = open(filepath, 'w')
|
||||
f.write(t.content)
|
||||
f.close()
|
||||
except IOError:
|
||||
pass
|
||||
return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
|
||||
except:
|
||||
pass
|
||||
raise TemplateDoesNotExist, template_name
|
||||
load_template_source.is_usable = True
|
||||
load_template_source.is_usable = True
|
||||
|
||||
def remove_cached_template(instance):
|
||||
try:
|
||||
filepath = os.path.join(cache_dir, instance.name)
|
||||
os.remove(filepath)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
dispatcher.connect(remove_cached_template, sender=Template, signal=signals.post_save)
|
||||
dispatcher.connect(remove_cached_template, sender=Template, signal=signals.pre_delete)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue