- December 22, 2010
- view django-tagging tag expression
- 0 (after 0 ratings)
This view parses complex tag queries. It generates a list of model instances matching an expression of tags. The expression currently supports intersection, union and subtraction. Expressions can also be put in parenthesis and then combined with other expressions.
The expression must be passed to this view in the tag_expression argument. In my application this is simply passed from the URL querystring.
This snippet uses the django-tagging app.
It assumes that tags are composed of alphanumeric characters, underscores, hyphens and spaces, but the django-tagging application allows tags with other characters, so you might either want to restrict users to using tags that only contain the above characters, or you might prefer to improve this snippet.
Example: This URL
will retrieve all people who are either deceased parrots or members of Monty Python.
In the tag_expression argument:
ALL is treated as a keyword. If you happen (by some sad chance) to have a tag called ALL and want to use it in the expression, surround it in quotation marks. E.g. "ALL"
famous -returns all instances of the model tagged with famous
famous&deceased -returns all instances of the model tagged both famous and deceased.
famous|deceased -returns all instances of the model tagged famous or deceased.
parrot-deceased -returns all alive parrots in the model.
ALL-deceased -returns all instances of the model that are not tagged deceased.
ALL -returns all instances of the model
"ALL" -returns all instances of the model that are tagged ALL
"great author"&deceased -returns all models tagged as great authors and deceased.
request -- HTTP Request object
tag_expression -- a set expression of tags, supporting intersection, union, parenthesis and difference
app_name -- App for the model we're working on (defaults to pubman)
model_name -- Model on which to apply the set operations (defaults to Article)
view -- view to redirect to after the model instance list has been constructed
html_template -- HTML template to redirect (defaults to 'pubman/tag.html')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import re from tagging.models import Tag, TaggedItem from django.db.models import Q, get_model def tag_view(request, tag_expression, app_name = 'pubman', model_name='article', view=article_list_view, html_template='pubman/tag.html'): model_filter = TaggedItem.objects.all().filter(content_type__model=model_name) search_string = '\'[\w\s]+\'|\"[\w\s]+\"|[\w\s]+|&|\||\(|\)|-' parse_string = re.findall(search_string, tag_expression) print parse_string querystring = "" for token in parse_string: if token in ['&', '|','-', '(',')']: querystring += ' ' + token + ' ' elif token == 'ALL': querystring += ' set([i.id for i in get_model("' +\ app_name + '", "' +\ model_name + '")'+ '.objects.all()])' else: token = token.replace('"','') token = token.replace("'","") querystring += ' set([i.object_id for i in '+\ 'model_filter.filter(tag__name="' + token + '")])' print 'Query: ', querystring try: instances = eval(querystring) except: # This is the fallback when there's an error in the expression. # A better way might be to raise Http404. instances = model_filter.filter(tag__name=tag_expression) object_list = get_model(app_name, model_name).\ objects.all().filter(id__in=instances) return view(request, object_list, html_template)