Ticket #14711: 14711.diff

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

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

  • django/views/generic/dates.py

     
    7070        Get the previous valid month.
    7171        """
    7272        first_day, last_day = _month_bounds(date)
    73         prev = (first_day - datetime.timedelta(days=1)).replace(day=1)
     73        prev = (first_day - datetime.timedelta(days=1))
    7474        return _get_next_prev_month(self, prev, is_previous=True, use_first_day=True)
    7575
    7676
     
    519519    This is a bit complicated since it handles both next and previous months
    520520    and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.
    521521
    522     However in essance the logic comes down to:
     522    However in essence the logic comes down to:
    523523
    524524        * If allow_empty and allow_future are both true, this is easy: just
    525525          return the naive result (just the next/previous day or month,
     
    549549    # whose date_field is at least (greater than/less than) the given
    550550    # naive result
    551551    else:
    552         # Construct a lookup and an ordering depending on weather we're doing
     552        # Construct a lookup and an ordering depending on whether we're doing
    553553        # a previous date or a next date lookup.
    554554        if is_previous:
    555555            lookup = {'%s__lte' % date_field: naive_result}
     
    572572        result = result.date()
    573573
    574574    # For month views, we always want to have a date that's the first of the
    575     # month for consistancy's sake.
     575    # month for consistency's sake.
    576576    if result and use_first_day:
    577577        result = result.replace(day=1)
    578578
  • tests/regressiontests/generic_views/dates.py

     
    359359
    360360    def test_invalid_url(self):
    361361        self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/")
     362
     363class NextPreviousMonthViewTests(TestCase):
     364    urls = 'regressiontests.generic_views.urls'
     365
     366    def setUp(self):
     367        self.pubdate_list = [
     368            datetime.date(2010, month, day)
     369            for month,day in ((9,1), (10,2), (11,3))
     370        ]
     371        for pubdate in self.pubdate_list:
     372            name = str(pubdate)
     373            Book.objects.create(name=name, slug=name, pages=100, pubdate=pubdate)
     374
     375    def test_previous_month_bug(self):
     376        res = self.client.get('/dates/books/2010/nov/allow_empty/')
     377        self.assertEqual(res.status_code, 200)
     378        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
     379        # The following test demonstrates the bug
     380        res = self.client.get('/dates/books/2010/nov/')
     381        self.assertEqual(res.status_code, 200)
     382        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
     383        # The bug does not occur here because a Book with pubdate of Sep 1 exists
     384        res = self.client.get('/dates/books/2010/oct/')
     385        self.assertEqual(res.status_code, 200)
     386        self.assertEqual(res.context['previous_month'], datetime.date(2010,9,1))
  • tests/regressiontests/generic_views/tests.py

     
    11from regressiontests.generic_views.base import ViewTest, TemplateViewTest, RedirectViewTest
    2 from regressiontests.generic_views.dates import ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests
     2from regressiontests.generic_views.dates import ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests, NextPreviousMonthViewTests
    33from regressiontests.generic_views.detail import DetailViewTest
    44from regressiontests.generic_views.edit import ModelFormMixinTests, CreateViewTests, UpdateViewTests, DeleteViewTests
    55from regressiontests.generic_views.list import ListViewTests
Back to Top