Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#14711 closed (fixed)

MonthMixin.get_previous_month incorrect under certain conditions

Reported by: msundstr Owned by: nobody
Component: Generic views Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In the class-based generic views, MonthMixin.get_previous_month will give an incorrect result when allow_empty is False and there are no records that exist for the first day of the previous month.

For example, consider the Books model in the generic_views regression test cases, with 3 books with the pubdates:

2010-09-01
2010-10-02
2010-11-03

/dates/books/2010/oct/ should return a context where previous_month is 2010-09-01
and
/dates/books/2010/nov/ should similarly return 2010-10-01

However, in the second case, 2010-09-01 is returned instead. This is because the date that is passed to the helper function, _get_next_prev_month, (i.e., naive_result), is 2010-10-01, and the subsequent database lookup is for a record before to that date. Hence, the existing record dated 2010-10-02, is skipped and the previous month is returned instead.

Changing MonthMixin.get_previous_month to instead pass in the last day of the previous month, instead of the first day, fixes this problem.

Test demonstrating the problem and a patch for django.views.generic.dates.py included. (patch fixes 3 typos also)

Attachments (4)

dates.diff (1.6 KB) - added by msundstr 3 years ago.
Patch for dates.py
tests.diff (2.4 KB) - added by msundstr 3 years ago.
test case demonstrating the problem
test_case.py (1.1 KB) - added by msundstr 3 years ago.
the test case that should go in generic_views/dates.py (I can't get the tests.diff to upload)
14711.diff (4.2 KB) - added by msundstr 3 years ago.
Same test and fix as before, but all combined into one diff

Download all attachments as: .zip

Change History (8)

Changed 3 years ago by msundstr

Patch for dates.py

Changed 3 years ago by msundstr

test case demonstrating the problem

Changed 3 years ago by msundstr

the test case that should go in generic_views/dates.py (I can't get the tests.diff to upload)

comment:1 Changed 3 years ago by lrekucki

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

The "tests.diff" actually got uploaded. It's the Trac that sometimes has trouble displaying patches.

Changed 3 years ago by msundstr

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

comment:2 Changed 3 years ago by julien

  • Triage Stage changed from Accepted to Ready for checkin

comment:3 Changed 3 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

In [15438]:

Fixed #14711 -- Corrected the calculation for the previous month in generic date views. Thanks to msundstr for the report and patch.

comment:4 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.