From dfdc908e3010d08d54983466f9078dadf9bd1995 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Wed, 11 Oct 2023 08:53:16 +0200 Subject: [PATCH] [IMP] extendable_fastapi: Add generics schemas Add base schemas to be used to define pagination and paginated results when defining a search method. --- .../readme/newsfragments/.gitignore | 0 .../readme/newsfragments/380.feature | 6 ++++ extendable_fastapi/schemas.py | 29 ++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 extendable_fastapi/readme/newsfragments/.gitignore create mode 100644 extendable_fastapi/readme/newsfragments/380.feature diff --git a/extendable_fastapi/readme/newsfragments/.gitignore b/extendable_fastapi/readme/newsfragments/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/extendable_fastapi/readme/newsfragments/380.feature b/extendable_fastapi/readme/newsfragments/380.feature new file mode 100644 index 000000000..b24a4cf64 --- /dev/null +++ b/extendable_fastapi/readme/newsfragments/380.feature @@ -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. diff --git a/extendable_fastapi/schemas.py b/extendable_fastapi/schemas.py index dfa7a1664..c8d4650b0 100644 --- a/extendable_fastapi/schemas.py +++ b/extendable_fastapi/schemas.py @@ -1,3 +1,5 @@ +from typing import Generic, TypeVar + from extendable_pydantic import ExtendableBaseModel @@ -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 @@ -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