M. Mojrian
M. Mojrian

Reputation: 31

Get info of an item from database in pre-put callback in Eve

I have items with subId and some other fields stored in mongoDB. Before the PUT request is submitted, I want to check if the subId which is sent in the header of the PUT request is equal to the subId of the desired item which is stored in mongoDB. So, I decided to use pre_put_callback to do this check. Now I can get the subId from header of the request in this function, but I don't know how to access the subId of the item from mongoDB whose _id is provided by the user as the path of the request in pre_put_callback function. Is there a way to access the subId of that item (which is stored in MongoDB) from pre_put_callback function?

Upvotes: 0

Views: 75

Answers (1)

M. Mojrian
M. Mojrian

Reputation: 31

You can access an item of MongoDB database from a pre-request event hook such as pre_put_callback using current_app of flask package. For example for pre_PUT event hook it can be accessed as below:

from flask import current_app as app
from eve import Eve

def pre_put_callback(resource, request, lookup):
    resource_db = app.data.driver.db[resource]
    item = resource_db.find_one(lookup)
    print("DB Item: ", item)

app = Eve()
app.on_pre_PUT += pre_put_callback

app.run()

Upvotes: 1

Related Questions