Made loader and cache backends site-aware. The filesystem cache backend now saves the files under <dir>/<site_domain>/<file_name>. The Django cache backend the Site id in the cache key. Template is now saved explicitly to backend if not existent in cache (e.g. if deleted manually or invalidated). Bumped version to 0.5.4.

git-svn-id: https://django-dbtemplates.googlecode.com/svn/trunk@88 cfb8ba98-e953-0410-9cff-959ffddf5974

committer: leidel <leidel@cfb8ba98-e953-0410-9cff-959ffddf5974>

--HG--
extra : convert_revision : 564571948adb06cdda915b665aa61d00b3118ed0
This commit is contained in:
leidel 2008-12-26 17:39:48 +00:00
parent 7fafcbd72b
commit b7011dce7e
7 changed files with 24 additions and 10 deletions

View file

@ -1,2 +1,2 @@
VERSION = (0, 5, 3)
VERSION = (0, 5, 4)
__version__ = '.'.join(map(str, VERSION))

View file

@ -40,7 +40,7 @@ class DjangoCacheBackend(BaseCacheBackend):
A cache backend that uses Django's cache mechanism.
"""
def _cache_key(self, name):
return 'dbtemplates::%s' % name
return 'dbtemplates::%s::%s' % (name, self.site.pk)
def load(self, name):
cache_key = self._cache_key(name)
@ -65,11 +65,11 @@ class FileSystemBackend(BaseCacheBackend):
if not os.path.isdir(self.cache_dir):
pass
except:
raise ImproperlyConfigured('You\'re using the dbtemplates\' file system cache backend without having set the DBTEMPLATES_CACHE_DIR setting to a valid value. Make sure the directory exists and is writeable for the user your Django instance is running with.')
raise ImproperlyConfigured("You're using the dbtemplates file system cache backend without having set the DBTEMPLATES_CACHE_DIR setting to a valid value. Make sure the directory exists and is writeable for the user your Django instance is running with.")
super(FileSystemBackend, self).__init__()
def _filepath(self, name):
return os.path.join(self.cache_dir, name)
return os.path.join(self.cache_dir, self.site.domain, name)
def load(self, name):
try:

View file

@ -1,5 +1,6 @@
import os
from django.conf import settings
from django.contrib.sites.models import Site
from django.template import TemplateDoesNotExist
from django.core.exceptions import ImproperlyConfigured
@ -12,17 +13,21 @@ def load_template_source(template_name, template_dirs=None):
it falls back to query the database field ``name`` with the template path
and ``sites`` with the current site.
"""
display_name = 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name)
site = Site.objects.get_current()
display_name = 'db:%s:%s:%s' % (settings.DATABASE_ENGINE,
template_name, site.domain)
if backend:
try:
backend_template = backend.load(template_name)
if backend_template is not None:
if backend_template:
return backend_template, template_name
except:
pass
try:
template = Template.objects.get(name__exact=template_name,
sites__pk=settings.SITE_ID)
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)
return (template.content, display_name)
except:
pass

View file

@ -44,7 +44,7 @@ class Command(NoArgsCommand):
path = os.path.join(dirpath, f)
name = path.split(templatedir)[1][1:]
try:
t = Template.objects.get(name__exact=name)
t = Template.on_site.get(name__exact=name)
except Template.DoesNotExist:
if force == False:
confirm = raw_input(

View file

@ -4,6 +4,7 @@ from django.db import models
from django.conf import settings
from django.db.models import signals
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.utils.translation import gettext_lazy as _
from django.template import TemplateDoesNotExist
from django.template.loader import find_template_source
@ -20,6 +21,9 @@ class Template(models.Model):
creation_date = models.DateTimeField(_('creation date'), default=datetime.now)
last_changed = models.DateTimeField(_('last changed'), default=datetime.now)
objects = models.Manager()
on_site = CurrentSiteManager('sites')
class Meta:
db_table = 'django_template'
verbose_name = _('template')

View file

@ -82,3 +82,8 @@ INSTALLED_APPS = (
'django.contrib.flatpages',
'dbtemplates',
)
# Uncomment the following two settings to use the file system cache backend.
# It will cache in the directory "cache" inside the example project directory.
# DBTEMPLATES_CACHE_BACKEND = "dbtemplates.cache.FileSystemBackend"
# DBTEMPLATES_CACHE_DIR = "cache"

View file

@ -8,7 +8,7 @@ setup(
author='Jannis Leidel',
author_email='jannis@leidel.info',
url='http://github.com/jezdez/django-dbtemplates/wikis/',
download_url='http://github.com/jezdez/django-dbtemplates/zipball/0.5.3',
download_url='http://github.com/jezdez/django-dbtemplates/zipball/0.5.4',
packages=[
'dbtemplates',
'dbtemplates.management',