[FIX] fields: raise when setting aware datetime

Now that Date/Datetime fields use python naive datetime, we want
to prevent developper to call  or assign aware datetime to
fields. Moreover, if so, the timezone will be stored in the
database, but when reading Datetime fields, we expect naive datetime.
Comparing naive and aware datetime objects leads to errors.

This commit forces developpers to use naive datetime when saving
data in database.

As example, we have to erase the timezone of dates in calendar
modules. We also regroup the 2 assignations in one call to
`write` for performance reason.

Thanks to @dbeguin and @RomainLibert for spending some time
on this bug.
This commit is contained in:
jem-odoo
2018-09-05 15:05:33 +02:00
parent a997a5a1c6
commit a6401391ed
2 changed files with 7 additions and 2 deletions
+5 -2
View File
@@ -897,13 +897,16 @@ class Meeting(models.Model):
enddate = tz.localize(enddate)
enddate = enddate.replace(hour=18)
enddate = enddate.astimezone(pytz.utc)
meeting.stop = enddate
startdate = fields.Datetime.from_string(meeting.start_date)
startdate = tz.localize(startdate) # Add "+hh:mm" timezone
startdate = startdate.replace(hour=8) # Set 8 AM in localtime
startdate = startdate.astimezone(pytz.utc) # Convert to UTC
meeting.start = startdate
meeting.write({
'start': startdate.replace(tzinfo=None),
'stop': enddate.replace(tzinfo=None)
})
else:
meeting.write({'start': meeting.start_datetime,
'stop': meeting.stop_datetime})
+2
View File
@@ -1681,6 +1681,8 @@ class Datetime(Field):
return None
if isinstance(value, date):
if isinstance(value, datetime):
if value.tzinfo:
raise ValueError("Datetime field expects a naive datetime: %s" % value)
return value
return datetime.combine(value, time.min)
try: