diff --git a/dbtemplates/admin.py b/dbtemplates/admin.py index 3109a41..e109003 100644 --- a/dbtemplates/admin.py +++ b/dbtemplates/admin.py @@ -22,8 +22,8 @@ class CodeMirrorTextArea(forms.Textarea): content field of the Template model. """ class Media: - css = dict(screen=[ - posixpath.join(settings.DBTEMPLATES_MEDIA_PREFIX, 'css/editor.css')]) + css = dict(screen=[posixpath.join( + settings.DBTEMPLATES_MEDIA_PREFIX, 'css/editor.css')]) js = [posixpath.join(settings.DBTEMPLATES_MEDIA_PREFIX, 'js/codemirror.js')] def render(self, name, value, attrs=None): diff --git a/dbtemplates/conf.py b/dbtemplates/conf.py index 34357d0..f245983 100644 --- a/dbtemplates/conf.py +++ b/dbtemplates/conf.py @@ -29,8 +29,9 @@ class DbTemplatesSettings(AppSettings): else: return "default" if isinstance(value, basestring) and value.startswith("dbtemplates."): - raise ImproperlyConfigured("Please upgrade to one of the supported " - "backends as defined in the Django docs.") + raise ImproperlyConfigured("Please upgrade to one of the " + "supported backends as defined " + "in the Django docs.") return value def configure_use_reversion(self, value): diff --git a/dbtemplates/loader.py b/dbtemplates/loader.py index 9a73c09..c5cc4a4 100644 --- a/dbtemplates/loader.py +++ b/dbtemplates/loader.py @@ -32,12 +32,13 @@ class Loader(BaseLoader): pass try: template = Template.objects.get(name__exact=template_name) - return set_and_return(template.content, display_name) + return set_and_return(cache_key, 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) + return set_and_return( + cache_key, template.content, display_name) except Template.DoesNotExist: pass raise TemplateDoesNotExist(template_name) diff --git a/dbtemplates/models.py b/dbtemplates/models.py index 2cc4cf2..88eb386 100644 --- a/dbtemplates/models.py +++ b/dbtemplates/models.py @@ -14,7 +14,6 @@ from dbtemplates.utils.cache import add_template_to_cache, remove_cached_templat from dbtemplates.utils.template import get_template_source - class Template(models.Model): """ Defines a template model for use with the database template loader. diff --git a/dbtemplates/tests.py b/dbtemplates/tests.py index df95e54..fb2ca31 100644 --- a/dbtemplates/tests.py +++ b/dbtemplates/tests.py @@ -13,23 +13,29 @@ from dbtemplates.utils.template import get_template_source class DbTemplatesTestCase(TestCase): def setUp(self): - self.site1, created1 = Site.objects.get_or_create(domain="example.com", name="example.com") - self.site2, created2 = Site.objects.get_or_create(domain="example.org", name="example.org") - self.t1, _ = Template.objects.get_or_create(name='base.html', content='base') - self.t2, _ = Template.objects.get_or_create(name='sub.html', content='sub') + self.site1, created1 = Site.objects.get_or_create( + domain="example.com", name="example.com") + self.site2, created2 = Site.objects.get_or_create( + domain="example.org", name="example.org") + self.t1, _ = Template.objects.get_or_create( + name='base.html', content='base') + self.t2, _ = Template.objects.get_or_create( + name='sub.html', content='sub') self.t2.sites.add(self.site2) def test_basiscs(self): self.assertEqual(list(self.t1.sites.all()), [self.site1]) self.assertTrue("base" in self.t1.content) - self.assertEqual(list(Template.objects.filter(sites=self.site1)), [self.t1, self.t2]) + self.assertEqual(list(Template.objects.filter(sites=self.site1)), + [self.t1, self.t2]) self.assertEqual(list(self.t2.sites.all()), [self.site1, self.site2]) def test_empty_sites(self): old_add_default_site = settings.DBTEMPLATES_ADD_DEFAULT_SITE try: settings.DBTEMPLATES_ADD_DEFAULT_SITE = False - self.t3 = Template.objects.create(name='footer.html', content='footer') + self.t3 = Template.objects.create( + name='footer.html', content='footer') self.assertEqual(list(self.t3.sites.all()), []) finally: settings.DBTEMPLATES_ADD_DEFAULT_SITE = old_add_default_site diff --git a/dbtemplates/utils/cache.py b/dbtemplates/utils/cache.py index f93410f..611a490 100644 --- a/dbtemplates/utils/cache.py +++ b/dbtemplates/utils/cache.py @@ -16,12 +16,13 @@ def get_cache_key(name): return 'dbtemplates::%s::%s' % (name, current_site.pk) -def set_and_return(content, display_name): +def set_and_return(cache_key, content, display_name): # Save in cache backend explicitly if manually deleted or invalidated if cache: cache.set(cache_key, content) return (content, display_name) + def add_template_to_cache(instance, **kwargs): """ Called via Django's signals to cache the templates, if the template diff --git a/dbtemplates/utils/settings.py b/dbtemplates/utils/settings.py index 221e58d..0431888 100644 --- a/dbtemplates/utils/settings.py +++ b/dbtemplates/utils/settings.py @@ -98,7 +98,8 @@ class AppSettings(object): if setting == setting.upper(): prefixed = "%s_%s" % (prefix.upper(), setting.upper()) configured_value = getattr(settings, prefixed, class_value) - callback = getattr(self, "configure_%s" % setting.lower(), None) + callback_name = "configure_%s" % setting.lower() + callback = getattr(self, callback_name, None) if callable(callback): configured_value = callback(configured_value) delattr(self.__class__, setting)