From baa1e90d4cf029b42e028ee23d999c99751a24e7 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 28 Oct 2010 02:58:28 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#14504=20--=20Corrected=20the=20way=20o?= =?UTF-8?q?bject=5Flist=20is=20used=20in=20ListView=20to=20avoid=20overwri?= =?UTF-8?q?ting=20context.=20Includes=20improved=20usage=20of=20unittest2?= =?UTF-8?q?=20assertions.=20Thanks=20to=20=C5=81ukasz=20Rekucki=20for=20th?= =?UTF-8?q?e=20final=20patch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@14372 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/views/generic/list.py | 2 +- tests/regressiontests/generic_views/list.py | 42 +++++++++++---------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2492c36a02..e1eaded9c3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -399,6 +399,7 @@ answer newbie questions, and generally made Django that much better: Philippe Raoult Massimiliano Ravelli Brian Ray + Łukasz Rekucki remco@diji.biz Marc Remolt Bruno Renié diff --git a/django/views/generic/list.py b/django/views/generic/list.py index d01bacc956..ef90d75618 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -77,7 +77,7 @@ class MultipleObjectMixin(object): """ Get the context for this view. """ - queryset = kwargs.get('object_list') + queryset = kwargs.pop('object_list') page_size = self.get_paginate_by(queryset) if page_size: paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size) diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py index 5d62aa8fb8..8f6af741f7 100644 --- a/tests/regressiontests/generic_views/list.py +++ b/tests/regressiontests/generic_views/list.py @@ -19,18 +19,19 @@ class ListViewTests(TestCase): self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertEqual(list(res.context['authors']), list(Author.objects.all())) - self.assertEqual(res.context['paginator'], None) - self.assertEqual(res.context['page_obj'], None) - self.assertEqual(res.context['is_paginated'], False) + self.assertIs(res.context['authors'], res.context['object_list']) + self.assertIsNone(res.context['paginator']) + self.assertIsNone(res.context['page_obj']) + self.assertFalse(res.context['is_paginated']) def test_paginated_queryset(self): self._make_authors(100) res = self.client.get('/list/authors/paginated/') self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') - self.assertEqual(len(res.context['authors']), 30) - self.assertEqual(res.context['is_paginated'], True) + self.assertEqual(len(res.context['object_list']), 30) + self.assertIs(res.context['authors'], res.context['object_list']) + self.assertTrue(res.context['is_paginated']) self.assertEqual(res.context['page_obj'].number, 1) self.assertEqual(res.context['paginator'].num_pages, 4) self.assertEqual(res.context['authors'][0].name, 'Author 00') @@ -41,17 +42,18 @@ class ListViewTests(TestCase): self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertEqual(list(res.context['authors']), list(Author.objects.all())) - self.assertEqual(res.context['paginator'], None) - self.assertEqual(res.context['page_obj'], None) - self.assertEqual(res.context['is_paginated'], False) + self.assertIs(res.context['authors'], res.context['object_list']) + self.assertIsNone(res.context['paginator']) + self.assertIsNone(res.context['page_obj']) + self.assertFalse(res.context['is_paginated']) def test_paginated_get_page_by_query_string(self): self._make_authors(100) res = self.client.get('/list/authors/paginated/', {'page': '2'}) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') - self.assertEqual(len(res.context['authors']), 30) + self.assertEqual(len(res.context['object_list']), 30) + self.assertIs(res.context['authors'], res.context['object_list']) self.assertEqual(res.context['authors'][0].name, 'Author 30') self.assertEqual(res.context['page_obj'].number, 2) @@ -59,7 +61,8 @@ class ListViewTests(TestCase): self._make_authors(100) res = self.client.get('/list/authors/paginated/', {'page': 'last'}) self.assertEqual(res.status_code, 200) - self.assertEqual(len(res.context['authors']), 10) + self.assertEqual(len(res.context['object_list']), 10) + self.assertIs(res.context['authors'], res.context['object_list']) self.assertEqual(res.context['authors'][0].name, 'Author 90') self.assertEqual(res.context['page_obj'].number, 4) @@ -68,7 +71,8 @@ class ListViewTests(TestCase): res = self.client.get('/list/authors/paginated/3/') self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'generic_views/author_list.html') - self.assertEqual(len(res.context['authors']), 30) + self.assertEqual(len(res.context['object_list']), 30) + self.assertIs(res.context['authors'], res.context['object_list']) self.assertEqual(res.context['authors'][0].name, 'Author 60') self.assertEqual(res.context['page_obj'].number, 3) @@ -93,30 +97,30 @@ class ListViewTests(TestCase): res = self.client.get('/list/authors/template_name/') self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertEqual(list(res.context['authors']), list(Author.objects.all())) + self.assertIs(res.context['authors'], res.context['object_list']) self.assertTemplateUsed(res, 'generic_views/list.html') def test_template_name_suffix(self): res = self.client.get('/list/authors/template_name_suffix/') self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertEqual(list(res.context['authors']), list(Author.objects.all())) + self.assertIs(res.context['authors'], res.context['object_list']) self.assertTemplateUsed(res, 'generic_views/author_objects.html') def test_context_object_name(self): res = self.client.get('/list/authors/context_object_name/') self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertEqual(list(res.context['author_list']), list(Author.objects.all())) - self.assertFalse('authors' in res.context) + self.assertNotIn('authors', res.context) + self.assertIs(res.context['author_list'], res.context['object_list']) self.assertTemplateUsed(res, 'generic_views/author_list.html') def test_duplicate_context_object_name(self): res = self.client.get('/list/authors/dupe_context_object_name/') self.assertEqual(res.status_code, 200) self.assertEqual(list(res.context['object_list']), list(Author.objects.all())) - self.assertFalse('author_list' in res.context) - self.assertFalse('authors' in res.context) + self.assertNotIn('authors', res.context) + self.assertNotIn('author_list', res.context) self.assertTemplateUsed(res, 'generic_views/author_list.html') def test_missing_items(self):