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