Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10458 -- Corrected the `next_month` and `previous_month` conte…

…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...
commit 6cc0c3887ca1c43650f49d16916ac0d9862ce61f 1 parent cb43898
Russell Keith-Magee authored April 13, 2009
18  django/views/generic/date_based.py
@@ -144,12 +144,18 @@ def archive_month(request, year, month, queryset, date_field,
144 144
 
145 145
     # Calculate the next month, if applicable.
146 146
     if allow_future:
147  
-        next_month = last_day + datetime.timedelta(days=1)
148  
-    elif last_day < datetime.date.today():
149  
-        next_month = last_day + datetime.timedelta(days=1)
  147
+        next_month = last_day
  148
+    elif last_day <= datetime.date.today():
  149
+        next_month = last_day
150 150
     else:
151 151
         next_month = None
152 152
 
  153
+    # Calculate the previous month
  154
+    if first_day.month == 1:
  155
+        previous_month = first_day.replace(year=first_day.year-1,month=12)
  156
+    else:
  157
+        previous_month = first_day.replace(month=first_day.month-1)
  158
+
153 159
     if not template_name:
154 160
         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
155 161
     t = template_loader.get_template(template_name)
@@ -157,7 +163,7 @@ def archive_month(request, year, month, queryset, date_field,
157 163
         '%s_list' % template_object_name: object_list,
158 164
         'month': date,
159 165
         'next_month': next_month,
160  
-        'previous_month': first_day - datetime.timedelta(days=1),
  166
+        'previous_month': previous_month,
161 167
     }, context_processors)
162 168
     for key, value in extra_context.items():
163 169
         if callable(value):
@@ -239,7 +245,7 @@ def archive_day(request, year, month, day, queryset, date_field,
239 245
     """
240 246
     if extra_context is None: extra_context = {}
241 247
     try:
242  
-        tt = time.strptime('%s-%s-%s' % (year, month, day), 
  248
+        tt = time.strptime('%s-%s-%s' % (year, month, day),
243 249
                            '%s-%s-%s' % ('%Y', month_format, day_format))
244 250
         date = datetime.date(*tt[:3])
245 251
     except ValueError:
@@ -311,7 +317,7 @@ def object_detail(request, year, month, day, queryset, date_field,
311 317
     """
312 318
     if extra_context is None: extra_context = {}
313 319
     try:
314  
-        tt = time.strptime('%s-%s-%s' % (year, month, day), 
  320
+        tt = time.strptime('%s-%s-%s' % (year, month, day),
315 321
                            '%s-%s-%s' % ('%Y', month_format, day_format))
316 322
         date = datetime.date(*tt[:3])
317 323
     except ValueError:
23  tests/regressiontests/views/tests/generic/date_based.py
... ...
@@ -1,6 +1,6 @@
1 1
 # coding: utf-8
2 2
 from django.test import TestCase
3  
-from datetime import datetime
  3
+from datetime import datetime, date
4 4
 from datetime import timedelta
5 5
 from regressiontests.views.models import Article, Author, DateArticle
6 6
 
@@ -52,6 +52,8 @@ def test_archive_month_includes_only_month(self):
52 52
         article.save()
53 53
         response = self.client.get('/views/date_based/archive_month/2004/02/')
54 54
         self.assertEqual(response.status_code, 200)
  55
+        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
  56
+        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
55 57
 
56 58
         article.date_created = first_second_of_feb-two_seconds
57 59
         article.save()
@@ -62,6 +64,8 @@ def test_archive_month_includes_only_month(self):
62 64
         article.save()
63 65
         response = self.client.get('/views/date_based/archive_month/2004/02/')
64 66
         self.assertEqual(response.status_code, 200)
  67
+        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
  68
+        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
65 69
 
66 70
         article.date_created = first_second_of_mar
67 71
         article.save()
@@ -74,6 +78,8 @@ def test_archive_month_includes_only_month(self):
74 78
         article2.save()
75 79
         response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
76 80
         self.assertEqual(response.status_code, 200)
  81
+        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
  82
+        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
77 83
 
78 84
         article2.date_created = (first_second_of_feb-two_seconds).date()
79 85
         article2.save()
@@ -84,12 +90,27 @@ def test_archive_month_includes_only_month(self):
84 90
         article2.save()
85 91
         response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
86 92
         self.assertEqual(response.status_code, 200)
  93
+        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
  94
+        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
87 95
 
88 96
         article2.date_created = first_second_of_mar.date()
89 97
         article2.save()
90 98
         response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
91 99
         self.assertEqual(response.status_code, 404)
92 100
 
  101
+        now = datetime.now()
  102
+        prev_month = now.date().replace(day=1)
  103
+        if prev_month.month == 11:
  104
+            prev_month = prev_month.replace(year=prev_month.year-1, month=12)
  105
+        else:
  106
+            prev_month = prev_month.replace(month=prev_month.month-1)
  107
+        article2.date_created = now
  108
+        article2.save()
  109
+        response = self.client.get('/views/date_based/datefield/archive_month/%s/' % now.strftime('%Y/%m'))
  110
+        self.assertEqual(response.status_code, 200)
  111
+        self.assertEqual(response.context['next_month'], None)
  112
+        self.assertEqual(response.context['previous_month'], prev_month)
  113
+
93 114
 class DayArchiveTests(TestCase):
94 115
 
95 116
     def test_year_month_day_format(self):

0 notes on commit 6cc0c38

Please sign in to comment.
Something went wrong with that request. Please try again.