Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 3b2993ed04f200a89c2d49590131c97c8974e9ac 1 parent f4abba5
@aaugustin aaugustin authored
View
18 django/views/generic/dates.py
@@ -37,6 +37,18 @@ def get_year(self):
raise Http404(_(u"No year specified"))
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):
"""
Return the start date of the next interval.
@@ -419,7 +431,11 @@ def get_dated_items(self):
# to find information about the model.
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):
"""
View
10 docs/ref/class-based-views.txt
@@ -1171,7 +1171,15 @@ YearArchiveView
have objects available according to ``queryset``, represented as
``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**
View
14 docs/releases/1.5.txt
@@ -71,6 +71,18 @@ Backwards incompatible changes in 1.5
deprecation timeline for a given feature, its removal may appear as a
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
==========================
@@ -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 built-in `itertools.product` instead.
+the built-in :func:`itertools.product` instead.
View
12 tests/regressiontests/generic_views/dates.py
@@ -115,9 +115,13 @@ def test_year_view(self):
res = self.client.get('/dates/books/2008/')
self.assertEqual(res.status_code, 200)
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')
+ # 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):
res = self.client.get('/dates/books/2006/make_object_list/')
self.assertEqual(res.status_code, 200)
@@ -134,6 +138,10 @@ def test_year_view_empty(self):
self.assertEqual(list(res.context['date_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):
# Create a new book in the future
year = datetime.date.today().year + 1
@@ -162,7 +170,7 @@ def test_year_view_invalid_pattern(self):
def test_no_duplicate_query(self):
# Regression test for #18354
- with self.assertNumQueries(2):
+ with self.assertNumQueries(4):
self.client.get('/dates/books/2008/reverse/')
def test_datetime_year_view(self):
Please sign in to comment.
Something went wrong with that request. Please try again.