Fixed #14504 -- Corrected the way object_list is used in ListView to avoid overwriting context. Includes improved usage of unittest2 assertions. Thanks to Łukasz Rekucki for the final patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14372 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-10-28 02:58:28 +00:00
parent 8bc2f2a0b5
commit baa1e90d4c
3 changed files with 25 additions and 20 deletions

View File

@ -399,6 +399,7 @@ answer newbie questions, and generally made Django that much better:
Philippe Raoult <philippe.raoult@n2nsoft.com>
Massimiliano Ravelli <massimiliano.ravelli@gmail.com>
Brian Ray <http://brianray.chipy.org/>
Łukasz Rekucki <lrekucki@gmail.com>
remco@diji.biz
Marc Remolt <m.remolt@webmasters.de>
Bruno Renié <buburno@gmail.com>

View File

@ -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)

View File

@ -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):