-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] extendable_fastapi: Add PagedCollection schema
A specialized verion of the PagedCollection from odoo.addons.fastapi.schemas but supporting extendable models as type a args.
- Loading branch information
Showing
5 changed files
with
22 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters