Commits

Jeffrey Gelens committed b7eef61

Cleanup

Comments (0)

Files changed (3)

-from django.template import Library, TemplateSyntaxError, Node, NodeList
 from django.db import models
-from django.db.models import Q
+from django.utils.translation import ugettext_lazy as _
 
+class TextBrick(models.Model):
+    """Example brick model. lang_code and slug are required."""
+    title = models.CharField(_('title'), max_length=255);
+    content = models.TextField(_('content'), blank=True)
+    photo = models.ImageField(_('photo'), upload_to="brick/%Y%m%d", blank=True)
+    lang_code = models.CharField(_("language"), max_length=2)
 
-register = Library()
-
-def parse_wall(parser, token):
-    try:
-        _, model = token.split_contents()
-    except ValueError:
-        raise TemplateSyntaxError, "'wall' tag requires one argument"
-
-    if model[0] == model[-1] and model[0] in ('"', '\''):
-        nodelist = parser.parse(('endwall',))
-        parser.delete_first_token()
-        return BrickNode(nodelist, model[1:-1])
-    else:
-        raise TemplateSyntaxError, "'wall' tag's argument should be quoted"
-
-register.tag("wall", parse_wall)
-
-
-class BrickNode(Node):
-    def __init__(self, nodelist, model):
-        self.nodelist = nodelist
-        self.model = model
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-        nodelist = NodeList()
-
-        for brick_values in bricks.values():
-            context.update(brick_values)
-            nodelist.append(self.nodelist.render(context))
-
-        return nodelist.render(context)
-
-
-def parse_wall_with_slug(parser, token):
-    try:
-        _, model, slug = token.split_contents()
-    except ValueError:
-        raise TemplateSyntaxError, "'brick' tag requires two arguments"
-
-    if model[0] == model[-1] and model[0] in ('"', '\'') and \
-            slug[0] == slug[-1] and slug[0] in ('"', '\''):
-        nodelist = parser.parse(('endbrick',))
-        parser.delete_first_token()
-        return BrickSlugNode(nodelist, model[1:-1], slug[1:-1])
-    else:
-        raise TemplateSyntaxError, "'brick' tag's arguments should be quoted"
-
-register.tag("brick", parse_wall_with_slug)
-
-
-class BrickSlugNode(Node):
-    def __init__(self, nodelist, model, slug):
-        self.nodelist = nodelist
-        self.model = model
-        self.slug = slug
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(slug = self.slug),
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-        nodelist = NodeList()
-
-        for brick_values in bricks.values():
-            context.update(brick_values)
-            nodelist.append(self.nodelist.render(context))
-
-        return nodelist.render(context)
-
-
-def single_brick(parser, token):
-    try:
-       template_tag, model, slug, sep, as_varname = token.split_contents()
-    except ValueError:
-        raise TemplateSyntaxError, "%r tag requires three arguments" % token.contents.split()[0]
-
-    if model[0] == model[-1] and model[0] in ('"', '\'') and \
-            slug[0] == slug[-1] and slug[0] in ('"', '\''):
-        return BrickAsNode(model[1:-1], slug[1:-1], as_varname)
-    else:
-        raise TemplateSyntaxError, "%r tag's argument should be quoted" % model
-
-register.tag("single_brick", single_brick)
-
-
-class BrickAsNode(Node):
-    def __init__(self, model, slug, as_varname=None):
-        self.model = model
-        self.slug = slug
-        self.as_varname = as_varname
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(slug = self.slug),
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-
-        if bricks:
-            context[self.as_varname] = bricks[0]
-            return ''
-        else:
-            return '(empty)'
+    def __unicode__(self):
+        return self.title

bricks/templatetags/bricks.py

 
 register = Library()
 
