pyeve / eve-sqlalchemy

SQLAlchemy data layer for Eve-powered RESTful APIs
http://eve-sqlalchemy.readthedocs.io
Other
232 stars 70 forks source link

How to define a custom filter and bind it to a resource endpoint #98

Closed m3talstorm closed 7 years ago

m3talstorm commented 8 years ago

I'm using Python EVE, EVE-SQLALchemy, Flask, Flask-SQLALchemy, SQLALchemy and a Postgres database.

I have my API endpoints all setup and they are working fine out the box.

Now i'm trying to filter the items returned by the _somemodel resource so that it only returns items (GET) that belong to a certain user (the user doing the API request) or have a certain name (prefix). The SQlAlcehmy code to perform this is below:

session.query(SomeModel).filter(or_(SomeModel.name.like('SOMETHING_%'), SomeModel.account_id==1)).all()

I've looked at pre and post request hooks, User-Restricted Resource Access and everything else I could find, but nowhere seems to describe how to apply custom filters to (all) GET requests of a certain endpoint/resource.

Kind of like the URL filter functionality, but always on and filtering at the database level, not the serialization/response level.

dkellner commented 7 years ago

Have you looked at predefined database filters (http://python-eve.org/config.html#filter) ?

I will close this as your question is unfortunately quite old already and I doubt you still wait for an answer - sorry for that! Feel free to reopen it at any time if needed :).