From 6ad09153e57a121f3836f9a71b69b1f4bcb59078 Mon Sep 17 00:00:00 2001 From: Horst Gutmann Date: Fri, 29 Mar 2013 17:25:51 +0100 Subject: [PATCH] Code should now mostly follow PEP8 (using Flake8) and tox-integration --- .../management/commands/createflatblock.py | 13 +- .../management/commands/deleteflatblock.py | 9 +- flatblocks/models.py | 12 +- flatblocks/settings.py | 3 +- flatblocks/templatetags/flatblock_tags.py | 45 ++- flatblocks/tests.py | 274 +++++++++++------- flatblocks/urls.py | 6 +- flatblocks/views.py | 18 +- test_project/settings.py | 20 +- 9 files changed, 236 insertions(+), 164 deletions(-) diff --git a/flatblocks/management/commands/createflatblock.py b/flatblocks/management/commands/createflatblock.py index 25d92a6..47ceabd 100644 --- a/flatblocks/management/commands/createflatblock.py +++ b/flatblocks/management/commands/createflatblock.py @@ -9,12 +9,13 @@ class Command(BaseCommand): def handle(self, *args, **options): if len(args) != 1: - raise CommandError, "This command requires the slug of the new " \ - "flatblock as its first argument" + raise CommandError("This command requires the slug of the new " + "flatblock as its first argument") slug = args[0] - block = FlatBlock(header="[%s]"%slug, content="Empty flatblock", - slug=slug) + block = FlatBlock(header="[{0}]".format(slug), + content="Empty flatblock", + slug=slug) try: block.save() - except IntegrityError, e: - raise CommandError, "A flatblock with this name already exists" + except IntegrityError: + raise CommandError("A flatblock with this name already exists") diff --git a/flatblocks/management/commands/deleteflatblock.py b/flatblocks/management/commands/deleteflatblock.py index 732d4ee..ce61173 100644 --- a/flatblocks/management/commands/deleteflatblock.py +++ b/flatblocks/management/commands/deleteflatblock.py @@ -8,11 +8,10 @@ class Command(BaseCommand): def handle(self, *args, **options): if len(args) != 1: - raise CommandError, "This command requires the slug of the " \ - "flatblock as its first argument" + raise CommandError("This command requires the slug of the " + "flatblock as its first argument") slug = args[0] try: FlatBlock.objects.get(slug=slug).delete() - except FlatBlock.DoesNotExist, e: - raise CommandError, "The requested flatblock doesn't exist" - + except FlatBlock.DoesNotExist: + raise CommandError("The requested flatblock doesn't exist") diff --git a/flatblocks/models.py b/flatblocks/models.py index fdea5d8..8b270ea 100644 --- a/flatblocks/models.py +++ b/flatblocks/models.py @@ -12,13 +12,15 @@ class FlatBlock(models.Model): title (header) which you can, for example, use in a sidebar of a website. """ slug = models.CharField(max_length=255, unique=True, - verbose_name=_('Slug'), - help_text=_("A unique name used for reference in the templates")) + verbose_name=_('Slug'), + help_text=_("A unique name used for reference in " + "the templates")) header = models.CharField(blank=True, null=True, max_length=255, - verbose_name=_('Header'), - help_text=_("An optional header for this content")) + verbose_name=_('Header'), + help_text=_("An optional header for this " + "content")) content = models.TextField(verbose_name=_('Content'), blank=True, - null=True) + null=True) # Helper attributes used if content should be evaluated in order to # represent the original content. diff --git a/flatblocks/settings.py b/flatblocks/settings.py index 75f8a9e..f2a2614 100644 --- a/flatblocks/settings.py +++ b/flatblocks/settings.py @@ -2,4 +2,5 @@ CACHE_PREFIX = getattr(settings, 'FLATBLOCKS_CACHE_PREFIX', 'flatblocks_') AUTOCREATE_STATIC_BLOCKS = getattr(settings, - 'FLATBLOCKS_AUTOCREATE_STATIC_BLOCKS', False) + 'FLATBLOCKS_AUTOCREATE_STATIC_BLOCKS', + False) diff --git a/flatblocks/templatetags/flatblock_tags.py b/flatblocks/templatetags/flatblock_tags.py index f0762ed..2b4e643 100644 --- a/flatblocks/templatetags/flatblock_tags.py +++ b/flatblocks/templatetags/flatblock_tags.py @@ -56,6 +56,7 @@ FlatBlock = models.get_model('flatblocks', 'flatblock') + class BasicFlatBlockWrapper(object): def prepare(self, parser, token): """ @@ -92,7 +93,12 @@ def prepare(self, parser, token): if args[0] != 'using': # block, timeout, "evaluated" if args[1] != 'evaluated': - raise template.TemplateSyntaxError, "%r tag with two arguments has to include the cache timeout and the evaluated flag" % (tag_name,) + raise template.TemplateSyntaxError("{0!r} tag with two " + "arguments has to " + "include the cache " + "timeout and the " + "evaluated flag".format( + tag_name)) self.cache_time = args[0] self.evaluated = True else: @@ -110,7 +116,9 @@ def prepare(self, parser, token): self.evaluated = True self.tpl_name = args[3] else: - raise template.TemplateSyntaxError, "%r tag should have between 1 and 5 arguments" % (tokens[0],) + raise template.TemplateSyntaxError("{0!r} tag should have between " + "1 and 5 arguments".format( + tokens[0])) # Check to see if the slug is properly double/single quoted if not (self.slug[0] == self.slug[-1] and self.slug[0] in ('"', "'")): self.is_variable = True @@ -118,7 +126,8 @@ def prepare(self, parser, token): self.slug = self.slug[1:-1] # Clean up the template name if self.tpl_name is not None: - if not(self.tpl_name[0] == self.tpl_name[-1] and self.tpl_name[0] in ('"', "'")): + if not(self.tpl_name[0] == self.tpl_name[-1] and + self.tpl_name[0] in ('"', "'")): self.tpl_is_variable = True else: self.tpl_name = self.tpl_name[1:-1] @@ -128,21 +137,25 @@ def prepare(self, parser, token): def __call__(self, parser, token): self.prepare(parser, token) return FlatBlockNode(self.slug, self.is_variable, self.cache_time, - template_name=self.tpl_name, - tpl_is_variable=self.tpl_is_variable, - evaluated=self.evaluated) + template_name=self.tpl_name, + tpl_is_variable=self.tpl_is_variable, + evaluated=self.evaluated) + class PlainFlatBlockWrapper(BasicFlatBlockWrapper): def __call__(self, parser, token): self.prepare(parser, token) - return FlatBlockNode(self.slug, self.is_variable, self.cache_time, False, evaluated=self.evaluated) + return FlatBlockNode(self.slug, self.is_variable, self.cache_time, + False, evaluated=self.evaluated) + do_get_flatblock = BasicFlatBlockWrapper() do_plain_flatblock = PlainFlatBlockWrapper() + class FlatBlockNode(template.Node): def __init__(self, slug, is_variable, cache_time=0, with_template=True, - template_name=None, tpl_is_variable=False, evaluated=False): + template_name=None, tpl_is_variable=False, evaluated=False): if template_name is None: self.template_name = 'flatblocks/flatblock.html' else: @@ -185,17 +198,17 @@ def render(self, context): flatblock = FlatBlock.objects.get(slug=real_slug) else: flatblock, _ = FlatBlock.objects.get_or_create( - slug=real_slug, - defaults = {'content': real_slug} - ) + slug=real_slug, + defaults={'content': real_slug} + ) if self.cache_time != 0: if self.cache_time is None or self.cache_time == 'None': - logger.debug("Caching %s for the cache's default timeout" - % (real_slug,)) + logger.debug("Caching {0} for the cache's default " + "timeout".format(real_slug)) cache.set(cache_key, flatblock) else: - logger.debug("Caching %s for %s seconds" % (real_slug, - str(self.cache_time))) + logger.debug("Caching {0} for {1} seconds".format( + real_slug, str(self.cache_time))) cache.set(cache_key, flatblock, int(self.cache_time)) else: logger.debug("Don't cache %s" % (real_slug,)) @@ -208,7 +221,7 @@ def render(self, context): if self.with_template: tmpl = loader.get_template(real_template) - new_ctx.update({'flatblock':flatblock}) + new_ctx.update({'flatblock': flatblock}) return tmpl.render(new_ctx) else: return flatblock.content diff --git a/flatblocks/tests.py b/flatblocks/tests.py index b90fa18..467c8d5 100644 --- a/flatblocks/tests.py +++ b/flatblocks/tests.py @@ -13,35 +13,41 @@ class BasicTests(TestCase): urls = 'flatblocks.urls' def setUp(self): - self.testblock = FlatBlock.objects.create( - slug='block', - header='HEADER', - content='CONTENT' - ) - self.admin = User.objects.create_superuser('admin', 'admin@localhost', 'adminpwd') + self.testblock = FlatBlock.objects.create(slug='block', + header='HEADER', + content='CONTENT' + ) + self.admin = User.objects.create_superuser('admin', 'admin@localhost', + 'adminpwd') def testURLConf(self): # We have to support two different APIs here (1.1 and 1.2) def get_tmpl(resp): - if isinstance(resp.template, list): - return resp.template[0] - return resp.template - self.assertEquals(get_tmpl(self.client.get('/edit/1/')).name, 'admin/login.html') + if hasattr(resp, 'templates'): + return resp.templates[0] + else: + if isinstance(resp.template, list): + return resp.template[0] + return resp.template + self.assertEqual(get_tmpl(self.client.get('/edit/1/')).name, + 'admin/login.html') self.client.login(username='admin', password='adminpwd') - self.assertEquals(get_tmpl(self.client.get('/edit/1/')).name, 'flatblocks/edit.html') + self.assertEqual(get_tmpl(self.client.get('/edit/1/')).name, + 'flatblocks/edit.html') def testCacheReset(self): """ Tests if FlatBlock.save() resets the cache. """ - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 60 %}') + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" 60 %}') tpl.render(template.Context({})) name = '%sblock' % settings.CACHE_PREFIX - self.assertNotEquals(None, cache.get(name)) + self.assertNotEqual(None, cache.get(name)) block = FlatBlock.objects.get(slug='block') block.header = 'UPDATED' block.save() - self.assertEquals(None, cache.get(name)) + self.assertEqual(None, cache.get(name)) def testSaveKwargs(self): block = FlatBlock(slug='missing') @@ -56,22 +62,22 @@ def testCacheRemoval(self): """ block = FlatBlock(slug="test", content="CONTENT") block.save() - tpl = template.Template('{% load flatblock_tags %}{% flatblock "test" 100 %}') + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "test" 100 %}') # We fill the cache by rendering the block tpl.render(template.Context({})) cache_key = "%stest" % settings.CACHE_PREFIX - self.assertNotEquals(None, cache.get(cache_key)) + self.assertNotEqual(None, cache.get(cache_key)) block.delete() - self.assertEquals(None, cache.get(cache_key)) + self.assertEqual(None, cache.get(cache_key)) class TagTests(TestCase): def setUp(self): - self.testblock = FlatBlock.objects.create( - slug='block', - header='HEADER', - content='CONTENT' - ) + self.testblock = FlatBlock.objects.create(slug='block', + header='HEADER', + content='CONTENT' + ) def testLoadingTaglib(self): """Tests if the taglib defined in this app can be loaded""" @@ -79,126 +85,167 @@ def testLoadingTaglib(self): tpl.render(template.Context({})) def testExistingPlain(self): - tpl = template.Template('{% load flatblock_tags %}{% plain_flatblock "block" %}') + tpl = template.Template( + '{% load flatblock_tags %}{% plain_flatblock "block" %}') self.assertEqual(u'CONTENT', tpl.render(template.Context({})).strip()) def testExistingTemplate(self): expected = """
-

