Skip to content

Commit

Permalink
Adds Pydantic support. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtiesling authored Dec 19, 2023
1 parent 1a15a6b commit 0a564a2
Show file tree
Hide file tree
Showing 16 changed files with 1,062 additions and 57 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

With Flask-Muck you don't have to worry about the CRUD.

Flask-Muck is a batteries-included framework for automatically generating RESTful APIs with Create, Read,
Flask-Muck is a batteries-included declarative framework for automatically generating RESTful APIs with Create, Read,
Update and Delete (CRUD) endpoints in a Flask/SqlAlchemy application stack in as little as 9 lines of code.


Expand Down Expand Up @@ -68,11 +68,12 @@ MyModelApiView.add_rules_to_blueprint(blueprint)
```

## Features
- Automatic generation of CRUD endpoints.
- Uses a declarative and modular approach to automatically generate CRUD endpoints.
- Built-in search, filter, sort and pagination when listing resources.
- Support for APIs with nested resources (i.e. /api/classrooms/12345/students).
- Fully compatible with any other Flask method-based or class-based views. Mix & match with your existing views.
- Pre and post callbacks configurable on all manipulation endpoints. Allow for adding arbitrary logic before and after Create, Update or Delete operations.
- Supports Marshmallow and Pydantic for schema definitions.

## Documentation

Expand Down Expand Up @@ -124,3 +125,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## Support

Thanks for the stars! They mean nothing but bring me immense satisfaction. Keep 'em coming.

[![Star History Chart](https://api.star-history.com/svg?repos=dtiesling/flask-muck&type=Date)](https://star-history.com/#dtiesling/flask-muck&Date)
14 changes: 7 additions & 7 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ Configuration in Flask-Muck is handled entirely through setting class variables
## FlaskMuckApiView Class Variables

| Class Variable | Description | Required |
| :---------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------: |
|:------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| :------------------------: |
| session `scoped_session` | SqlAlchemy database session used to query and modify the resource. | :octicons-check-circle-16: |
| api_name `str` | Name of the API. Used as the url path appended to your Flask Blueprint. | :octicons-check-circle-16: |
| Model `SqlaModelType` | SqlAlchemy Model used to make queries. | :octicons-check-circle-16: |
| ResponseSchema `type[Schema]` | Marshmallow schema used to serialize the resource returned by any of the views. | :octicons-check-circle-16: |
| ResponseSchema `SerializerType` | Marshmallow schema or Pydantic model used to serialize the resource returned by any of the views. | :octicons-check-circle-16: |
| decorators `list[Callable]` | List of decorators to apply to all views in the API. This is inherited functionality built into Flask's [class-based views](https://flask.palletsprojects.com/en/2.3.x/views/#view-decorators). | |
| parent `Optional[type[FlaskMuckApiView]]` | If set, this API becomes a nested resource API. For more information on nested APIs see the [documentation](nesting_apis.md). | |
| CreateSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the POST request JSON payload sent to create a resource. | |
| UpdateSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the PUT request JSON payload sent to update a resource. | |
| PatchSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the PATCH request JSON payload sent to patch a resource. | |
| DeleteSchema `Optional[type[Schema]]` | Marshmallow schema used to validate the DELETE request JSON payload sent to create a resource. Optional. | |
| DetailSchema `Optional[type[Schema]]` | Optional Marshmallow schema used to serialize the resource returned by the GET /<api_name\>/<ID\>/ endpoint. If this schema is not set the ResponseSchema is used. | |
| CreateSchema `Optional[SerializerType]]` | Marshmallow schema or Pydantic model used to validate the POST request JSON payload sent to create a resource. | |
| UpdateSchema `Optional[SerializerType]]` | Marshmallow schema or Pydantic model used to validate the PUT request JSON payload sent to update a resource. | |
| PatchSchema `Optional[SerializerType]]` | Marshmallow schema or Pydantic model used to validate the PATCH request JSON payload sent to patch a resource. | |
| DeleteSchema `Optional[SerializerType]]` | Marshmallow schema or Pydantic model used to validate the DELETE request JSON payload sent to create a resource. Optional. | |
| DetailSchema `Optional[SerializerType]]` | Optional Marshmallow schema or Pydantic model used to serialize the resource returned by the GET /<api_name\>/<ID\>/ endpoint. If this schema is not set the ResponseSchema is used. | |
| pre_create_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is created. Ideal for validation. | |
| pre_update_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is updated. Ideal for validation. | |
| pre_patch_callbacks `list[type[FlaskMuckCallback]]` | List of callback classes to be called before a resource is patched. Ideal for validation. | |
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ Update and Delete (CRUD) endpoints in a Flask/SqlAlchemy application stack.
*With Flask-Muck you don't have to worry about the CRUD.*

## Features
- Automatic generation of CRUD endpoints.
- Uses a declarative and modular approach to automatically generate CRUD endpoints.
- Built-in search, filter, sort and pagination when listing resources.
- Support for APIs with nested resources (i.e. /api/class/\<ID\>/students).
- Fully compatible with any other Flask method-based or class-based views. Mix & match with your existing views.
- Pre and post callbacks configurable on all manipulation endpoints that allow for arbitrary logic before and after Create, Update or Delete operations.
- Supports Marshmallow and Pydantic for schema definitions.

## License
Flask-Muck is distributed under the [MIT](https://spdx.org/licenses/MIT.html) license.
Expand Down
12 changes: 10 additions & 2 deletions docs/docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ class Teacher(db.Model):
years_teaching = db.Column(db.Integer)
```

## Create Request and Response Marshmallow Schemas
Flask-Muck requires configuring [Marshmallow](https://marshmallow.readthedocs.io/en/stable/) schemas that will be used to validate the payload data for the Create, Update, Patch, and (optionally) Delete endpoints. Additionally, a schema must be supplied that will serialize the endpoint's resource in responses. In this example, a simple schema is defined that can be re-used for all validation and serialization.
## Create Request and Response Schemas
Flask-Muck requires configuring Marshmallow and/or Pydantic classes that will be used to validate the payload data for
the Create, Update, Patch, and (optionally) Delete endpoints. Additionally, a schema must be supplied that will
serialize the endpoint's resource in responses. In this example, a simple schema is defined that can be re-used for
all validation and serialization. In this example you'll be defining [Marshmallow](https://marshmallow.readthedocs.io/en/stable/)
schemas.

!!! tip
In v0.2.0 and higher, Pydantic models can also be used as schemas. You can see an example app using Pydantic models
[here](https://github.com/dtiesling/flask-muck/tree/main/examples/02_pydantic)

```python
from marshmallow import Schema, fields as mf
Expand Down
1 change: 1 addition & 0 deletions examples/00_quickstart/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ install = "*"
marshmallow = "*"
flask-muck = {file = "../.."}
webargs = "*"
pydantic = "*"

[dev-packages]

Expand Down
Loading

0 comments on commit 0a564a2

Please sign in to comment.