Skip to content

Commit

Permalink
Simplify API model unions usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bkis committed Nov 14, 2024
1 parent 35d5000 commit d181773
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 122 deletions.
4 changes: 2 additions & 2 deletions Tekst-API/tekst/models/browse.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from tekst.models.common import ModelBase
from tekst.models.location import LocationRead
from tekst.resources import AnyContentReadBody
from tekst.resources import AnyContentRead


class LocationData(ModelBase):
location_path: list[LocationRead] = []
contents: list[AnyContentReadBody] = []
contents: list[AnyContentRead] = []
14 changes: 6 additions & 8 deletions Tekst-API/tekst/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,17 @@ class TranslationBase(TypedDict):
)


class ModelTransformerMixin:
@classmethod
def model_from(cls, obj: BaseModel) -> BaseModel:
return cls.model_validate(obj, from_attributes=True)


class ModelBase(ModelTransformerMixin, BaseModel):
class ModelBase(BaseModel):
model_config = ConfigDict(
alias_generator=camelize,
populate_by_name=True,
from_attributes=True,
)

@classmethod
def model_from(cls, obj: BaseModel) -> BaseModel:
return cls.model_validate(obj, from_attributes=True)

@classmethod
def _field_excluded_from_model_variant(
cls, field_name: str, model_variant: Literal["create", "update"]
Expand All @@ -109,7 +107,7 @@ def _field_excluded_from_model_variant(
return False


class DocumentBase(ModelTransformerMixin, Document):
class DocumentBase(Document):
"""Base model for all Tekst ODMs"""

class Settings:
Expand Down
162 changes: 76 additions & 86 deletions Tekst-API/tekst/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,117 +462,107 @@ def init_resource_types_mgr() -> None:

# ### create union type aliases for models of any resource type model

# CREATE
AnyResourceCreate = Union[ # noqa: UP007
tuple(
[
rt.resource_model().create_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyResourceCreateBody = Annotated[
AnyResourceCreate,
AnyResourceCreate = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.resource_model().create_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# READ
AnyResourceRead = Union[ # noqa: UP007
tuple(
[
rt.resource_model().read_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyResourceReadBody = Annotated[
AnyResourceRead,
AnyResourceRead = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.resource_model().read_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# UPDATE
AnyResourceUpdate = Union[ # noqa: UP007
tuple(
[
rt.resource_model().update_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyResourceUpdateBody = Annotated[
AnyResourceUpdate,
AnyResourceUpdate = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.resource_model().update_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
]

# DOCUMENT
AnyResourceDocument = Union[ # noqa: UP007
tuple(
[
rt.resource_model().document_model()
for rt in resource_types_mgr.get_all().values()
]
)
Field(discriminator="resource_type"),
]


# ### create union type aliases for models of any content type model
# ### CREATE UNION TYPE ALIASES FOR MODELS OF ANY CONTENT TYPE MODEL

# CREATE
AnyContentCreate = Union[ # noqa: UP007
tuple(
[
rt.content_model().create_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyContentCreateBody = Annotated[
AnyContentCreate,
AnyContentCreate = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.content_model().create_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# READ
AnyContentRead = Union[ # noqa: UP007
tuple(
[
rt.content_model().read_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyContentReadBody = Annotated[
AnyContentRead,
AnyContentRead = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.content_model().read_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# UPDATE
AnyContentUpdate = Union[ # noqa: UP007
tuple(
[
rt.content_model().update_model()
for rt in resource_types_mgr.get_all().values()
]
)
]
AnyContentUpdateBody = Annotated[
AnyContentUpdate,
AnyContentUpdate = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.content_model().update_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# DOCUMENT
AnyContentDocument = Union[ # noqa: UP007
tuple(
[
rt.content_model().document_model()
for rt in resource_types_mgr.get_all().values()
]
)
AnyContentDocument = Annotated[
Union[ # noqa: UP007
tuple(
[
rt.content_model().document_model()
for rt in resource_types_mgr.get_all().values()
]
)
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]

# ANY RESOURCE SEARCH QUERY

# ### CREATE UNION TYPE ALIASES FOR MODELS OF RESOURCE TYPE-SPECIFIC SEARCH QUERIES

AnyResourceSearchQuery = Annotated[
Union[ # noqa: UP007
tuple([rt.search_query_model() for rt in resource_types_mgr.get_all().values()])
],
Body(discriminator="resource_type"),
Field(discriminator="resource_type"),
]
4 changes: 2 additions & 2 deletions Tekst-API/tekst/routers/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from tekst.models.resource import (
ResourceBaseDocument,
)
from tekst.resources import AnyContentReadBody
from tekst.resources import AnyContentRead


# initialize content router
Expand All @@ -27,7 +27,7 @@

@router.get(
"/content-siblings",
response_model=list[AnyContentReadBody],
response_model=list[AnyContentRead],
status_code=status.HTTP_200_OK,
responses=errors.responses(
[
Expand Down
18 changes: 9 additions & 9 deletions Tekst-API/tekst/routers/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from tekst.models.content import ContentBaseDocument
from tekst.models.resource import ResourceBaseDocument
from tekst.resources import (
AnyContentCreateBody,
AnyContentCreate,
AnyContentDocument,
AnyContentReadBody,
AnyContentUpdateBody,
AnyContentRead,
AnyContentUpdate,
resource_types_mgr,
)
from tekst.search import set_index_ood
Expand All @@ -27,7 +27,7 @@

@router.post(
"",
response_model=AnyContentReadBody,
response_model=AnyContentRead,
status_code=status.HTTP_201_CREATED,
responses=errors.responses(
[
Expand All @@ -37,7 +37,7 @@
),
)
async def create_content(
content: AnyContentCreateBody,
content: AnyContentCreate,
user: UserDep,
) -> AnyContentDocument:
# check if the resource this content belongs to is writable by user
Expand Down Expand Up @@ -73,7 +73,7 @@ async def create_content(

@router.get(
"/{id}",
response_model=AnyContentReadBody,
response_model=AnyContentRead,
status_code=status.HTTP_200_OK,
responses=errors.responses(
[
Expand Down Expand Up @@ -101,7 +101,7 @@ async def get_content(

@router.patch(
"/{id}",
response_model=AnyContentReadBody,
response_model=AnyContentRead,
status_code=status.HTTP_200_OK,
responses=errors.responses(
[
Expand All @@ -113,7 +113,7 @@ async def get_content(
)
async def update_content(
content_id: Annotated[PydanticObjectId, Path(alias="id")],
updates: AnyContentUpdateBody,
updates: AnyContentUpdate,
user: UserDep,
) -> AnyContentDocument:
content_doc = await ContentBaseDocument.get(content_id, with_children=True)
Expand Down Expand Up @@ -176,7 +176,7 @@ async def delete_content(

@router.get(
"",
response_model=list[AnyContentReadBody],
response_model=list[AnyContentRead],
status_code=status.HTTP_200_OK,
)
async def find_contents(
Expand Down
Loading

0 comments on commit d181773

Please sign in to comment.