HEADER

+

HEADER

-
CONTENT
+
CONTENT
""" - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" %}') + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" %}') self.assertEqual(expected, tpl.render(template.Context({}))) def testUsingMissingTemplate(self): - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" using "missing_template.html" %}') - exception = template.TemplateSyntaxError + tpl = template.Template( + '{% load flatblock_tags %}' + '{% flatblock "block" using "missing_template.html" %}') + exception = template.base.TemplateDoesNotExist self.assertRaises(exception, tpl.render, template.Context({})) def testSyntax(self): - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" %}') + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block"')) - self.assertEquals('block', node.slug) - self.assertEquals(False, node.evaluated) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block"')) + self.assertEqual('block', node.slug) + self.assertEqual(False, node.evaluated) + + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" 123 %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" 123')) - self.assertEquals('block', node.slug) - self.assertEquals(False, node.evaluated) - self.assertEquals(123, node.cache_time) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" using "flatblocks/flatblock.html" %}') + node = do_get_flatblock(None, template.Token('TOKEN_TEXT', + 'flatblock "block" 123')) + self.assertEqual('block', node.slug) + self.assertEqual(False, node.evaluated) + self.assertEqual(123, node.cache_time) + + tpl = template.Template( + '{% load flatblock_tags %}' + '{% flatblock "block" using "flatblocks/flatblock.html" %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" using "flatblocks/flatblock.html"')) - self.assertEquals('block', node.slug) - self.assertEquals(False, node.evaluated) - self.assertEquals(0, node.cache_time) - self.assertEquals("flatblocks/flatblock.html", node.template_name) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 using "flatblocks/flatblock.html" %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" using ' + '"flatblocks/flatblock.html"')) + self.assertEqual('block', node.slug) + self.assertEqual(False, node.evaluated) + self.assertEqual(0, node.cache_time) + self.assertEqual("flatblocks/flatblock.html", node.template_name) + + tpl = template.Template( + '{% load flatblock_tags %}' + '{% flatblock "block" 123 using "flatblocks/flatblock.html" %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" 123 using "flatblocks/flatblock.html"')) - self.assertEquals('block', node.slug) - self.assertEquals(False, node.evaluated) - self.assertEquals(123, node.cache_time) - self.assertEquals("flatblocks/flatblock.html", node.template_name) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" evaluated %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" 123 using ' + '"flatblocks/flatblock.html"')) + self.assertEqual('block', node.slug) + self.assertEqual(False, node.evaluated) + self.assertEqual(123, node.cache_time) + self.assertEqual("flatblocks/flatblock.html", node.template_name) + + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" evaluated %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" evaluated')) - self.assertEquals('block', node.slug) - self.assertEquals(True, node.evaluated) - self.assertEquals(0, node.cache_time) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" evaluated using "flatblocks/flatblock.html" %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" evaluated')) + self.assertEqual('block', node.slug) + self.assertEqual(True, node.evaluated) + self.assertEqual(0, node.cache_time) + + tpl = template.Template( + '{% load flatblock_tags %}' + '{% flatblock "block" evaluated using ' + '"flatblocks/flatblock.html" %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" evaluated using "flatblocks/flatblock.html"')) - self.assertEquals('block', node.slug) - self.assertEquals(True, node.evaluated) - self.assertEquals(0, node.cache_time) - self.assertEquals("flatblocks/flatblock.html", node.template_name) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 evaluated %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" evaluated ' + 'using ' + '"flatblocks/flatblock.html"')) + self.assertEqual('block', node.slug) + self.assertEqual(True, node.evaluated) + self.assertEqual(0, node.cache_time) + self.assertEqual("flatblocks/flatblock.html", node.template_name) + + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" 123 evaluated %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" 123 evaluated')) - self.assertEquals(123, node.cache_time) - self.assertEquals(True, node.evaluated) - - tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 evaluated using "flatblocks/flatblock.html" %}') + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" 123 ' + 'evaluated')) + self.assertEqual(123, node.cache_time) + self.assertEqual(True, node.evaluated) + + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "block" 123 evaluated ' + 'using "flatblocks/flatblock.html" %}') tpl.render(template.Context({})) - node = do_get_flatblock(None, template.Token('TOKEN_TEXT', 'flatblock "block" 123 evaluated using "flatblocks/flatblock.html"')) - self.assertEquals('block', node.slug) - self.assertEquals(True, node.evaluated) - self.assertEquals(123, node.cache_time) - self.assertEquals("flatblocks/flatblock.html", node.template_name) - + node = do_get_flatblock(None, + template.Token('TOKEN_TEXT', + 'flatblock "block" 123 ' + 'evaluated using ' + '"flatblocks/flatblock.html"')) + self.assertEqual('block', node.slug) + self.assertEqual(True, node.evaluated) + self.assertEqual(123, node.cache_time) + self.assertEqual("flatblocks/flatblock.html", node.template_name) def testBlockAsVariable(self): - tpl = template.Template('{% load flatblock_tags %}{% flatblock blockvar %}') + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock blockvar %}') tpl.render(template.Context({'blockvar': 'block'})) def testContentEvaluation(self): """ - If a block is set in the template to be evaluated the actual content of the block is treated - as a Django template and receives the parent template's context. + If a block is set in the template to be evaluated the actual content of + the block is treated as a Django template and receives the parent + template's context. """ - FlatBlock.objects.create( - slug='tmpl_block', - header='HEADER', - content='{{ variable }}' - ) - tpl = template.Template('{% load flatblock_tags %}{% plain_flatblock "tmpl_block" evaluated %}') + FlatBlock.objects.create(slug='tmpl_block', + header='HEADER', + content='{{ variable }}' + ) + tpl = template.Template( + '{% load flatblock_tags %}' + '{% plain_flatblock "tmpl_block" evaluated %}') result = tpl.render(template.Context({'variable': 'value'})) - self.assertEquals('value', result) + self.assertEqual('value', result) def testDisabledEvaluation(self): """ If "evaluated" is not passed, no evaluation should take place. """ - FlatBlock.objects.create( - slug='tmpl_block', - header='HEADER', - content='{{ variable }}' - ) - tpl = template.Template('{% load flatblock_tags %}{% plain_flatblock "tmpl_block" %}') + FlatBlock.objects.create(slug='tmpl_block', + header='HEADER', + content='{{ variable }}' + ) + tpl = template.Template( + '{% load flatblock_tags %}{% plain_flatblock "tmpl_block" %}') result = tpl.render(template.Context({'variable': 'value'})) - self.assertEquals('{{ variable }}', result) + self.assertEqual('{{ variable }}', result) def testHeaderEvaluation(self): """ Also the header should receive the context and get evaluated. """ - FlatBlock.objects.create( - slug='tmpl_block', - header='{{ header_variable }}', - content='{{ variable }}' - ) - tpl = template.Template('{% load flatblock_tags %}{% flatblock "tmpl_block" evaluated %}') + FlatBlock.objects.create(slug='tmpl_block', + header='{{ header_variable }}', + content='{{ variable }}' + ) + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock "tmpl_block" evaluated %}') result = tpl.render(template.Context({ 'variable': 'value', 'header_variable': 'header-value' @@ -213,27 +260,34 @@ def testMissingStaticBlock(self): """Tests if a missing block with hardcoded name will be auto-created""" expected = """
-
foo
+
foo
""" settings.AUTOCREATE_STATIC_BLOCKS = True - tpl = template.Template('{% load flatblock_tags %}{% flatblock "foo" %}') + 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""" + """ + 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" %}') + 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): + """ + Tests if a missing block with variable name will simply return an empty + string + """ 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()) - - + tpl = template.Template( + '{% load flatblock_tags %}{% flatblock name %}') + self.assertEqual('', + tpl.render(template.Context({'name': 'foo'})).strip()) diff --git a/flatblocks/urls.py b/flatblocks/urls.py index de7df9f..57e2f0b 100644 --- a/flatblocks/urls.py +++ b/flatblocks/urls.py @@ -3,6 +3,6 @@ from flatblocks.views import edit urlpatterns = patterns('', - url('^edit/(?P\d+)/$', staff_member_required(edit), - name='flatblocks-edit') -) + url('^edit/(?P\d+)/$', staff_member_required(edit), + name='flatblocks-edit') + ) diff --git a/flatblocks/views.py b/flatblocks/views.py index eda214e..122b071 100644 --- a/flatblocks/views.py +++ b/flatblocks/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext -from django.http import HttpResponseRedirect, HttpResponseForbidden,\ - HttpResponse +from django.http import HttpResponseRedirect, HttpResponseForbidden +from django.http import HttpResponse from django.utils.translation import ugettext as _ from flatblocks.models import FlatBlock @@ -9,7 +9,7 @@ def edit(request, pk, modelform_class=FlatBlockForm, permission_check=None, - template_name='flatblocks/edit.html', success_url=None): + template_name='flatblocks/edit.html', success_url=None): """ This view provides a simple editor implementation for flatblocks. @@ -20,7 +20,7 @@ def edit(request, pk, modelform_class=FlatBlockForm, permission_check=None, The other entry point helps you check permissions: Pass a simple function via the ``permission_check`` keyword-argument in order to check permissions on the flatblock-level:: - + def my_perm_check(request, flatblock): return request.user.is_staff @@ -42,14 +42,15 @@ def my_perm_check(request, flatblock): if permission_check is not None: permcheck_result = permission_check(request, flatblock) if permcheck_result is False: - return HttpResponseForbidden(_('You are not allowed to edit this flatblock')) + return HttpResponseForbidden( + _('You are not allowed to edit this flatblock')) if isinstance(permcheck_result, HttpResponse): return permcheck_result session_key = 'flatblock.origin.%d' % (int(pk), ) if request.method == 'POST': origin = request.session.get(session_key, - request.META.get('HTTP_REFERER', '/')) + request.META.get('HTTP_REFERER', '/')) form = modelform_class(request.POST, instance=flatblock) if form.is_valid(): instance = form.save(commit=False) @@ -61,11 +62,12 @@ def my_perm_check(request, flatblock): else: origin = request.META.get('HTTP_REFERER', '/') # Don't set origin to this view's url no matter what - origin = origin == request.get_full_path() and request.session.get(session_key, '/') or origin + origin = origin == request.get_full_path() and \ + request.session.get(session_key, '/') or origin form = modelform_class(instance=flatblock) request.session[session_key] = origin return render_to_response(template_name, { 'form': form, 'origin': origin, 'flatblock': flatblock, - }, context_instance=RequestContext(request)) + }, context_instance=RequestContext(request)) diff --git a/test_project/settings.py b/test_project/settings.py index 11142a9..cb969c2 100644 --- a/test_project/settings.py +++ b/test_project/settings.py @@ -1,31 +1,31 @@ import os -import sys -import logging from django.conf.global_settings import * -logging.basicConfig(level=logging.DEBUG) - PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) #sys.path.insert(0, os.path.dirname(PROJECT_ROOT)) -DEBUG=True -TEMPLATE_DEBUG=True -DATABASE_ENGINE = 'sqlite3' -DATABASE_NAME = '/tmp/flatblocks.db' +DEBUG = True +TEMPLATE_DEBUG = True +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(PROJECT_ROOT, 'test.db') + } +} INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', 'flatblocks', - 'south' ) TEMPLATE_CONTEXT_PROCESSORS = TEMPLATE_CONTEXT_PROCESSORS + ( 'django.core.context_processors.request', 'django.contrib.auth.context_processors.auth', ) -LANGUAGE_CODE="en" +LANGUAGE_CODE = "en" TEMPLATE_DIRS = ( os.path.join(PROJECT_ROOT, 'templates'), ) ROOT_URLCONF = 'test_project.urls' +SECRET_KEY = "12345"