Fixed #18353 -- Inconsistency in date-based CBVs.

This commit is contained in:
Aymeric Augustin 2012-05-24 13:02:19 +02:00
parent f4abba5200
commit 3b2993ed04
4 changed files with 49 additions and 5 deletions

View File

@ -37,6 +37,18 @@ class YearMixin(object):
raise Http404(_(u"No year specified")) raise Http404(_(u"No year specified"))
return year return year
def get_next_year(self, date):
"""
Get the next valid year.
"""
return _get_next_prev(self, date, is_previous=False, period='year')
def get_previous_year(self, date):
"""
Get the previous valid year.
"""
return _get_next_prev(self, date, is_previous=True, period='year')
def _get_next_year(self, date): def _get_next_year(self, date):
""" """
Return the start date of the next interval. Return the start date of the next interval.
@ -419,7 +431,11 @@ class BaseYearArchiveView(YearMixin, BaseDateListView):
# to find information about the model. # to find information about the model.
qs = qs.none() qs = qs.none()
return (date_list, qs, {'year': year}) return (date_list, qs, {
'year': date,
'next_year': self.get_next_year(date),
'previous_year': self.get_previous_year(date),
})
def get_make_object_list(self): def get_make_object_list(self):
""" """

View File

@ -1171,7 +1171,15 @@ YearArchiveView
have objects available according to ``queryset``, represented as have objects available according to ``queryset``, represented as
``datetime.datetime`` objects, in ascending order. ``datetime.datetime`` objects, in ascending order.
* ``year``: The given year, as a four-character string. * ``year``: A ``datetime.date`` object representing the given year.
* ``next_year``: A ``datetime.date`` object representing the first day
of the next year. If the next year is in the future, this will be
``None``.
* ``previous_year``: A ``datetime.date`` object representing the first
day of the previous year. Unlike ``next_year``, this will never be
``None``.
**Notes** **Notes**

View File

@ -71,6 +71,18 @@ Backwards incompatible changes in 1.5
deprecation timeline for a given feature, its removal may appear as a deprecation timeline for a given feature, its removal may appear as a
backwards incompatible change. backwards incompatible change.
Context in year archive class-based views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For consistency with the other date-based generic views,
:class:`~django.views.generic.dates.YearArchiveView` now passes ``year`` in
the context as a :class:`datetime.date` rather than a string. If you are
using ``{{ year }}`` in your templates, you must replace it with ``{{
year|date:"Y" }}``.
``next_year`` and ``previous_year`` were also added in the context. They are
calculated according to ``allow_empty`` and ``allow_future``.
Features deprecated in 1.5 Features deprecated in 1.5
========================== ==========================
@ -86,4 +98,4 @@ our own copy of ``simplejson``. You can safely change any use of
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
The :func:`~django.utils.itercompat.product` function has been deprecated. Use The :func:`~django.utils.itercompat.product` function has been deprecated. Use
the built-in `itertools.product` instead. the built-in :func:`itertools.product` instead.

View File

@ -115,9 +115,13 @@ class YearArchiveViewTests(TestCase):
res = self.client.get('/dates/books/2008/') res = self.client.get('/dates/books/2008/')
self.assertEqual(res.status_code, 200) self.assertEqual(res.status_code, 200)
self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)]) self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)])
self.assertEqual(res.context['year'], '2008') self.assertEqual(res.context['year'], datetime.date(2008, 1, 1))
self.assertTemplateUsed(res, 'generic_views/book_archive_year.html') self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
# Since allow_empty=False, next/prev years must be valid (#7164)
self.assertEqual(res.context['next_year'], None)
self.assertEqual(res.context['previous_year'], datetime.date(2006, 1, 1))
def test_year_view_make_object_list(self): def test_year_view_make_object_list(self):
res = self.client.get('/dates/books/2006/make_object_list/') res = self.client.get('/dates/books/2006/make_object_list/')
self.assertEqual(res.status_code, 200) self.assertEqual(res.status_code, 200)
@ -134,6 +138,10 @@ class YearArchiveViewTests(TestCase):
self.assertEqual(list(res.context['date_list']), []) self.assertEqual(list(res.context['date_list']), [])
self.assertEqual(list(res.context['book_list']), []) self.assertEqual(list(res.context['book_list']), [])
# Since allow_empty=True, next/prev are allowed to be empty years (#7164)
self.assertEqual(res.context['next_year'], datetime.date(2000, 1, 1))
self.assertEqual(res.context['previous_year'], datetime.date(1998, 1, 1))
def test_year_view_allow_future(self): def test_year_view_allow_future(self):
# Create a new book in the future # Create a new book in the future
year = datetime.date.today().year + 1 year = datetime.date.today().year + 1
@ -162,7 +170,7 @@ class YearArchiveViewTests(TestCase):
def test_no_duplicate_query(self): def test_no_duplicate_query(self):
# Regression test for #18354 # Regression test for #18354
with self.assertNumQueries(2): with self.assertNumQueries(4):
self.client.get('/dates/books/2008/reverse/') self.client.get('/dates/books/2008/reverse/')
def test_datetime_year_view(self): def test_datetime_year_view(self):