Permalink
Browse files

tag views are now class-base, #140

  • Loading branch information...
1 parent dc56829 commit 784d95961df7868c364d8f0747093bc1175f6968 @Fantomas42 committed Apr 4, 2012
Showing with 69 additions and 52 deletions.
  1. +9 −20 zinnia/tests/custom_views_detail_urls.py
  2. +16 −8 zinnia/urls/tags.py
  3. +44 −24 zinnia/views/tags.py
@@ -1,10 +1,8 @@
"""Test urls for the zinnia project"""
-from functools import wraps
-
from django.conf.urls import url
from django.conf.urls import patterns
-from zinnia.views.tags import tag_detail
+from zinnia.views.tags import TagDetail
from zinnia.views.authors import AuthorDetail
from zinnia.views.categories import CategoryDetail
from zinnia.tests.urls import urlpatterns as test_urlpatterns
@@ -22,27 +20,16 @@ def get_context_data(self, **kwargs):
return context
-class CustomAuthorDetail(CustomModelDetailMixin, AuthorDetail):
+class CustomTagDetail(CustomModelDetailMixin, TagDetail):
pass
-class CustomCategoryDetail(CustomModelDetailMixin, CategoryDetail):
+class CustomAuthorDetail(CustomModelDetailMixin, AuthorDetail):
pass
-def call_with_template_and_extra_context(
- view, template_name='zinnia/entry_list.html',
- extra_context={'extra': 'context'}):
-
- @wraps(view)
- def wrapper(*args, **kwargs):
- return view(template_name=template_name,
- extra_context=extra_context,
- *args, **kwargs)
-
- return wrapper
-
-custom_tag_detail = call_with_template_and_extra_context(tag_detail)
+class CustomCategoryDetail(CustomModelDetailMixin, CategoryDetail):
+ pass
urlpatterns = patterns(
@@ -60,7 +47,9 @@ def wrapper(*args, **kwargs):
CustomCategoryDetail.as_view(),
name='zinnia_category_detail'),
url(r'^tags/(?P<tag>[- \w]+)/$',
- custom_tag_detail, name='zinnia_tag_detail'),
+ CustomTagDetail.as_view(),
+ name='zinnia_tag_detail'),
url(r'^tags/(?P<tag>[- \w]+)/page/(?P<page>\d+)/$',
- custom_tag_detail, name='zinnia_tag_detail_paginated'),
+ CustomTagDetail.as_view(),
+ name='zinnia_tag_detail_paginated'),
) + test_urlpatterns
View
@@ -2,11 +2,19 @@
from django.conf.urls import url
from django.conf.urls import patterns
-urlpatterns = patterns('zinnia.views.tags',
- url(r'^$', 'tag_list',
- name='zinnia_tag_list'),
- url(r'^(?P<tag>[^/]+(?u))/$', 'tag_detail',
- name='zinnia_tag_detail'),
- url(r'^(?P<tag>[^/]+(?u))/page/(?P<page>\d+)/$',
- 'tag_detail', name='zinnia_tag_detail_paginated'),
- )
+from zinnia.views.tags import TagList
+from zinnia.views.tags import TagDetail
+
+
+urlpatterns = patterns(
+ '',
+ url(r'^$',
+ TagList.as_view(),
+ name='zinnia_tag_list'),
+ url(r'^(?P<tag>[^/]+(?u))/$',
+ TagDetail.as_view(),
+ name='zinnia_tag_detail'),
+ url(r'^(?P<tag>[^/]+(?u))/page/(?P<page>\d+)/$',
+ TagDetail.as_view(),
+ name='zinnia_tag_detail_paginated'),
+ )
View
@@ -1,31 +1,51 @@
"""Views for Zinnia tags"""
+from django.http import Http404
+from django.views.generic.list import ListView
+from django.views.generic.list import BaseListView
from django.template.defaultfilters import slugify
-from django.template.response import TemplateResponse
+from django.utils.translation import ugettext as _
+from tagging.utils import get_tag
from tagging.models import Tag
-from tagging.views import tagged_object_list
+from tagging.models import TaggedItem
from zinnia.models import Entry
from zinnia.settings import PAGINATION
-
-from zinnia.views.decorators import template_name_for_entry_queryset_filtered
-
-
-def tag_list(request, template_name='zinnia/tag_list.html'):
- """Return the list of published tags with counts,
- try to simulate an object_list view"""
- tag_list = Tag.objects.usage_for_queryset(
- Entry.published.all(), counts=True)
- return TemplateResponse(request, template_name, {'object_list': tag_list})
-
-
-def tag_detail(request, tag, page=None, **kwargs):
- """Display the entries of a tag"""
- if not kwargs.get('template_name'):
- kwargs['template_name'] = template_name_for_entry_queryset_filtered(
- 'tag', slugify(tag))
-
- return tagged_object_list(request, tag=tag,
- queryset_or_model=Entry.published.all(),
- paginate_by=PAGINATION, page=page,
- **kwargs)
+from zinnia.views.mixins import EntryQuerysetTemplateResponseMixin
+
+
+class TagList(ListView):
+ """View return a list of all published tags"""
+ template_name = 'zinnia/tag_list.html'
+
+ def get_queryset(self):
+ """Override the get_queryset method to
+ compute and return the published tags"""
+ return Tag.objects.usage_for_queryset(
+ Entry.published.all(), counts=True)
+
+
+class TagDetail(EntryQuerysetTemplateResponseMixin, BaseListView):
+ """View return a list of all the entries
+ published under the current tag"""
+ model_type = 'tag'
+ paginate_by = PAGINATION
+
+ def get_model_name(self):
+ """The model name is the tag slugified"""
+ return slugify(self.tag)
+
+ def get_queryset(self):
+ """Return a queryset of entries published
+ belonging to the current tag"""
+ self.tag = get_tag(self.kwargs['tag'])
+ if self.tag is None:
+ raise Http404(_('No Tag found matching "%s".') % self.tag)
+ return TaggedItem.objects.get_by_model(
+ Entry.published.all(), self.tag)
+
+ def get_context_data(self, **kwargs):
+ """Add the current tag in context"""
+ context = super(TagDetail, self).get_context_data(**kwargs)
+ context['tag'] = self.tag
+ return context

0 comments on commit 784d959

Please sign in to comment.