From 598032b8c4ec7d642b6223af8569bef81d183385 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Apr 2011 12:03:58 +0000 Subject: [PATCH] Fixed #15698 -- Fixed inconsistant handling of context_object_name in paginated MultipleObjectMixin views. Thanks, Dave Hall. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16079 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/views/generic/list.py | 2 +- tests/regressiontests/generic_views/dates.py | 30 ++++++++++++++++++++ tests/regressiontests/generic_views/urls.py | 8 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/django/views/generic/list.py b/django/views/generic/list.py index 56684573a3..5135763d53 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -89,6 +89,7 @@ class MultipleObjectMixin(object): """ queryset = kwargs.pop('object_list') page_size = self.get_paginate_by(queryset) + context_object_name = self.get_context_object_name(queryset) if page_size: paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size) context = { @@ -105,7 +106,6 @@ class MultipleObjectMixin(object): 'object_list': queryset } context.update(kwargs) - context_object_name = self.get_context_object_name(queryset) if context_object_name is not None: context[context_object_name] = queryset return context diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py index d5345befd0..5b784f6810 100644 --- a/tests/regressiontests/generic_views/dates.py +++ b/tests/regressiontests/generic_views/dates.py @@ -119,6 +119,13 @@ class YearArchiveViewTests(TestCase): self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['date_list']), [datetime.datetime(year, 1, 1)]) + def test_year_view_paginated(self): + res = self.client.get('/dates/books/2006/paginated/') + self.assertEqual(res.status_code, 200) + self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006))) + self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006))) + self.assertTemplateUsed(res, 'generic_views/book_archive_year.html') + def test_year_view_invalid_pattern(self): res = self.client.get('/dates/books/no_year/') self.assertEqual(res.status_code, 404) @@ -190,6 +197,13 @@ class MonthArchiveViewTests(TestCase): self.assertEqual(res.context['next_month'], future) self.assertEqual(res.context['previous_month'], datetime.date(2006, 5, 1)) + def test_month_view_paginated(self): + res = self.client.get('/dates/books/2008/oct/paginated/') + self.assertEqual(res.status_code, 200) + self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10))) + self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10))) + self.assertTemplateUsed(res, 'generic_views/book_archive_month.html') + def test_custom_month_format(self): res = self.client.get('/dates/books/2008/10/') self.assertEqual(res.status_code, 200) @@ -251,6 +265,15 @@ class WeekArchiveViewTests(TestCase): self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['book_list']), [b]) + def test_week_view_paginated(self): + week_start = datetime.date(2008, 9, 28) + week_end = week_start + datetime.timedelta(days=7) + res = self.client.get('/dates/books/2008/week/39/') + self.assertEqual(res.status_code, 200) + self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end))) + self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end))) + self.assertTemplateUsed(res, 'generic_views/book_archive_week.html') + def test_week_view_invalid_pattern(self): res = self.client.get('/dates/books/2007/week/no_week/') self.assertEqual(res.status_code, 404) @@ -327,6 +350,13 @@ class DayArchiveViewTests(TestCase): self.assertEqual(res.context['next_day'], future) self.assertEqual(res.context['previous_day'], datetime.date(2006, 5, 1)) + def test_day_view_paginated(self): + res = self.client.get('/dates/books/2008/oct/1/') + self.assertEqual(res.status_code, 200) + self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1))) + self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1))) + self.assertTemplateUsed(res, 'generic_views/book_archive_day.html') + def test_next_prev_context(self): res = self.client.get('/dates/books/2008/oct/01/') self.assertEqual(res.content, "Archive for Oct. 1, 2008. Previous day is May 1, 2006") diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py index c06b5d8a23..067c1f687a 100644 --- a/tests/regressiontests/generic_views/urls.py +++ b/tests/regressiontests/generic_views/urls.py @@ -146,6 +146,8 @@ urlpatterns = patterns('', views.BookYearArchive.as_view(allow_empty=True)), (r'^dates/books/(?P\d{4})/allow_future/$', views.BookYearArchive.as_view(allow_future=True)), + (r'^dates/books/(?P\d{4})/paginated/$', + views.BookYearArchive.as_view(make_object_list=True, paginate_by=30)), (r'^dates/books/no_year/$', views.BookYearArchive.as_view()), @@ -158,6 +160,8 @@ urlpatterns = patterns('', views.BookMonthArchive.as_view(allow_empty=True)), (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/allow_future/$', views.BookMonthArchive.as_view(allow_future=True)), + (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/paginated/$', + views.BookMonthArchive.as_view(paginate_by=30)), (r'^dates/books/(?P\d{4})/no_month/$', views.BookMonthArchive.as_view()), @@ -168,6 +172,8 @@ urlpatterns = patterns('', views.BookWeekArchive.as_view(allow_empty=True)), (r'^dates/books/(?P\d{4})/week/(?P\d{1,2})/allow_future/$', views.BookWeekArchive.as_view(allow_future=True)), + (r'^dates/books/(?P\d{4})/week/(?P\d{1,2})/paginated/$', + views.BookWeekArchive.as_view(paginate_by=30)), (r'^dates/books/(?P\d{4})/week/no_week/$', views.BookWeekArchive.as_view()), (r'^dates/books/(?P\d{4})/week/(?P\d{1,2})/monday/$', @@ -182,6 +188,8 @@ urlpatterns = patterns('', views.BookDayArchive.as_view(allow_empty=True)), (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/(?P\d{1,2})/allow_future/$', views.BookDayArchive.as_view(allow_future=True)), + (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/(?P\d{1,2})/paginated/$', + views.BookDayArchive.as_view(paginate_by=True)), (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/no_day/$', views.BookDayArchive.as_view()),