1 # -*- coding: utf-8 -*-
5 from django.http import Http404
6 from django.utils.translation import ugettext as _
7 from django.views.generic.list_detail import object_list
10 def tagged_object_list(request, queryset_or_model=None, tag_model=None, tags=None,
11 related_tags=False, related_tag_counts=True, **kwargs):
14 ``django.views.generic.list_detail.object_list`` which creates a
15 ``QuerySet`` containing instances of the given queryset or model
16 tagged with the given tag.
18 In addition to the context variables set up by ``object_list``, a
19 ``tag`` context variable will contain the ``Tag`` instance for the
22 If ``related_tags`` is ``True``, a ``related_tags`` context variable
23 will contain tags related to the given tag for the given model.
24 Additionally, if ``related_tag_counts`` is ``True``, each related
25 tag will have a ``count`` attribute indicating the number of items
26 which have it in addition to the given tag.
29 if queryset_or_model is None:
30 raise AttributeError(_('tagged_object_list must be called with a queryset or a model.'))
32 raise AttributeError(_('tagged_object_list must be called with a tag model.'))
34 raise AttributeError(_('tagged_object_list must be called with a tag.'))
36 tag_instances = tag_model.get_tag_list(tags)
37 if tag_instances is None:
38 raise Http404(_('No tags found matching "%s".') % tags)
39 queryset = tag_model.intermediary_table_model.objects.get_intersection_by_model(queryset_or_model, tag_instances)
40 if not kwargs.has_key('extra_context'):
41 kwargs['extra_context'] = {}
42 kwargs['extra_context']['tags'] = tag_instances
44 kwargs['extra_context']['related_tags'] = \
45 tag_model.objects.related_for_model(tag_instances, queryset_or_model,
46 counts=related_tag_counts)
47 return object_list(request, queryset, **kwargs)