Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added setting for the block autocreation feature

  • Loading branch information...
commit 88f41616831349364ad2bd06d0e3baf830b27154 1 parent 8f9aa41
@zerok authored
View
3  AUTHORS.txt
@@ -12,4 +12,5 @@ Contributions by
* Michael Greene <http://michaelgreene.org/>
* Stephan Jaekel <http://www.sjaekel.com/>
* James O'Donnell <http://squarefactor.com/words/>
-
+ * Mikhail Korobov <http://github.com/kmike>
+
View
4 README.rst
@@ -136,6 +136,10 @@ the `django-better-chunks`_ fork (``django.contrib.site``- and i18n-support).
Releases
--------
+0.4 (unreleased)
+ * FlatBlock autocreation by Mikhail Korobov (can be enabled/disabled
+ with FLATBLOCKS\_AUTOCREATE\_STATIC\_BLOCKS setting)
+
0.3.5
* Russian translation by Mikhail Korobov
View
2  flatblocks/settings.py
@@ -1,3 +1,5 @@
from django.conf import settings
CACHE_PREFIX = getattr(settings, 'FLATBLOCKS_CACHE_PREFIX', 'flatblocks_')
+AUTOCREATE_STATIC_BLOCKS = getattr(settings,
+ 'FLATBLOCKS_AUTOCREATE_STATIC_BLOCKS', False)
View
8 flatblocks/templatetags/flatblock_tags.py
@@ -44,7 +44,7 @@
from django.db import models
from django.core.cache import cache
-from flatblocks.settings import CACHE_PREFIX
+from flatblocks import settings
register = template.Library()
@@ -142,13 +142,15 @@ def render(self, context):
new_ctx = template.Context({})
new_ctx.update(context)
try:
- cache_key = CACHE_PREFIX + real_slug
+ cache_key = settings.CACHE_PREFIX + real_slug
flatblock = cache.get(cache_key)
if flatblock is None:
# if flatblock's slug is hard-coded in template then it is
# safe and convenient to auto-create block if it doesn't exist.
- if self.is_variable:
+ # This behaviour can be configured using the
+ # FLATBLOCKS_AUTOCREATE_STATIC_BLOCKS setting
+ if self.is_variable or not settings.AUTOCREATE_STATIC_BLOCKS:
flatblock = FlatBlock.objects.get(slug=real_slug)
else:
flatblock, _ = FlatBlock.objects.get_or_create(
View
14 flatblocks/tests.py
@@ -5,7 +5,7 @@
from django import db
from flatblocks.models import FlatBlock
-from flatblocks.settings import CACHE_PREFIX
+from flatblocks import settings
class BasicTests(TestCase):
@@ -35,7 +35,7 @@ def testCacheReset(self):
"""
tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 60 %}')
tpl.render(template.Context({}))
- name = '%sblock' % CACHE_PREFIX
+ name = '%sblock' % settings.CACHE_PREFIX
self.assertNotEquals(None, cache.get(name))
block = FlatBlock.objects.get(slug='block')
block.header = 'UPDATED'
@@ -107,13 +107,23 @@ def testMissingStaticBlock(self):
<div class="content">foo</div>
</div>"""
+ settings.AUTOCREATE_STATIC_BLOCKS = True
tpl = template.Template('{% load flatblock_tags %}{% flatblock "foo" %}')
self.assertEqual(expected, tpl.render(template.Context({})).strip())
self.assertEqual(FlatBlock.objects.count(), 1)
self.assertEqual(expected, tpl.render(template.Context({})).strip())
self.assertEqual(FlatBlock.objects.count(), 1)
+ def testNotAutocreatedMissingStaticBlock(self):
+ """Tests if a missing block with hardcoded name won't be auto-created if feature is disabled"""
+ expected = u""
+ settings.AUTOCREATE_STATIC_BLOCKS = False
+ tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" %}')
+ self.assertEqual(expected, tpl.render(template.Context({})).strip())
+ self.assertEqual(FlatBlock.objects.filter(slug='block').count(), 0)
+
def testMissingVariableBlock(self):
+ settings.AUTOCREATE_STATIC_BLOCKS = True
"""Tests if a missing block with variable name will simply return an empty string"""
tpl = template.Template('{% load flatblock_tags %}{% flatblock name %}')
self.assertEqual('', tpl.render(template.Context({'name': 'foo'})).strip())
Please sign in to comment.
Something went wrong with that request. Please try again.