In my app I have a Hook that should update a field in a resource: In another post someone used the patch_internal method, but I did not understand, how.
from my_application import app
from eve.methods.patch import patch_internal
def my_hook(...):
with app.test_request_context():
patch_internal("my_resource", payload={"bar": "bar_value"}, lookup={"foo": "foo_value"})
I tried to use PRESERVE_CONTEXT_ON_EXCEPTION = False
in settings.py
.
Depending on the way I try it, I get either
a 404 for the original request, that triggered the hook in the first place,
or an AssertionError:
.
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File ".../lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File ".../lib/python2.7/site-packages/flask/app.py", line 1825, in wsgi_app
ctx.auto_pop(error)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 374, in auto_pop
self.pop(exc)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 357, in pop
% (rv, self)
AssertionError: Popped wrong request context.
(<RequestContext 'http://127.0.0.1:5001/' [GET] of eve> instead of <RequestContext 'http://127.0.0.1:5001/my_endpoint' [GET] of eve>)
My question is:
What are the proper parameters for patch_internal? How do I tell Eve, which item I want to change?
This is a trivial example that should work:
from eve import Eve
from eve.methods.patch import patch_internal
app = Eve()
def my_hook(*args):
with app.test_request_context():
payload = {"bar": "bar_value"}
lookup = {"_id": "4f71e038c88e201118000002"}
patch_internal("my_resource", payload, **lookup)
# this is rather stupid. We're going to patch the same document on *every* GET request, but you get the point
app.on_post_GET += my_hook
if __name__ == '__main__':
app.run()
Or you could check the patch internal test for a slightly more articulate example. Also here is a good post_internal example. Hope this helps.