web/lib/tagging/views.py
changeset 11 f236caaceb43
equal deleted inserted replaced
10:7207a5a27b8f 11:f236caaceb43
       
     1 """
       
     2 Tagging related views.
       
     3 """
       
     4 from django.http import Http404
       
     5 from django.utils.translation import ugettext as _
       
     6 from django.views.generic.list_detail import object_list
       
     7 
       
     8 from tagging.models import Tag, TaggedItem
       
     9 from tagging.utils import get_tag, get_queryset_and_model
       
    10 
       
    11 def tagged_object_list(request, queryset_or_model=None, tag=None,
       
    12         related_tags=False, related_tag_counts=True, **kwargs):
       
    13     """
       
    14     A thin wrapper around
       
    15     ``django.views.generic.list_detail.object_list`` which creates a
       
    16     ``QuerySet`` containing instances of the given queryset or model
       
    17     tagged with the given tag.
       
    18 
       
    19     In addition to the context variables set up by ``object_list``, a
       
    20     ``tag`` context variable will contain the ``Tag`` instance for the
       
    21     tag.
       
    22 
       
    23     If ``related_tags`` is ``True``, a ``related_tags`` context variable
       
    24     will contain tags related to the given tag for the given model.
       
    25     Additionally, if ``related_tag_counts`` is ``True``, each related
       
    26     tag will have a ``count`` attribute indicating the number of items
       
    27     which have it in addition to the given tag.
       
    28     """
       
    29     if queryset_or_model is None:
       
    30         try:
       
    31             queryset_or_model = kwargs.pop('queryset_or_model')
       
    32         except KeyError:
       
    33             raise AttributeError(_('tagged_object_list must be called with a queryset or a model.'))
       
    34 
       
    35     if tag is None:
       
    36         try:
       
    37             tag = kwargs.pop('tag')
       
    38         except KeyError:
       
    39             raise AttributeError(_('tagged_object_list must be called with a tag.'))
       
    40 
       
    41     tag_instance = get_tag(tag)
       
    42     if tag_instance is None:
       
    43         raise Http404(_('No Tag found matching "%s".') % tag)
       
    44     queryset = TaggedItem.objects.get_by_model(queryset_or_model, tag_instance)
       
    45     if not kwargs.has_key('extra_context'):
       
    46         kwargs['extra_context'] = {}
       
    47     kwargs['extra_context']['tag'] = tag_instance
       
    48     if related_tags:
       
    49         kwargs['extra_context']['related_tags'] = \
       
    50             Tag.objects.related_for_model(tag_instance, queryset_or_model,
       
    51                                           counts=related_tag_counts)
       
    52     return object_list(request, queryset, **kwargs)