From d65aa3c3c146b12548a54c894060bce9a8715ad2 Mon Sep 17 00:00:00 2001 From: Jonas Genannt Date: Sun, 15 Nov 2015 22:26:06 +0100 Subject: Imported Upstream version 0.4 --- tagging/templatetags/__init__.py | 3 + tagging/templatetags/tagging_tags.py | 131 ++++++++++++++++++++++++----------- 2 files changed, 94 insertions(+), 40 deletions(-) (limited to 'tagging/templatetags') diff --git a/tagging/templatetags/__init__.py b/tagging/templatetags/__init__.py index e69de29..ab524e2 100644 --- a/tagging/templatetags/__init__.py +++ b/tagging/templatetags/__init__.py @@ -0,0 +1,3 @@ +""" +Templatetags module for tagging. +""" diff --git a/tagging/templatetags/tagging_tags.py b/tagging/templatetags/tagging_tags.py index 11d31cc..bd38b30 100644 --- a/tagging/templatetags/tagging_tags.py +++ b/tagging/templatetags/tagging_tags.py @@ -1,12 +1,22 @@ +""" +Templatetags for tagging. +""" +from django.template import Node +from django.template import Library +from django.template import Variable +from django.template import TemplateSyntaxError from django.db.models import get_model -from django.template import Library, Node, TemplateSyntaxError, Variable, resolve_variable from django.utils.translation import ugettext as _ -from tagging.models import Tag, TaggedItem -from tagging.utils import LINEAR, LOGARITHMIC +from tagging.utils import LINEAR +from tagging.utils import LOGARITHMIC +from tagging.models import Tag +from tagging.models import TaggedItem + register = Library() + class TagsForModelNode(Node): def __init__(self, model, context_var, counts): self.model = model @@ -16,10 +26,14 @@ class TagsForModelNode(Node): def render(self, context): model = get_model(*self.model.split('.')) if model is None: - raise TemplateSyntaxError(_('tags_for_model tag was given an invalid model: %s') % self.model) - context[self.context_var] = Tag.objects.usage_for_model(model, counts=self.counts) + raise TemplateSyntaxError( + _('tags_for_model tag was given an invalid model: %s') % + self.model) + context[self.context_var] = Tag.objects.usage_for_model( + model, counts=self.counts) return '' + class TagCloudForModelNode(Node): def __init__(self, model, context_var, **kwargs): self.model = model @@ -29,11 +43,14 @@ class TagCloudForModelNode(Node): def render(self, context): model = get_model(*self.model.split('.')) if model is None: - raise TemplateSyntaxError(_('tag_cloud_for_model tag was given an invalid model: %s') % self.model) - context[self.context_var] = \ - Tag.objects.cloud_for_model(model, **self.kwargs) + raise TemplateSyntaxError( + _('tag_cloud_for_model tag was given an invalid model: %s') % + self.model) + context[self.context_var] = Tag.objects.cloud_for_model( + model, **self.kwargs) return '' + class TagsForObjectNode(Node): def __init__(self, obj, context_var): self.obj = Variable(obj) @@ -44,6 +61,7 @@ class TagsForObjectNode(Node): Tag.objects.get_for_object(self.obj.resolve(context)) return '' + class TaggedObjectsNode(Node): def __init__(self, tag, model, context_var): self.tag = Variable(tag) @@ -53,11 +71,14 @@ class TaggedObjectsNode(Node): def render(self, context): model = get_model(*self.model.split('.')) if model is None: - raise TemplateSyntaxError(_('tagged_objects tag was given an invalid model: %s') % self.model) - context[self.context_var] = \ - TaggedItem.objects.get_by_model(model, self.tag.resolve(context)) + raise TemplateSyntaxError( + _('tagged_objects tag was given an invalid model: %s') % + self.model) + context[self.context_var] = TaggedItem.objects.get_by_model( + model, self.tag.resolve(context)) return '' + def do_tags_for_model(parser, token): """ Retrieves a list of ``Tag`` objects associated with a given model @@ -86,19 +107,26 @@ def do_tags_for_model(parser, token): bits = token.contents.split() len_bits = len(bits) if len_bits not in (4, 6): - raise TemplateSyntaxError(_('%s tag requires either three or five arguments') % bits[0]) + raise TemplateSyntaxError( + _('%s tag requires either three or five arguments') % bits[0]) if bits[2] != 'as': - raise TemplateSyntaxError(_("second argument to %s tag must be 'as'") % bits[0]) + raise TemplateSyntaxError( + _("second argument to %s tag must be 'as'") % bits[0]) if len_bits == 6: if bits[4] != 'with': - raise TemplateSyntaxError(_("if given, fourth argument to %s tag must be 'with'") % bits[0]) + raise TemplateSyntaxError( + _("if given, fourth argument to %s tag must be 'with'") % + bits[0]) if bits[5] != 'counts': - raise TemplateSyntaxError(_("if given, fifth argument to %s tag must be 'counts'") % bits[0]) + raise TemplateSyntaxError( + _("if given, fifth argument to %s tag must be 'counts'") % + bits[0]) if len_bits == 4: return TagsForModelNode(bits[1], bits[3], counts=False) else: return TagsForModelNode(bits[1], bits[3], counts=True) + def do_tag_cloud_for_model(parser, token): """ Retrieves a list of ``Tag`` objects for a given model, with tag @@ -132,19 +160,25 @@ def do_tag_cloud_for_model(parser, token): Examples:: {% tag_cloud_for_model products.Widget as widget_tags %} - {% tag_cloud_for_model products.Widget as widget_tags with steps=9 min_count=3 distribution=log %} + {% tag_cloud_for_model products.Widget as widget_tags + with steps=9 min_count=3 distribution=log %} """ bits = token.contents.split() len_bits = len(bits) if len_bits != 4 and len_bits not in range(6, 9): - raise TemplateSyntaxError(_('%s tag requires either three or between five and seven arguments') % bits[0]) + raise TemplateSyntaxError( + _('%s tag requires either three or between five ' + 'and seven arguments') % bits[0]) if bits[2] != 'as': - raise TemplateSyntaxError(_("second argument to %s tag must be 'as'") % bits[0]) + raise TemplateSyntaxError( + _("second argument to %s tag must be 'as'") % bits[0]) kwargs = {} if len_bits > 5: if bits[4] != 'with': - raise TemplateSyntaxError(_("if given, fourth argument to %s tag must be 'with'") % bits[0]) + raise TemplateSyntaxError( + _("if given, fourth argument to %s tag must be 'with'") % + bits[0]) for i in range(5, len_bits): try: name, value = bits[i].split('=') @@ -152,32 +186,42 @@ def do_tag_cloud_for_model(parser, token): try: kwargs[str(name)] = int(value) except ValueError: - raise TemplateSyntaxError(_("%(tag)s tag's '%(option)s' option was not a valid integer: '%(value)s'") % { - 'tag': bits[0], - 'option': name, - 'value': value, - }) + raise TemplateSyntaxError( + _("%(tag)s tag's '%(option)s' option was not " + "a valid integer: '%(value)s'") % { + 'tag': bits[0], + 'option': name, + 'value': value, + }) elif name == 'distribution': if value in ['linear', 'log']: - kwargs[str(name)] = {'linear': LINEAR, 'log': LOGARITHMIC}[value] + kwargs[str(name)] = {'linear': LINEAR, + 'log': LOGARITHMIC}[value] else: - raise TemplateSyntaxError(_("%(tag)s tag's '%(option)s' option was not a valid choice: '%(value)s'") % { + raise TemplateSyntaxError( + _("%(tag)s tag's '%(option)s' option was not " + "a valid choice: '%(value)s'") % { + 'tag': bits[0], + 'option': name, + 'value': value, + }) + else: + raise TemplateSyntaxError( + _("%(tag)s tag was given an " + "invalid option: '%(option)s'") % { 'tag': bits[0], 'option': name, - 'value': value, }) - else: - raise TemplateSyntaxError(_("%(tag)s tag was given an invalid option: '%(option)s'") % { + except ValueError: + raise TemplateSyntaxError( + _("%(tag)s tag was given a badly " + "formatted option: '%(option)s'") % { 'tag': bits[0], - 'option': name, + 'option': bits[i], }) - except ValueError: - raise TemplateSyntaxError(_("%(tag)s tag was given a badly formatted option: '%(option)s'") % { - 'tag': bits[0], - 'option': bits[i], - }) return TagCloudForModelNode(bits[1], bits[3], **kwargs) + def do_tags_for_object(parser, token): """ Retrieves a list of ``Tag`` objects associated with an object and @@ -193,11 +237,14 @@ def do_tags_for_object(parser, token): """ bits = token.contents.split() if len(bits) != 4: - raise TemplateSyntaxError(_('%s tag requires exactly three arguments') % bits[0]) + raise TemplateSyntaxError( + _('%s tag requires exactly three arguments') % bits[0]) if bits[2] != 'as': - raise TemplateSyntaxError(_("second argument to %s tag must be 'as'") % bits[0]) + raise TemplateSyntaxError( + _("second argument to %s tag must be 'as'") % bits[0]) return TagsForObjectNode(bits[1], bits[3]) + def do_tagged_objects(parser, token): """ Retrieves a list of instances of a given model which are tagged with @@ -218,13 +265,17 @@ def do_tagged_objects(parser, token): """ bits = token.contents.split() if len(bits) != 6: - raise TemplateSyntaxError(_('%s tag requires exactly five arguments') % bits[0]) + raise TemplateSyntaxError( + _('%s tag requires exactly five arguments') % bits[0]) if bits[2] != 'in': - raise TemplateSyntaxError(_("second argument to %s tag must be 'in'") % bits[0]) + raise TemplateSyntaxError( + _("second argument to %s tag must be 'in'") % bits[0]) if bits[4] != 'as': - raise TemplateSyntaxError(_("fourth argument to %s tag must be 'as'") % bits[0]) + raise TemplateSyntaxError( + _("fourth argument to %s tag must be 'as'") % bits[0]) return TaggedObjectsNode(bits[1], bits[3], bits[5]) + register.tag('tags_for_model', do_tags_for_model) register.tag('tag_cloud_for_model', do_tag_cloud_for_model) register.tag('tags_for_object', do_tags_for_object) -- cgit v1.2.3