Skip to content

Commit f859628

Browse files
committed
Use CBV for TaggedItemList
1 parent 804f66a commit f859628

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

tagging/views.py

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
Tagging related views.
33
"""
44
from django.http import Http404
5+
from django.views.generic.list import ListView
56
from django.utils.translation import ugettext as _
6-
from django.views.generic.list_detail import object_list
7+
from django.core.exceptions import ImproperlyConfigured
78

89
from tagging.models import Tag
910
from tagging.models import TaggedItem
1011
from tagging.utils import get_tag
1112

1213

13-
def tagged_object_list(request, queryset_or_model=None, tag=None,
14-
related_tags=False, related_tag_counts=True, **kwargs):
14+
class TaggedObjectList(ListView):
1515
"""
1616
A thin wrapper around
17-
``django.views.generic.list_detail.object_list`` which creates a
17+
``django.views.generic.list.ListView`` which creates a
1818
``QuerySet`` containing instances of the given queryset or model
1919
tagged with the given tag.
2020
@@ -28,30 +28,50 @@ def tagged_object_list(request, queryset_or_model=None, tag=None,
2828
tag will have a ``count`` attribute indicating the number of items
2929
which have it in addition to the given tag.
3030
"""
31-
if queryset_or_model is None:
32-
try:
33-
queryset_or_model = kwargs.pop('queryset_or_model')
34-
except KeyError:
35-
raise AttributeError(
36-
_('tagged_object_list must be called '
37-
'with a queryset or a model.'))
38-
39-
if tag is None:
40-
try:
41-
tag = kwargs.pop('tag')
42-
except KeyError:
43-
raise AttributeError(
44-
_('tagged_object_list must be called with a tag.'))
45-
46-
tag_instance = get_tag(tag)
47-
if tag_instance is None:
48-
raise Http404(_('No Tag found matching "%s".') % tag)
49-
queryset = TaggedItem.objects.get_by_model(queryset_or_model, tag_instance)
50-
if 'extra_context' not in kwargs:
51-
kwargs['extra_context'] = {}
52-
kwargs['extra_context']['tag'] = tag_instance
53-
if related_tags:
54-
kwargs['extra_context']['related_tags'] = \
55-
Tag.objects.related_for_model(tag_instance, queryset_or_model,
56-
counts=related_tag_counts)
57-
return object_list(request, queryset, **kwargs)
31+
tag = None
32+
related_tags = False
33+
related_tag_counts = True
34+
35+
def get_tag(self):
36+
if self.tag is None:
37+
try:
38+
self.tag = self.kwargs.pop('tag')
39+
except KeyError:
40+
raise AttributeError(
41+
_('TaggedObjectList must be called with a tag.'))
42+
43+
tag_instance = get_tag(self.tag)
44+
if tag_instance is None:
45+
raise Http404(_('No Tag found matching "%s".') % self.tag)
46+
47+
return tag_instance
48+
49+
def get_queryset_or_model(self):
50+
if self.queryset is not None:
51+
return self.queryset
52+
elif self.model is not None:
53+
return self.model
54+
else:
55+
raise ImproperlyConfigured(
56+
"%(cls)s is missing a QuerySet. Define "
57+
"%(cls)s.model, %(cls)s.queryset, or override "
58+
"%(cls)s.get_queryset_or_model()." % {
59+
'cls': self.__class__.__name__
60+
}
61+
)
62+
63+
def get_queryset(self):
64+
self.queryset_or_model = self.get_queryset_or_model()
65+
self.tag_instance = self.get_tag()
66+
return TaggedItem.objects.get_by_model(
67+
self.queryset_or_model, self.tag_instance)
68+
69+
def get_context_data(self, **kwargs):
70+
context = super(TaggedObjectList, self).get_context_data(**kwargs)
71+
context['tag'] = self.tag_instance
72+
73+
if self.related_tags:
74+
context['related_tags'] = Tag.objects.related_for_model(
75+
self.tag_instance, self.queryset_or_model,
76+
counts=self.related_tag_counts)
77+
return context

0 commit comments

Comments
 (0)