Skip to content

Commit

Permalink
Fixed #10458 -- Corrected the next_month and previous_month conte…
Browse files Browse the repository at this point in the history
…xt 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
  • Loading branch information
freakboy3742 committed Apr 13, 2009
1 parent cb43898 commit 6cc0c38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
18 changes: 12 additions & 6 deletions django/views/generic/date_based.py
Expand Up @@ -144,20 +144,26 @@ 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)
c = RequestContext(request, {
'%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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 22 additions & 1 deletion 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

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit 6cc0c38

Please sign in to comment.