cciw-website / cciw / tagging / templatetags / tagging.py

Luke Plant 17eca0b 












































from django import template
from cciw.tagging import utils
from cciw.tagging.models import Tag

register = template.Library()

class TagSummaryNode(template.Node):
    def __init__(self, object_var_name, output_var_name):
        self.object_var_name = object_var_name
        self.output_var_name = output_var_name
        
    def render(self, context):
        try:
            obj = template.resolve_variable(self.object_var_name, context)
        except template.VariableDoesNotExist:
            return ''
        context[self.output_var_name] = Tag.objects.get_tag_summaries(target=obj)
        return ''

def do_get_tag_summaries(parser, token):
    """
    Gets tag summaries for an object, populating the context with
    a TagSummaryCollection using the named defined in the 'as' clause.
    
    Syntax::
    
        {% get_tag_summaries for [object] as [varname] %}
        
    Example usage::
        
        {% get_tag_summaries for post as post_tags %}
        
    """
    tokens = token.contents.split()
    # Now tokens is a list like this:
    # ['get_tag_summaries', 'for', 'foo', 'as', 'bar']
    if not len(tokens) == 5:
        raise template.TemplateSyntaxError, "%r tag requires 4 arguments" % tokens[0]
    if tokens[1] != 'for':
        raise template.TemplateSyntaxError, "First argument in %r tag must be 'for'" % tokens[0]
    if tokens[3] != 'as':
        raise template.TemplateSyntaxError, "Third argument in %r must be 'as'" % tokens[0]

    return TagSummaryNode(tokens[2], tokens[4])

register.tag('get_tag_summaries', do_get_tag_summaries)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.