Skip to content

Commit

Permalink
[IMP] extendable_fastapi: Add generics schemas
Browse files Browse the repository at this point in the history
Add base schemas to be used to define pagination and paginated results when defining a search router.
  • Loading branch information
lmignon committed Oct 11, 2023
1 parent dd3f433 commit 4c0428a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Empty file.
6 changes: 6 additions & 0 deletions extendable_fastapi/readme/newsfragments/380.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Introduce two new base schemas: `PagedCollection` and `Paging`. These
schemas are used to describe the structure of a paged collection of
resources and the paging information parameters that you can use when you
define a fastapi method that returns a paged collection of resources.
These schemas are similar to the ones defined in the Odoo's **fastapi** addon
but works as/with extendable models.
29 changes: 28 additions & 1 deletion extendable_fastapi/schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Generic, TypeVar

from extendable_pydantic import ExtendableBaseModel


Expand All @@ -11,7 +13,7 @@ class StrictExtendableBaseModel(
An ExtendableBaseModel with strict validation.
By default, Pydantic does not revalidate instances during validation, nor
when the data is changed. Validation only occurs when the model is created.
when the data are 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
Expand All @@ -24,3 +26,28 @@ class StrictExtendableBaseModel(
* validate_assignment=True: revalidate the model when the data is changed (default is False)
* extra="forbid": Forbid any extra attributes (default is "ignore")
"""


T = TypeVar("T")


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]


class Paging(StrictExtendableBaseModel):
"""Paging parameters
By default, the limit is set to 80. To disable the limit set it to null.
"""

limit: int | None = 80
offset: int = 0

0 comments on commit 4c0428a

Please sign in to comment.