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:
parent
0d3f567a7a
commit
5848305218
|
@ -297,9 +297,11 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
self.date_list, self.object_list, extra_context = self.get_dated_items()
|
self.date_list, self.object_list, extra_context = self.get_dated_items()
|
||||||
context = self.get_context_data(object_list=self.object_list,
|
context = self.get_context_data(
|
||||||
date_list=self.date_list)
|
object_list=self.object_list,
|
||||||
context.update(extra_context)
|
date_list=self.date_list,
|
||||||
|
**extra_context
|
||||||
|
)
|
||||||
return self.render_to_response(context)
|
return self.render_to_response(context)
|
||||||
|
|
||||||
def get_dated_items(self):
|
def get_dated_items(self):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.test import TestCase, override_settings, skipUnlessDBFeature
|
from django.test import TestCase, override_settings, skipUnlessDBFeature
|
||||||
|
@ -274,6 +275,22 @@ class YearArchiveViewTests(TestDataMixin, TestCase):
|
||||||
res = self.client.get('/dates/books/2011/')
|
res = self.client.get('/dates/books/2011/')
|
||||||
self.assertEqual(list(res.context['date_list']), list(sorted(res.context['date_list'])))
|
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')
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
||||||
class MonthArchiveViewTests(TestDataMixin, TestCase):
|
class MonthArchiveViewTests(TestDataMixin, TestCase):
|
||||||
|
|
Loading…
Reference in New Issue