Skip to content

Commit

Permalink
Code should now mostly follow PEP8 (using Flake8) and tox-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Horst Gutmann committed Mar 29, 2013
1 parent da3c5df commit 6ad0915
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 164 deletions.
13 changes: 7 additions & 6 deletions flatblocks/management/commands/createflatblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
9 changes: 4 additions & 5 deletions flatblocks/management/commands/deleteflatblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
12 changes: 7 additions & 5 deletions flatblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion flatblocks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
45 changes: 29 additions & 16 deletions flatblocks/templatetags/flatblock_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

FlatBlock = models.get_model('flatblocks', 'flatblock')


class BasicFlatBlockWrapper(object):
def prepare(self, parser, token):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -110,15 +116,18 @@ 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
else:
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]
Expand All @@ -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:
Expand Down Expand Up @@ -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,))
Expand All @@ -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
Expand Down
Loading

0 comments on commit 6ad0915

Please sign in to comment.