Skip to content

Commit

Permalink
Propose a fix for the issue #253 by not comparing IDatetime fields wh…
Browse files Browse the repository at this point in the history
…en patching and set the value always.
  • Loading branch information
sneridagh committed Mar 8, 2017
1 parent 64b8a8e commit 4438c88
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Bugfixes:
- Add missing id to the Plone site serialization, related to issue #186
[sneridagh]

- Propose a fix for the issue #253 by not comparing IDatetime fields when
patching and set the value always.
[sneridagh]

1.0a9 (2017-03-03)
------------------
Expand Down
10 changes: 9 additions & 1 deletion src/plone/restapi/deserializer/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from zope.schema import getFields
from zope.schema.interfaces import ValidationError
from zope.security.interfaces import IPermission
from zope.schema.interfaces import IDatetime


@implementer(IDeserializeFromJson)
Expand Down Expand Up @@ -75,9 +76,16 @@ def __call__(self, validate_all=False):
'message': e.doc(), 'field': name, 'error': e})
else:
field_data[name] = value
if value != dm.get():

if IDatetime.providedBy(dm.field):
# Do not compare both, just set the value. See
# https://github.com/plone/plone.restapi/issues/253
dm.set(value)
modified = True
else:
if value != dm.get():
dm.set(value)
modified = True

elif validate_all:
# Never validate the changeNote of p.a.versioningbehavior
Expand Down
52 changes: 52 additions & 0 deletions src/plone/restapi/tests/test_content_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from plone.app.testing import login
from plone.app.testing import setRoles
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
from plone.restapi.testing import RelativeSession
from DateTime import DateTime

import datetime
import requests
import transaction
import unittest
Expand All @@ -22,8 +25,14 @@ class TestContentPatch(unittest.TestCase):
def setUp(self):
self.app = self.layer['app']
self.portal = self.layer['portal']
self.portal_url = self.portal.absolute_url()
setRoles(self.portal, TEST_USER_ID, ['Member'])
login(self.portal, SITE_OWNER_NAME)

self.api_session = RelativeSession(self.portal_url)
self.api_session.headers.update({'Accept': 'application/json'})
self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

self.portal.invokeFactory(
'Document',
id='doc1',
Expand Down Expand Up @@ -78,3 +87,46 @@ def test_patch_document_returns_401_unauthorized(self):
data='{"title": "Patched Document"}',
)
self.assertEqual(401, response.status_code)

def test_patch_feed_event_with_get_contents(self):
start_date = DateTime(datetime.datetime.today() +
datetime.timedelta(days=1)).ISO8601()
end_date = DateTime(datetime.datetime.today() +
datetime.timedelta(days=1, hours=1)).ISO8601()
response = self.api_session.post(
'/',
json={
"title": "An Event",
"@type": "Event",
"start": start_date,
"end": end_date,
"timezone": "Europe/Vienna"
},
)

self.assertEqual(201, response.status_code)
response = response.json()
event_id = response['id']
two_days_ahead = DateTime(datetime.datetime.today() +
datetime.timedelta(days=2))
response = self.api_session.patch(
'/{}'.format(event_id),
json={
"start": response['start'],
"end": two_days_ahead.ISO8601()
}
)

self.assertEqual(204, response.status_code)

response = self.api_session.get('/{}'.format(event_id))
response = response.json()
self.assertEquals(
DateTime(response['end']).day(),
two_days_ahead.day()
)

self.assertEquals(
DateTime(response['end']).hour(),
two_days_ahead.hour()
)

0 comments on commit 4438c88

Please sign in to comment.