Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Fixed django#18353 -- Inconsistency in date-based CBVs.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed May 24, 2012
1 parent f4abba5 commit 3b2993e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
18 changes: 17 additions & 1 deletion django/views/generic/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
"""
Expand Down
10 changes: 9 additions & 1 deletion docs/ref/class-based-views.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
14 changes: 13 additions & 1 deletion docs/releases/1.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========================

Expand All @@ -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.
12 changes: 10 additions & 2 deletions tests/regressiontests/generic_views/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3b2993e

Please sign in to comment.