Skip to content

Commit

Permalink
Implement a better handling of the use case when two dates are compar…
Browse files Browse the repository at this point in the history
…ed on update
  • Loading branch information
sneridagh committed Aug 30, 2017
1 parent 7f5dd84 commit ba84c0f
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/plone/restapi/deserializer/dxcontent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from AccessControl import getSecurityManager
from datetime import datetime
from plone.autoform.interfaces import WRITE_PERMISSIONS_KEY
from plone.dexterity.interfaces import IDexterityContent
from plone.dexterity.utils import iterSchemata
Expand All @@ -23,6 +24,8 @@

from .mixins import OrderingMixin

import pytz


@implementer(IDeserializeFromJson)
@adapter(IDexterityContent, Interface)
Expand Down Expand Up @@ -78,14 +81,20 @@ def __call__(self, validate_all=False, data=None): # noqa: ignore=C901
'message': e.doc(), 'field': name, 'error': e})
else:
field_data[name] = value
try:
if value != dm.get():
dm.set(value)
modified = True
except TypeError:
# Most probably due to offset-naive and offset
# aware objects, set the value as they most likely
# are not the same
dm_value = dm.get()

# This is required in case that we can compare
# offset-naive and offset aware objects. We convert all
# offset-naive datetimes to UTC timezone first and then
# compare them
if isinstance(value, datetime) and \
isinstance(dm_value, datetime):
if value.tzinfo is None:
value = value.replace(tzinfo=pytz.UTC)
if dm_value.tzinfo is None:
dm_value = dm_value.replace(tzinfo=pytz.UTC)

if value != dm_value:
dm.set(value)
modified = True

Expand Down

0 comments on commit ba84c0f

Please sign in to comment.