Code

Ticket #14711: 14711.diff

File 14711.diff, 4.2 KB (added by msundstr, 3 years ago)

Same test and fix as before, but all combined into one diff

Line 
1Index: django/views/generic/dates.py
2===================================================================
3--- django/views/generic/dates.py       (revision 14841)
4+++ django/views/generic/dates.py       (working copy)
5@@ -70,7 +70,7 @@
6         Get the previous valid month.
7         """
8         first_day, last_day = _month_bounds(date)
9-        prev = (first_day - datetime.timedelta(days=1)).replace(day=1)
10+        prev = (first_day - datetime.timedelta(days=1))
11         return _get_next_prev_month(self, prev, is_previous=True, use_first_day=True)
12 
13 
14@@ -519,7 +519,7 @@
15     This is a bit complicated since it handles both next and previous months
16     and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.
17 
18-    However in essance the logic comes down to:
19+    However in essence the logic comes down to:
20 
21         * If allow_empty and allow_future are both true, this is easy: just
22           return the naive result (just the next/previous day or month,
23@@ -549,7 +549,7 @@
24     # whose date_field is at least (greater than/less than) the given
25     # naive result
26     else:
27-        # Construct a lookup and an ordering depending on weather we're doing
28+        # Construct a lookup and an ordering depending on whether we're doing
29         # a previous date or a next date lookup.
30         if is_previous:
31             lookup = {'%s__lte' % date_field: naive_result}
32@@ -572,7 +572,7 @@
33         result = result.date()
34 
35     # For month views, we always want to have a date that's the first of the
36-    # month for consistancy's sake.
37+    # month for consistency's sake.
38     if result and use_first_day:
39         result = result.replace(day=1)
40 
41Index: tests/regressiontests/generic_views/dates.py
42===================================================================
43--- tests/regressiontests/generic_views/dates.py        (revision 14841)
44+++ tests/regressiontests/generic_views/dates.py        (working copy)
45@@ -359,3 +359,28 @@
46 
47     def test_invalid_url(self):
48         self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/")
49+
50+class NextPreviousMonthViewTests(TestCase):
51+    urls = 'regressiontests.generic_views.urls'
52+
53+    def setUp(self):
54+        self.pubdate_list = [
55+            datetime.date(2010, month, day)
56+            for month,day in ((9,1), (10,2), (11,3))
57+        ]
58+        for pubdate in self.pubdate_list:
59+            name = str(pubdate)
60+            Book.objects.create(name=name, slug=name, pages=100, pubdate=pubdate)
61+
62+    def test_previous_month_bug(self):
63+        res = self.client.get('/dates/books/2010/nov/allow_empty/')
64+        self.assertEqual(res.status_code, 200)
65+        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
66+        # The following test demonstrates the bug
67+        res = self.client.get('/dates/books/2010/nov/')
68+        self.assertEqual(res.status_code, 200)
69+        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
70+        # The bug does not occur here because a Book with pubdate of Sep 1 exists
71+        res = self.client.get('/dates/books/2010/oct/')
72+        self.assertEqual(res.status_code, 200)
73+        self.assertEqual(res.context['previous_month'], datetime.date(2010,9,1))
74Index: tests/regressiontests/generic_views/tests.py
75===================================================================
76--- tests/regressiontests/generic_views/tests.py        (revision 14841)
77+++ tests/regressiontests/generic_views/tests.py        (working copy)
78@@ -1,5 +1,5 @@
79 from regressiontests.generic_views.base import ViewTest, TemplateViewTest, RedirectViewTest
80-from regressiontests.generic_views.dates import ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests
81+from regressiontests.generic_views.dates import ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests, NextPreviousMonthViewTests
82 from regressiontests.generic_views.detail import DetailViewTest
83 from regressiontests.generic_views.edit import ModelFormMixinTests, CreateViewTests, UpdateViewTests, DeleteViewTests
84 from regressiontests.generic_views.list import ListViewTests