+
+class BrickBase(Node):
+    """Gets brick based on some queryset."""
+
+    def __init__(self, nodelist, model):
+        self.nodelist = nodelist
+        self.model = model
+
+    def get_queryset(self):
+        return None
+
+    def render(self, context):
+        nodelist = NodeList()
+        bricks = self.get_queryset(context)
+
+        for brick_values in bricks.values():
+            context.update(brick_values)
+            nodelist.append(self.nodelist.render(context))
+
+        return nodelist.render(context)
+
+
+class BrickNode(BrickBase):
+    """Gets brick based on model and lang_code."""
+
+    def get_queryset(self, context):
+        brick = models.get_model("bricks", self.model)
+
+        return brick.objects.filter(
+            Q(lang_code = context['LANGUAGE_CODE']) |
+            Q(lang_code = "")
+        )
+
+
+class BrickSlugNode(BrickBase):
+    """Gets brick based on model and lang_code."""
+
+    def __init__(self, nodelist, model, slug):
+        super(BrickSlugNode, self).__init__(nodelist, model)
+        self.slug = slug
+
+    def get_queryset(self, context):
+        brick = models.get_model("bricks", self.model)
+
+        return brick.objects.filter(
+            Q(slug = self.slug),
+            Q(lang_code = context['LANGUAGE_CODE']) |
+            Q(lang_code = "")
+        )
+
+
+class BrickAsNode(BrickSlugNode):
+    """Gets brick and stores in given context variable."""
+
+    def __init__(self, model, slug, as_varname=None):
+        self.model = model
+        self.slug = slug
+        self.as_varname = as_varname
+
+    def render(self, context):
+        bricks = self.get_queryset(context)
+
+        if bricks:
+            context[self.as_varname] = bricks[0]
+            return ''
+        else:
+            return '(empty)'
+
+
+@register.tag(name="wall")
 def parse_wall(parser, token):
+    """
+    """
     try:
         _, model = token.split_contents()
     except ValueError:
     if model[0] == model[-1] and model[0] in ('"', '\''):
         nodelist = parser.parse(('endwall',))
         parser.delete_first_token()
+
         return BrickNode(nodelist, model[1:-1])
     else:
         raise TemplateSyntaxError, "'wall' tag's argument should be quoted"
 
-register.tag("wall", parse_wall)
 
-
-class BrickNode(Node):
-    def __init__(self, nodelist, model):
-        self.nodelist = nodelist
-        self.model = model
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-        nodelist = NodeList()
-
-        for brick_values in bricks.values():
-            context.update(brick_values)
-            nodelist.append(self.nodelist.render(context))
-
-        return nodelist.render(context)
-
-
+@register.tag(name="brick")
 def parse_wall_with_slug(parser, token):
     try:
         _, model, slug = token.split_contents()
 
     if model[0] == model[-1] and model[0] in ('"', '\'') and \
             slug[0] == slug[-1] and slug[0] in ('"', '\''):
+
         nodelist = parser.parse(('endbrick',))
         parser.delete_first_token()
+
         return BrickSlugNode(nodelist, model[1:-1], slug[1:-1])
     else:
         raise TemplateSyntaxError, "'brick' tag's arguments should be quoted"
 
-register.tag("brick", parse_wall_with_slug)
 
-
-class BrickSlugNode(Node):
-    def __init__(self, nodelist, model, slug):
-        self.nodelist = nodelist
-        self.model = model
-        self.slug = slug
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(slug = self.slug),
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-        nodelist = NodeList()
-
-        for brick_values in bricks.values():
-            context.update(brick_values)
-            nodelist.append(self.nodelist.render(context))
-
-        return nodelist.render(context)
-
-
+@register.tag(name="single_brick")
 def single_brick(parser, token):
     try:
        template_tag, model, slug, sep, as_varname = token.split_contents()
 
     if model[0] == model[-1] and model[0] in ('"', '\'') and \
             slug[0] == slug[-1] and slug[0] in ('"', '\''):
+
         return BrickAsNode(model[1:-1], slug[1:-1], as_varname)
     else:
         raise TemplateSyntaxError, "%r tag's argument should be quoted" % model
-
-register.tag("single_brick", single_brick)
-
-
-class BrickAsNode(Node):
-    def __init__(self, model, slug, as_varname=None):
-        self.model = model
-        self.slug = slug
-        self.as_varname = as_varname
-
-    def render(self, context):
-        CustomBrick = models.get_model("bricks", self.model)
-        bricks = CustomBrick.objects.filter(
-            Q(slug = self.slug),
-            Q(lang_code = context['LANGUAGE_CODE']) |
-            Q(lang_code = "")
-        )
-
-        if bricks:
-            context[self.as_varname] = bricks[0]
-            return ''
-        else:
-            return '(empty)'
 
 setup(
     name='django-bricks',
-    version='0.1.1',
+    version='0.1.2',
     description='',
     long_description=open('README.rst').read(),
     author='Jeffrey Gelens',