Code

Opened 2 years ago

Closed 2 years ago

#18217 closed Bug (fixed)

Date-based generic views use naive datetime objects

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

Description

Some date-based generic views (MonthArchiveView, WeekArchiveView, DayArchiveView, TodayArchiveView, DateDetailView) raise time zone warnings if USE_TZ because of using naive datetime objects and date objects in _date_from_string, _month_bounds and _date_lookup_for_field from django.views.generic.dates module.

Attachments (2)

18217-backport.patch (23.5 KB) - added by aaugustin 2 years ago.
46b082e0-backport.patch (3.5 KB) - added by aaugustin 2 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

The warning is a symtom of the actual problem, which is that a "day" or "month" isn't a well defined period of time unless a time zone is also specified.

This problem is covered by #17260.

comment:3 Changed 2 years ago by aaugustin

  • Resolution duplicate deleted
  • Status changed from closed to reopened

I'm reopening this because some parts may be fixed by using date ranges instead of year/month/day lookups, similar to r17670.

comment:4 Changed 2 years ago by aaugustin

  • Owner changed from nobody to aaugustin
  • Status changed from reopened to new
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 2 years ago by aaugustin

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

Fixed in 78ba9670af373f5261f95d6560bfd08425adcaad.

Now, date-based generic views display objects consistently in the current time zone. However, date_list is still off — that's #17260.

comment:6 Changed 2 years ago by aaugustin

  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Accepted to Ready for checkin

I'm tempted to backport this commit to 1.4.X. The queryset returned by YearView is wrong when time zone support is enabled, which I would have considered a release blocker for 1.4.

comment:7 Changed 2 years ago by aaugustin

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

I've written the patch (see attachement), but for the tests to pass, I'd have to backport 46b082e05ccedffeb623028caf70adbaba7bce6f first.

I feel that these changes are quite large for fixing a minor bug (the results are off by the UTC offset only for the year view).

Unless someone's strongly in favor of backporting these two commits, I'll leave it there.

Changed 2 years ago by aaugustin

comment:8 Changed 2 years ago by Fantomas42

Hi aaugustin,

I'm strongly in favor of backporting these two commits.

Because without these commits, I cannot complete my migration process for enabling the time zone support without warnings when I run my test suite.

I make an intensive usage of the dates based generic views, that's why I'm doing this request.

Regards

comment:9 Changed 2 years ago by aaugustin

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:10 Changed 2 years ago by aaugustin

I tried backporting both changesets and still I got two test failures.

======================================================================
FAIL: test_aware_datetime_date_detail (regressiontests.generic_views.dates.DateDetailViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/myk/Documents/dev/django-trunk/django/test/utils.py", line 205, in inner
    return test_func(*args, **kwargs)
  File "/Users/myk/Documents/dev/django-trunk/tests/regressiontests/generic_views/dates.py", line 530, in test_aware_datetime_date_detail
    self.assertEqual(res.status_code, 200)
AssertionError: 404 != 200

======================================================================
FAIL: test_aware_datetime_day_view (regressiontests.generic_views.dates.DayArchiveViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/myk/Documents/dev/django-trunk/django/test/utils.py", line 205, in inner
    return test_func(*args, **kwargs)
  File "/Users/myk/Documents/dev/django-trunk/tests/regressiontests/generic_views/dates.py", line 456, in test_aware_datetime_day_view
    self.assertEqual(res.status_code, 200)
AssertionError: 404 != 200

Warnings can be silenced with warnings.filterwarnings if they really bother you, until you upgrade to Django 1.5...

Changed 2 years ago by aaugustin

comment:11 Changed 2 years ago by fantomas42@…

Ok aaugustin,

thank you for the information and for your try of a backport.

For the now I will disable the time zone support in my test cases where dates based views are involved.

Regards

comment:12 Changed 2 years ago by aaugustin

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

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.