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/views.py | 86 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 30 deletions(-) (limited to 'tagging/views.py') diff --git a/tagging/views.py b/tagging/views.py index 9e7e2f5..527bdd7 100644 --- a/tagging/views.py +++ b/tagging/views.py @@ -2,17 +2,20 @@ Tagging related views. """ from django.http import Http404 +from django.views.generic.list import ListView from django.utils.translation import ugettext as _ -from django.views.generic.list_detail import object_list +from django.core.exceptions import ImproperlyConfigured -from tagging.models import Tag, TaggedItem -from tagging.utils import get_tag, get_queryset_and_model +from tagging.models import Tag +from tagging.models import TaggedItem +from tagging.utils import get_tag +from tagging.utils import get_queryset_and_model -def tagged_object_list(request, queryset_or_model=None, tag=None, - related_tags=False, related_tag_counts=True, **kwargs): + +class TaggedObjectList(ListView): """ A thin wrapper around - ``django.views.generic.list_detail.object_list`` which creates a + ``django.views.generic.list.ListView`` which creates a ``QuerySet`` containing instances of the given queryset or model tagged with the given tag. @@ -26,27 +29,50 @@ def tagged_object_list(request, queryset_or_model=None, tag=None, tag will have a ``count`` attribute indicating the number of items which have it in addition to the given tag. """ - if queryset_or_model is None: - try: - queryset_or_model = kwargs.pop('queryset_or_model') - except KeyError: - raise AttributeError(_('tagged_object_list must be called with a queryset or a model.')) - - if tag is None: - try: - tag = kwargs.pop('tag') - except KeyError: - raise AttributeError(_('tagged_object_list must be called with a tag.')) - - tag_instance = get_tag(tag) - if tag_instance is None: - raise Http404(_('No Tag found matching "%s".') % tag) - queryset = TaggedItem.objects.get_by_model(queryset_or_model, tag_instance) - if not kwargs.has_key('extra_context'): - kwargs['extra_context'] = {} - kwargs['extra_context']['tag'] = tag_instance - if related_tags: - kwargs['extra_context']['related_tags'] = \ - Tag.objects.related_for_model(tag_instance, queryset_or_model, - counts=related_tag_counts) - return object_list(request, queryset, **kwargs) + tag = None + related_tags = False + related_tag_counts = True + + def get_tag(self): + if self.tag is None: + try: + self.tag = self.kwargs.pop('tag') + except KeyError: + raise AttributeError( + _('TaggedObjectList must be called with a tag.')) + + tag_instance = get_tag(self.tag) + if tag_instance is None: + raise Http404(_('No Tag found matching "%s".') % self.tag) + + return tag_instance + + def get_queryset_or_model(self): + if self.queryset is not None: + return self.queryset + elif self.model is not None: + return self.model + else: + raise ImproperlyConfigured( + "%(cls)s is missing a QuerySet. Define " + "%(cls)s.model, %(cls)s.queryset, or override " + "%(cls)s.get_queryset_or_model()." % { + 'cls': self.__class__.__name__ + } + ) + + def get_queryset(self): + self.queryset_or_model = self.get_queryset_or_model() + self.tag_instance = self.get_tag() + return TaggedItem.objects.get_by_model( + self.queryset_or_model, self.tag_instance) + + def get_context_data(self, **kwargs): + context = super(TaggedObjectList, self).get_context_data(**kwargs) + context['tag'] = self.tag_instance + + if self.related_tags: + queryset, model = get_queryset_and_model(self.queryset_or_model) + context['related_tags'] = Tag.objects.related_for_model( + self.tag_instance, model, counts=self.related_tag_counts) + return context -- cgit v1.2.3