Code

Opened 3 years ago

Closed 19 months ago

Last modified 14 months ago

#16218 closed Bug (fixed)

Class-based month archive doesn't behave like previous generic view

Reported by: nnrcschmdt Owned by: aaugustin
Component: Generic views Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While converting some of the regression tests for generic views to class-based views, I've noticed that the class-based month archive doesn't behave like the previous generic views.

Applying the attached patch breaks the tests:

FAIL: test_archive_month_date_list (regressiontests.views.tests.generic.date_based.MonthArchiveTest)
...
AssertionError: datetime.datetime(2010, 1, 2, 0, 0) != datetime.datetime(2010, 1, 1, 0, 0)

FAIL: test_archive_month_includes_only_month (regressiontests.views.tests.generic.date_based.MonthArchiveTest)
Regression for #3031: Archives around Feburary include only one month
...
AssertionError: None != datetime.date(2004, 3, 1)

Attachments (3)

ticket-16218.diff (1.3 KB) - added by nnrcschmdt 3 years ago.
django-16218.asctest.diff (777 bytes) - added by bpeschier 3 years ago.
django-16218.diff (5.2 KB) - added by bpeschier 3 years ago.

Download all attachments as: .zip

Change History (15)

Changed 3 years ago by nnrcschmdt

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Could you be a little bit more specific?

I trust that applying this patch breaks the tests, but it's difficult to determine if that means the code does not behave according to the docs and/or obvious expectations.

comment:2 Changed 3 years ago by bpeschier

Quick digging reveals that the old generic views do

date_list = queryset.dates(date_field, 'day')

while the new one does

date_list = queryset.dates(date_field, date_type)[::-1]

So, the two dates tested are just sorted differently apparently. The questions are: why? and does it hurt?

Changed 3 years ago by bpeschier

comment:3 Changed 3 years ago by bpeschier

  • Triage Stage changed from Unreviewed to Accepted

According to the docs at https://docs.djangoproject.com/en/dev/ref/class-based-views/#montharchiveview, date_list should be ordered ascending. Seems like that is not the case, I added a test for ascending order in the generic CBV tests, which gives:

FAIL: test_date_list_order (regressiontests.generic_views.dates.ArchiveIndexViewTests)
date_list should be ascending
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/bpeschier/dev/python/envs/django-dev/django-trunk/tests/regressiontests/generic_views/dates.py", line 83, in test_date_list_order
    self.assertEqual(res.context['date_list'], list(sorted(res.context['date_list'])))
AssertionError: Lists differ: [datetime.datetime(2011, 1, 1,... != [datetime.datetime(2006, 1, 1,...

Changed 3 years ago by bpeschier

comment:4 Changed 3 years ago by bpeschier

  • Has patch set

Patch updates generic CBV to sort date_list in basic index view descending and year/month view ascending. It also adds tests to check the order. This covers the first fail in the report.

The second fail is a different story: in the generic CBV code there is a section about preventing to link to empty pages [1]. Conforming to that, the patch contains an update for the docs to clarify that behaviour with the allow_empty setting. This is different from the old generic views, where this data is given even if allow_empty is False.

[1]: https://code.djangoproject.com/browser/django/trunk/django/views/generic/dates.py?rev=16363#L520

comment:5 Changed 3 years ago by adamnelson

  • Patch needs improvement set

Patch no longer cleanly applies

comment:6 Changed 2 years ago by aaugustin

  • Owner changed from nobody to aaugustin

comment:7 Changed 20 months ago by aaugustin

  • Patch needs improvement unset

I just reviewed the history of this ticket and updated the patch. Pull request: https://github.com/django/django/pull/362

The documented order seems much more logical to me than the implemented order. Like nnrcschmdt and bpeschier, I'd prefer to restore the documented order, even though it introduces a backwards incompatibility.

As an unscientific datapoint, I always reverse date_list in year and month views in my templates; when we fix this ticket I'll be able to remove that code.

comment:8 Changed 19 months ago by aaugustin

Marking RFC per apollo13's comment on the PR.

comment:9 Changed 19 months ago by Aymeric Augustin <aymeric.augustin@…>

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

In baa33cd8faa16737524b1ac355802a10dd63571c:

Fixed #16218 -- date_list order in generic CBVs.

Thanks nnrcschmdt for the report and bpeschier for the initial
version of the patch.

comment:10 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

In a0c67c69bf49d9e1902afd57d95b8511aa911ccb:

Documented ArchiveIndexView's date_list context variable.

Refs #16218.

comment:11 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

In 11f6295551fbcedae50df0efcae5eb34555a46dd:

[1.5.x] Documented ArchiveIndexView's date_list context variable.

Refs #16218.

Backport of a0c67c6.

comment:12 Changed 14 months ago by Aymeric Augustin <aymeric.augustin@…>

In 11f6295551fbcedae50df0efcae5eb34555a46dd:

[1.5.x] Documented ArchiveIndexView's date_list context variable.

Refs #16218.

Backport of a0c67c6.

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.