Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jetbridge/smorest-crud
CRUD for Flask-Smorest (SQLAlchemy, Marshmallow). Eliminate boilerplate.
https://github.com/jetbridge/smorest-crud
Last synced: about 1 month ago
JSON representation
CRUD for Flask-Smorest (SQLAlchemy, Marshmallow). Eliminate boilerplate.
- Host: GitHub
- URL: https://github.com/jetbridge/smorest-crud
- Owner: jetbridge
- License: mit
- Created: 2020-02-11T15:05:48.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-12T15:17:06.000Z (over 3 years ago)
- Last Synced: 2024-10-07T01:05:46.621Z (3 months ago)
- Language: Python
- Size: 196 KB
- Stars: 12
- Watchers: 10
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Documentation Status](https://readthedocs.org/projects/smorest-crud/badge/?version=latest)](https://smorest-crud.readthedocs.io/en/latest/?badge=latest)
# Flask Smorest CRUD
_Why repeat yourself?_This library aims to tie together Flask-SQLAlchemy and Flask-Smorest to implement a sane default but easily customizable CRUD API based on SQLAlchemy models inside of Flask.
# Quickstart
In `create_app()`:
```python
from smorest_crud import CRUD
from flask_jwt_extended import JWTManager, get_current_userapp = Flask()
JWTManager(app)
CRUD(app)app.config.update(
CRUD_GET_USER=get_current_user,
CRUD_ACCESS_CHECKS_ENABLED=True,
SECRET_KEY="wnt2die",
)
```CRUD View:
```python
from flask_smorest import Blueprint
from smorest_crud import ResourceView, CollectionViewpet_blp = Blueprint("pets", "pets", url_prefix="/pet")
@pet_blp.route("")
class PetCollection(CollectionView):
model = Pet
prefetch = [Pet.human, (Pet.human, Human.cars)] # joinedload
access_checks_enabled = Falsecreate_enabled = True
list_enabled = True@pet_blp.response(PetSchema(many=True))
def get(self):
"""List pets."""
query = super().get()
return query.filter_by(name='mischa')@pet_blp.arguments(PetSchema)
@pet_blp.response(PetSchema(many=True))
def post(self, args):
"""Create a pet."""
return super().post(args)@pet_blp.route("/")
class PetResource(ResourceView):
model = Petaccess_checks_enabled = True
get_enabled = True
update_enabled = True
delete_enabled = True@pet_blp.response(PetSchema)
def get(self, pk):
return super().get(pk)@pet_blp.arguments(PetSchema)
@pet_blp.response(PetSchema)
def patch(self, args, pk):
return super().patch(args, pk)@pet_blp.response(PetSchema)
def delete(self, pk):
return super().delete(pk)
```# Who is this for?
This library is only useful if your application uses:
* [Flask-Smorest](https://flask-smorest.readthedocs.io/en/stable/)
* [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/)
* [Flask-JWT-Extended](https://flask-jwt-extended.readthedocs.io/en/stable/)# Where to look?
Look at `smorest_crud/test/app.py`Feedback and comments welcome. What would you like to see? Open an issue or a PR with your thoughts.