Commits

Jeffrey Gelens committed 32b8459

New templatetags and removed unused models

  • Participants
  • Parent commits 00bc44d

Comments (0)

Files changed (3)

File bricks/admin.py

-from django.contrib import admin
-from django import forms
-from models import Brick, Wall, WallPart, Language
-
-class WallPartForm(forms.ModelForm):
-    model = WallPart
-
-    class Media:
-        js = (
-            'scripts/jquery-ui-sortable.js',
-            'scripts/admin/jquery-init.js',
-            'scripts/admin/tabular-inline-sortable.js',
-            'scripts/admin/tabular-inline-edit.js',
-        )
-
-
-class WallPartRelationshipInline(admin.TabularInline):
-    model = WallPart
-    form = WallPartForm
-    extra = 0
-
-
-class WallAdmin(admin.ModelAdmin):
-    inlines = (WallPartRelationshipInline,)
-
-
-class BrickInline(admin.TabularInline):
-    model = Brick
-
-
-class WallPartAdmin(admin.ModelAdmin):
-    model = WallPart
-
-
-class BrickAdmin(admin.ModelAdmin):
-    search_fields = ('key', 'content',)
-
-    class Media:
-        js = (
-            'scripts/admin/jquery-init.js',
-            'scripts/tiny_mce/jquery.tinymce.js',
-            'scripts/tiny_mce/loader.js',
-        )
-
-admin.site.register(Brick, BrickAdmin)
-admin.site.register(Wall, WallAdmin)
-admin.site.register(Language)
-admin.site.register(WallPart, WallPartAdmin)

File bricks/models.py

+from django.template import Library, TemplateSyntaxError, Node, NodeList
 from django.db import models
-from django.conf import settings
-from django.utils.translation import ugettext_lazy as _
-from django.utils.translation import ugettext as __
-from django.contrib.contenttypes.models import ContentType
-from django.contrib.contenttypes import generic
+from django.db.models import Q
 
 
-class Brick(models.Model):
-    """
-    A brick is a relation or a piece of content that can be inserted in to
-    any template using a template tag
-    """
-    key = models.SlugField(_('key'),
-        help_text=_('A unique identifier for this brick'),
-        unique=True)
-    content = models.TextField(blank=True)
-    content_type = models.ForeignKey(ContentType, blank=True, null=True)
-    object_id = models.PositiveIntegerField(blank=True, null=True)
-    content_object = generic.GenericForeignKey('content_type', 'object_id')
+register = Library()
 
-    def __unicode__(self):
-        return self.key
+def parse_wall(parser, token):
+    try:
+        _, model = token.split_contents()
+    except ValueError:
+        raise TemplateSyntaxError, "'wall' tag requires one argument"
 
-    def inscription(self):
-        """A related object has priority over the content."""
-        if self.object_id:
-            return content_object
+    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 self.content
-
-
-class Language(models.Model):
-    code = models.CharField(
-            max_length=2,
-            default=settings.LANGUAGE_CODE,
-            choices=settings.LANGUAGES,
-            unique=True)
-
-    def __unicode__(self):
-        return dict(settings.LANGUAGES).get(self.code)
-
-
-class Wall(models.Model):
-    """
-    A wall is a set of ordered bricks
-    """
-    key = models.SlugField(_('A unique identifier for this wall'), unique=True)
-    bricks = models.ManyToManyField(Brick, through='WallPart')
-
-    def __unicode__(self):
-        return self.key
-
-
-class WallPart(models.Model):
-    wall = models.ForeignKey(Wall)
-    brick = models.ForeignKey(Brick)
-    order = models.PositiveIntegerField(_('order'), blank=True, null=True)
-    language = models.ForeignKey(Language, blank=True, null=True)
-
-    class Meta:
-        ordering = ('order',)
-
-    def __unicode__(self):
-        if not self.language:
-            return __('No language')
-
-        return "%s..." % self.brick.inscription()[:50]
-
-
+            return '(empty)'

File bricks/templatetags/bricks.py

-from django import template
+from django.template import Library, TemplateSyntaxError, Node, NodeList
 from django.db import models
+from django.db.models import Q
 
-register = template.Library()
-Brick = models.get_model('bricks', 'brick')
-Wall = models.get_model('bricks', 'wall')
 
+register = Library()
 
-class WallNode(template.Node):
-    def __init__(self, key, as_varname=None):
-        self.key = key
+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):
-        try:
-            bricks = Brick.objects.filter(
-                wall__key=self.key,
-                wallpart__language__code=context['LANGUAGE_CODE']
-            )
-        except Wall.DoesNotExist:
+        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 ''
-        except Brick.DoesNotExist:
-            return ''
-
-        if self.as_varname is None:
-            return "\n".join([brick.inscription() for brick in bricks])
         else:
-            context[self.as_varname] = bricks
-
-
-def wall(parser, token):
-    """
-    Syntax::
-
-        {% wall "key" as wall_iterator %}
-    """
-    try:
-       template_tag, key, sep, as_varname = token.split_contents()
-    except ValueError:
-        raise template.TemplateSyntaxError, "%r tag requires two arguments" % token.contents.split()[0]
-
-    if key[0] == key[-1] and key[0] in ('"', '\''):
-        return WallNode(key[1:-1], as_varname)
-    else:
-        raise template.TemplateSyntaxError, "%r tag's argument should be quoted" % key
-
-
-def bricks(parser, token):
-    """
-    Syntax::
-        
-        {% bricks from "wall" %}
-    """
-    try:
-       template_tag, sep, key = token.split_contents()
-    except ValueError:
-        raise template.TemplateSyntaxError, "%r tag requires one arguments" % token.contents.split()[0]
-
-    if key[0] == key[-1] and key[0] in ('"', '\''):
-        return WallNode(key[1:-1])
-    else:
-        raise template.TemplateSyntaxError, "%r tag's argument should be quoted" % key
-
-
-register.tag('wall', wall)
-register.tag('bricks', bricks)
+            return '(empty)'