Opened 9 years ago

Closed 9 years ago

#25352 closed Bug (needsinfo)

django.views.generic.dates.WeekArchiveView use of _date_from_string results on off-by-one weeks handling

Reported by: Didier Raboud Owned by: nobody
Component: Generic views Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While trying to use WeekArchiveView to display objects in given weeks (and navigate within weeks), I've found that the data would be displayed off by one week, which makes it very confusing to use.

I think the problem start there:
https://github.com/django/django/blob/master/django/views/generic/dates.py#L551 :

        date = _date_from_string(year, self.get_year_format(),
                                 week_start, '%w',
                                 week, week_format)

        since = self._make_date_lookup_arg(date)
        until = self._make_date_lookup_arg(self._get_next_week(date))

date is then used as since, which should be the start of the current week, while _date_from_string outputs the day starting the next week (format in use being '%w').

>>> from django.utils import timezone
>>> now = timezone.datetime.now()
>>> now.strftime('%W')
'35'
>>> now.isocalendar()[1]
36
>>> from django.views.generic.dates import _date_from_string
>>> _date_from_string('2015', '%Y', '1', '%w', '35', '%W')
datetime.date(2015, 8, 31)

Actually, I'm not sure where the bug lays, but seeing .isocalendar()[1] differ from .strftime('%W') is certainly weird. So far, it seems to me to be the cause for my previous_week and next_week-based buttons to have off-by-one errors. Is there any reason for .strftime('%W') https://github.com/django/django/blob/master/django/utils/dateformat.py#L300 not to use .isocalendar() ?

What am I overlooking, or doing wrong?

Change History (2)

comment:1 by Tim Graham, 9 years ago

Could you check Django's test suite to see if this is covered there? Sometimes by trying to make the change you think needs to be made, you'll uncover a failing test that shows the reason why things the way they are. Also, if you could provide a failing test case for your report, that's very helpful in understanding it.

comment:2 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Is the issue that weeks are 0-indexed? As noted in the Python docs, "All days in a new year preceding the first Sunday are considered to be in week 0."

Please reopen if you can provide more details like a sample project or a test case for Django's test suite. The current report is a bit abstract and it's difficult for me to understand what problem you're reporting. Thanks!

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