0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

Fixed #28082 -- Made BaseDateListView pass context from get_dated_items() to subclasses.

Thanks leon-matthews for the report and fix.
This commit is contained in:
Sebastian Sassi 2017-08-17 18:55:58 -03:00 committed by Tim Graham
parent 0d3f567a7a
commit 5848305218
2 changed files with 22 additions and 3 deletions

View File

@ -297,9 +297,11 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
def get(self, request, *args, **kwargs):
self.date_list, self.object_list, extra_context = self.get_dated_items()
context = self.get_context_data(object_list=self.object_list,
date_list=self.date_list)
context.update(extra_context)
context = self.get_context_data(
object_list=self.object_list,
date_list=self.date_list,
**extra_context
)
return self.render_to_response(context)
def get_dated_items(self):

View File

@ -1,4 +1,5 @@
import datetime
from unittest import mock
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings, skipUnlessDBFeature
@ -274,6 +275,22 @@ class YearArchiveViewTests(TestDataMixin, TestCase):
res = self.client.get('/dates/books/2011/')
self.assertEqual(list(res.context['date_list']), list(sorted(res.context['date_list'])))
@mock.patch('django.views.generic.list.MultipleObjectMixin.get_context_data')
def test_get_context_data_receives_extra_context(self, mock):
"""
MultipleObjectMixin.get_context_data() receives the context set by
BaseYearArchiveView.get_dated_items(). This behavior is implemented in
BaseDateListView.get().
"""
BookSigning.objects.create(event_date=datetime.datetime(2008, 4, 2, 12, 0))
with self.assertRaisesMessage(TypeError, 'context must be a dict rather than MagicMock.'):
self.client.get('/dates/booksignings/2008/')
args, kwargs = mock.call_args
# These are context values from get_dated_items().
self.assertEqual(kwargs['year'], datetime.date(2008, 1, 1))
self.assertIsNone(kwargs['previous_year'])
self.assertIsNone(kwargs['next_year'])
@override_settings(ROOT_URLCONF='generic_views.urls')
class MonthArchiveViewTests(TestDataMixin, TestCase):