flask-peewee icon indicating copy to clipboard operation
flask-peewee copied to clipboard

REST API post-processors

Open rudimk opened this issue 10 years ago • 1 comments

When a request(GET/POST) is made to a model using the in-built API module, can one execute a post-processor? For instance, if I have a City model, and I make a POST request to add a new user, which then fires up a function to, say, get the city's co-ordinates from the Google Maps API?

rudimk avatar Apr 09 '14 06:04 rudimk

Might be a bit late now, but you could subclass RestResource and override create like so:

class PostSaveResource(RestResource):

    def save_object(self, instance, raw_data):
        saved_instance = super(PostSaveResource, self).save_object(instance, raw_data)
        #Do whatever you need to with the instance here
        return saved_instance

Note that the same method is also called on saving existing instances, not just new ones. There are separate edit and create-methods, but you'd have to duplicate them partially to get hold of the instance and not just the response object.

jmtoball avatar Jun 29 '14 01:06 jmtoball