Skip to content

Commit

Permalink
Merge branch 'pull_#1224'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaiarocci committed Feb 9, 2019
2 parents c364130 + 7b04229 commit 4898012
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Here you can see the full list of changes between each Eve release.
Version 0.8.2
-------------

New
~~~
- ``on_fetched_diffs`` event hooks (`#1224`_)

Fixed
~~~~~
- HATEOAS ``_links`` seems to get an extra ``&version=diffs`` (`#1228`_)
Expand All @@ -24,6 +28,7 @@ Improved
- Make the parsing of ``req.sort`` and ``req.where`` easily reusable by moving
their logic to dedicated methods (`#1194`_)

.. _`#1224`: https://github.com/pyeve/eve/pull/1224
.. _`#1228`: https://github.com/pyeve/eve/pull/1228
.. _`#1218`: https://github.com/pyeve/eve/pull/1218
.. _`#1209`: https://github.com/pyeve/eve/issues/1209
Expand Down
18 changes: 14 additions & 4 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,12 @@ Let's see an overview of what events are available:
| | | +--------------------------------------------------+
| | | || ``on_fetched_item_<resource_name>`` |
| | | || ``def event(response)`` |
| +--------+------+--------------------------------------------------+
| |Diffs |After || ``on_fetched_diffs`` |
| | | || ``def event(resource_name, response)`` |
| | | +--------------------------------------------------+
| | | || ``on_fetched_diffs_<resource_name>`` |
| | | || ``def event(response)`` |
+-------+--------+------+--------------------------------------------------+
|Insert |Items |Before|| ``on_insert`` |
| | | || ``def event(resource_name, items)`` |
Expand Down Expand Up @@ -1349,6 +1355,8 @@ These are the fetch events with their method signature:
- ``on_fetched_resource_<resource_name>(response)``
- ``on_fetched_item(resource_name, response)``
- ``on_fetched_item_<resource_name>(response)``
- ``on_fetched_diffs(resource_name, response)``
- ``on_fetched_diffs_<resource_name>(response)``
They are raised when items have just been read from the database and are
about to be sent to the client. Registered callback functions can manipulate
Expand All @@ -1374,10 +1382,12 @@ the items as needed before they are returned to the client.
>>> app.on_fetched_item += before_returning_item
>>> app.on_fetched_item_contacts += before_returning_contact
It is important to note that fetch events will work with `Document
Versioning`_ for specific document versions or accessing all document
versions with ``?version=all``, but they *will not* work when accessing diffs
of all versions with ``?version=diffs``.
It is important to note that item fetch events will work with `Document
Versioning`_ for specific document versions like ``?version=5`` and all
document versions with ``?version=all``. Accessing diffs of all versions
with ``?version=diffs`` will only work with the diffs fetch events. Note
that diffs returns partial documents which should be handled in the
callback.
Insert Events
Expand Down
35 changes: 20 additions & 15 deletions eve/methods/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def _perform_aggregation(resource, pipeline, options):
"""
.. versionadded:: 0.7
"""

# TODO move most of this down to the Mongo layer?

# TODO experiment with cursor.batch_size as alternative pagination
Expand Down Expand Up @@ -496,24 +497,28 @@ def getitem_internal(resource, **lookup):
)
)

# callbacks not supported on version diffs because of partial documents
if version != "diffs":
# TODO: callbacks not currently supported with ?version=all

# notify registered callback functions. Please note that, should
# the functions modify the document, last_modified and etag
# won't be updated to reflect the changes (they always reflect the
# documents state on the database).
if resource_def["versioning"] is True and version == "all":
versions = response
if config.DOMAIN[resource]["hateoas"]:
versions = response[config.ITEMS]
# callbacks supported on all version methods - even for diffs with partial documents
# partial documents should be handled properly in the callback
#
# notify registered callback functions. Please note that, should
# the functions modify the document, last_modified and etag
# won't be updated to reflect the changes (they always reflect the
# documents state on the database).
if resource_def["versioning"] is True and version in ["all", "diffs"]:
versions = response
if config.DOMAIN[resource]["hateoas"]:
versions = response[config.ITEMS]

if version == "diffs":
getattr(app, "on_fetched_diffs")(resource, versions)
getattr(app, "on_fetched_diffs_%s" % resource)(versions)
else:
for version_item in versions:
getattr(app, "on_fetched_item")(resource, version_item)
getattr(app, "on_fetched_item_%s" % resource)(version_item)
else:
getattr(app, "on_fetched_item")(resource, response)
getattr(app, "on_fetched_item_%s" % resource)(response)
else:
getattr(app, "on_fetched_item")(resource, response)
getattr(app, "on_fetched_item_%s" % resource)(response)

return response, last_modified, etag, 200

Expand Down
59 changes: 59 additions & 0 deletions eve/tests/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,65 @@ def test_on_fetched_item_contacts(self):

# TODO: also test with HATEOS off

def test_on_fetched_diffs(self):
""" Verify that on_fetched_item events are fired for
version=diffs requests.
"""
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=1"
)
self.assertEqual(None, devent.called)

# check for ?version=all requests
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=all"
)
self.assertEqual(None, devent.called)

# check for ?version=diffs requests
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=diffs"
)
self.assertEqual(self.known_resource, devent.called[0])
self.assertEqual(2, len(devent.called))

def test_on_fetched_diffs_contacts(self):
""" Verify that on_fetched_diffs_contacts events are fired for
version=diffs requests.
"""
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs_contacts += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=1"
)
self.assertEqual(None, devent.called)

# check for ?version=all requests
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs_contacts += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=all"
)
self.assertEqual(None, devent.called)

# check for ?version=diffs requests
devent = DummyEvent(lambda: True)
self.app.on_fetched_diffs_contacts += devent
response, status = self.get(
self.known_resource, item=self.item_id, query="?version=diffs"
)
# Verify first document has id_field
self.assertEqual(self.item_id, str(devent.called[0][0][self.id_field]))
self.assertEqual(1, len(devent.called))

# TODO: also test with HATEOS off

def test_getitem_version_diffs(self):
""" Verify that the first document is returned in its entirety and that
subsequent documents are simply diff to the previous version.
Expand Down

0 comments on commit 4898012

Please sign in to comment.