Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#16218 closed Bug (fixed)

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

Reported by: Ernesto Rico-Schmidt Owned by: Aymeric Augustin
Component: Generic views Version: dev
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 Ernesto Rico-Schmidt 13 years ago.
django-16218.asctest.diff (777 bytes ) - added by Bas Peschier 13 years ago.
django-16218.diff (5.2 KB ) - added by Bas Peschier 13 years ago.

Download all attachments as: .zip

Change History (15)

by Ernesto Rico-Schmidt, 13 years ago

Attachment: ticket-16218.diff added

comment:1 by Aymeric Augustin, 13 years ago

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 by Bas Peschier, 13 years ago

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?

by Bas Peschier, 13 years ago

Attachment: django-16218.asctest.diff added

comment:3 by Bas Peschier, 13 years ago

Triage Stage: UnreviewedAccepted

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,...

by Bas Peschier, 13 years ago

Attachment: django-16218.diff added

comment:4 by Bas Peschier, 13 years ago

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 by Adam Nelson, 13 years ago

Patch needs improvement: set

Patch no longer cleanly applies

comment:6 by Aymeric Augustin, 13 years ago

Owner: changed from nobody to Aymeric Augustin

comment:7 by Aymeric Augustin, 12 years ago

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 by Aymeric Augustin, 12 years ago

Marking RFC per apollo13's comment on the PR.

comment:9 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

Resolution: fixed
Status: newclosed

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 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

In a0c67c69bf49d9e1902afd57d95b8511aa911ccb:

Documented ArchiveIndexView's date_list context variable.

Refs #16218.

comment:11 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

In 11f6295551fbcedae50df0efcae5eb34555a46dd:

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

Refs #16218.

Backport of a0c67c6.

comment:12 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

In 11f6295551fbcedae50df0efcae5eb34555a46dd:

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

Refs #16218.

Backport of a0c67c6.

Note: See TracTickets for help on using tickets.
Back to Top