Commits

Horst Gutmann committed b87bccf

Adds class-based ArchiveIndexView to also provide pagination support

Comments (0)

Files changed (2)

cmsplugin_news/urls.py

 from cmsplugin_news.models import News
 
 from . import feeds
+from . import views
 
 
 news_info_dict = {
 
 urlpatterns = patterns('django.views.generic.date_based',
     url(r'^$', 
-        'archive_index', news_info_dict, name='news_archive_index'),
+        views.ArchiveIndexView.as_view(), news_info_dict, name='news_archive_index'),
     
     url(r'^(?P<year>\d{4})/$', 
         'archive_year', news_info_dict, name='news_archive_year'),

cmsplugin_news/views.py

-# Create your views here.
+from django.views.generic import ListView
+
+from . import models
+
+
+class ArchiveIndexView(ListView):
+    """
+    A simple archive view that exposes following context:
+
+    * latest
+    * date_list
+    * paginator
+    * page_obj
+    * object_list
+    * is_paginated
+
+    The first two are intended to mimic the behaviour of the
+    date_based.archive_index view while the latter ones are provided by
+    ListView.
+    """
+    paginate_by = 15
+    queryset = models.News.published.all()
+    template_name = 'cmsplugin_news/news_archive.html'
+    include_yearlist = True
+
+    def get_context_data(self, **kwargs):
+        context = super(ArchiveIndexView, self).get_context_data(**kwargs)
+        context['latest'] = context['object_list']
+        if self.include_yearlist:
+            date_list = self.get_queryset().dates('pub_date', 'year')[::-1]
+            context['date_list'] = date_list
+        return context
+