Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.X] Fixed #10458 -- Corrected the `next_month` and `previous_mont…

…h` context variables provided with the generic month_archive view. The value returned now matches the docstring and the generic views documentation. Thanks to fperetti for the report and initial patch.

Merge of r10556 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10557 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e471bbe2099bca630650acc7f6b15c7cad5e2c3f 1 parent 960d317
@freakboy3742 freakboy3742 authored
View
18 django/views/generic/date_based.py
@@ -144,12 +144,18 @@ def archive_month(request, year, month, queryset, date_field,
# Calculate the next month, if applicable.
if allow_future:
- next_month = last_day + datetime.timedelta(days=1)
- elif last_day < datetime.date.today():
- next_month = last_day + datetime.timedelta(days=1)
+ next_month = last_day
+ elif last_day <= datetime.date.today():
+ next_month = last_day
else:
next_month = None
+ # Calculate the previous month
+ if first_day.month == 1:
+ previous_month = first_day.replace(year=first_day.year-1,month=12)
+ else:
+ previous_month = first_day.replace(month=first_day.month-1)
+
if not template_name:
template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name)
@@ -157,7 +163,7 @@ def archive_month(request, year, month, queryset, date_field,
'%s_list' % template_object_name: object_list,
'month': date,
'next_month': next_month,
- 'previous_month': first_day - datetime.timedelta(days=1),
+ 'previous_month': previous_month,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
@@ -239,7 +245,7 @@ def archive_day(request, year, month, day, queryset, date_field,
"""
if extra_context is None: extra_context = {}
try:
- tt = time.strptime('%s-%s-%s' % (year, month, day),
+ tt = time.strptime('%s-%s-%s' % (year, month, day),
'%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3])
except ValueError:
@@ -311,7 +317,7 @@ def object_detail(request, year, month, day, queryset, date_field,
"""
if extra_context is None: extra_context = {}
try:
- tt = time.strptime('%s-%s-%s' % (year, month, day),
+ tt = time.strptime('%s-%s-%s' % (year, month, day),
'%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3])
except ValueError:
View
23 tests/regressiontests/views/tests/generic/date_based.py
@@ -1,6 +1,6 @@
# coding: utf-8
from django.test import TestCase
-from datetime import datetime
+from datetime import datetime, date
from datetime import timedelta
from regressiontests.views.models import Article, Author, DateArticle
@@ -52,6 +52,8 @@ def test_archive_month_includes_only_month(self):
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['next_month'], date(2004, 3, 1))
+ self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
article.date_created = first_second_of_feb-two_seconds
article.save()
@@ -62,6 +64,8 @@ def test_archive_month_includes_only_month(self):
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['next_month'], date(2004, 3, 1))
+ self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
article.date_created = first_second_of_mar
article.save()
@@ -74,6 +78,8 @@ def test_archive_month_includes_only_month(self):
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['next_month'], date(2004, 3, 1))
+ self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
article2.date_created = (first_second_of_feb-two_seconds).date()
article2.save()
@@ -84,12 +90,27 @@ def test_archive_month_includes_only_month(self):
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['next_month'], date(2004, 3, 1))
+ self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
article2.date_created = first_second_of_mar.date()
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
+ now = datetime.now()
+ prev_month = now.date().replace(day=1)
+ if prev_month.month == 11:
+ prev_month = prev_month.replace(year=prev_month.year-1, month=12)
+ else:
+ prev_month = prev_month.replace(month=prev_month.month-1)
+ article2.date_created = now
+ article2.save()
+ response = self.client.get('/views/date_based/datefield/archive_month/%s/' % now.strftime('%Y/%m'))
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['next_month'], None)
+ self.assertEqual(response.context['previous_month'], prev_month)
+
class DayArchiveTests(TestCase):
def test_year_month_day_format(self):
Please sign in to comment.
Something went wrong with that request. Please try again.