From b7011dce7e00a98850fa09cd0b76b6555dea1056 Mon Sep 17 00:00:00 2001 From: leidel Date: Fri, 26 Dec 2008 17:39:48 +0000 Subject: [PATCH] Made loader and cache backends site-aware. The filesystem cache backend now saves the files under //. 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 --HG-- extra : convert_revision : 564571948adb06cdda915b665aa61d00b3118ed0 --- dbtemplates/__init__.py | 2 +- dbtemplates/cache.py | 6 +++--- dbtemplates/loader.py | 13 +++++++++---- dbtemplates/management/commands/sync_templates.py | 2 +- dbtemplates/models.py | 4 ++++ example/settings.py | 5 +++++ setup.py | 2 +- 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/dbtemplates/__init__.py b/dbtemplates/__init__.py index 3c9dcb6..818c5fb 100644 --- a/dbtemplates/__init__.py +++ b/dbtemplates/__init__.py @@ -1,2 +1,2 @@ -VERSION = (0, 5, 3) +VERSION = (0, 5, 4) __version__ = '.'.join(map(str, VERSION)) \ No newline at end of file diff --git a/dbtemplates/cache.py b/dbtemplates/cache.py index 39ad583..b407a98 100644 --- a/dbtemplates/cache.py +++ b/dbtemplates/cache.py @@ -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: diff --git a/dbtemplates/loader.py b/dbtemplates/loader.py index 2503091..a4cf0db 100644 --- a/dbtemplates/loader.py +++ b/dbtemplates/loader.py @@ -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 diff --git a/dbtemplates/management/commands/sync_templates.py b/dbtemplates/management/commands/sync_templates.py index c6a2aff..2743c46 100644 --- a/dbtemplates/management/commands/sync_templates.py +++ b/dbtemplates/management/commands/sync_templates.py @@ -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( diff --git a/dbtemplates/models.py b/dbtemplates/models.py index 18d3e12..2f891ff 100644 --- a/dbtemplates/models.py +++ b/dbtemplates/models.py @@ -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') diff --git a/example/settings.py b/example/settings.py index 87555f6..dc39774 100644 --- a/example/settings.py +++ b/example/settings.py @@ -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" diff --git a/setup.py b/setup.py index d31517f..47f6c9d 100644 --- a/setup.py +++ b/setup.py @@ -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',