1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Fixed #10458 -- Corrected the next_month and previous_month 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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10556 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-04-13 13:23:03 +00:00
parent cb43898d49
commit 6cc0c3887c
2 changed files with 34 additions and 7 deletions

View File

@ -144,12 +144,18 @@ def archive_month(request, year, month, queryset, date_field,
# Calculate the next month, if applicable. # Calculate the next month, if applicable.
if allow_future: if allow_future:
next_month = last_day + datetime.timedelta(days=1) next_month = last_day
elif last_day < datetime.date.today(): elif last_day <= datetime.date.today():
next_month = last_day + datetime.timedelta(days=1) next_month = last_day
else: else:
next_month = None 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: if not template_name:
template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name) 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, '%s_list' % template_object_name: object_list,
'month': date, 'month': date,
'next_month': next_month, 'next_month': next_month,
'previous_month': first_day - datetime.timedelta(days=1), 'previous_month': previous_month,
}, context_processors) }, context_processors)
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
@ -239,7 +245,7 @@ def archive_day(request, year, month, day, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: 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)) '%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3]) date = datetime.date(*tt[:3])
except ValueError: except ValueError:
@ -311,7 +317,7 @@ def object_detail(request, year, month, day, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: 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)) '%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3]) date = datetime.date(*tt[:3])
except ValueError: except ValueError:

View File

@ -1,6 +1,6 @@
# coding: utf-8 # coding: utf-8
from django.test import TestCase from django.test import TestCase
from datetime import datetime from datetime import datetime, date
from datetime import timedelta from datetime import timedelta
from regressiontests.views.models import Article, Author, DateArticle from regressiontests.views.models import Article, Author, DateArticle
@ -52,6 +52,8 @@ class MonthArchiveTest(TestCase):
article.save() article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/') response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200) 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.date_created = first_second_of_feb-two_seconds
article.save() article.save()
@ -62,6 +64,8 @@ class MonthArchiveTest(TestCase):
article.save() article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/') response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200) 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.date_created = first_second_of_mar
article.save() article.save()
@ -74,6 +78,8 @@ class MonthArchiveTest(TestCase):
article2.save() article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200) 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.date_created = (first_second_of_feb-two_seconds).date()
article2.save() article2.save()
@ -84,12 +90,27 @@ class MonthArchiveTest(TestCase):
article2.save() article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200) 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.date_created = first_second_of_mar.date()
article2.save() article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 404) 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): class DayArchiveTests(TestCase):
def test_year_month_day_format(self): def test_year_month_day_format(self):