Skip to content

Commit

Permalink
[IMP] extendable_fastapi: Add PagedCollection schema
Browse files Browse the repository at this point in the history
A specialized verion of the PagedCollection from
odoo.addons.fastapi.schemas but supporting extendable models as type a
args.
  • Loading branch information
lmignon committed Oct 11, 2023
1 parent dd3f433 commit dad0fb4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion extendable_fastapi/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"external_dependencies": {
"python": [
"pydantic>=2.0.0",
"extendable-pydantic>=1.1.0",
"extendable-pydantic>=1.2.0",
],
},
"installable": True,
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions extendable_fastapi/readme/newsfragments/380.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* New base schemas: *PagedCollection*. This schema is used to
the structure of a paged collection of resources. This schema is similar
to the ones defined in the Odoo's **fastapi** addon but works as/with
extendable models.

* The *StrictExtendableBaseModel* has been moved to the *extendable_pydantic*
python lib. You should consider to import it from there.
35 changes: 13 additions & 22 deletions extendable_fastapi/schemas.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
from extendable_pydantic import ExtendableBaseModel
from typing import Generic, TypeVar

from extendable_pydantic import StrictExtendableBaseModel

class StrictExtendableBaseModel(
ExtendableBaseModel,
revalidate_instances="always",
validate_assignment=True,
extra="forbid",
):
"""
An ExtendableBaseModel with strict validation.
T = TypeVar("T")

By default, Pydantic does not revalidate instances during validation, nor
when the data is changed. Validation only occurs when the model is created.
This is not suitable for a REST API, where the data is changed after the
model is created or the model is created from a partial set of data and
then updated with more data. This class enforces strict validation by
forcing the revalidation of instances when the method `model_validate` is
called and by ensuring that the values assignment is validated.

The following parameters are added:
* revalidate_instances="always": model instances are revalidated during validation
(default is "never")
* validate_assignment=True: revalidate the model when the data is changed (default is False)
* extra="forbid": Forbid any extra attributes (default is "ignore")
"""
class PagedCollection(StrictExtendableBaseModel, Generic[T]):
"""A paged collection of items"""

# This is a generic model. The type of the items is defined by the generic type T.
# It provides you a common way to return a paged collection of items of
# extendable models. It's based on the StrictExtendableBaseModel to ensure
# a strict validation when used within the odoo fastapi framework.

total: int
items: list[T]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apispec>=4.0.0
cerberus
contextvars
extendable-pydantic
extendable-pydantic>=1.1.0
extendable-pydantic>=1.2.0
extendable>=0.0.4
fastapi
graphene
Expand Down

0 comments on commit dad0fb4

Please sign in to comment.