Skip to content

Commit d181773

Browse files
committed
Simplify API model unions usage
1 parent 35d5000 commit d181773

File tree

6 files changed

+109
-122
lines changed

6 files changed

+109
-122
lines changed

Tekst-API/tekst/models/browse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from tekst.models.common import ModelBase
22
from tekst.models.location import LocationRead
3-
from tekst.resources import AnyContentReadBody
3+
from tekst.resources import AnyContentRead
44

55

66
class LocationData(ModelBase):
77
location_path: list[LocationRead] = []
8-
contents: list[AnyContentReadBody] = []
8+
contents: list[AnyContentRead] = []

Tekst-API/tekst/models/common.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,17 @@ class TranslationBase(TypedDict):
7474
)
7575

7676

77-
class ModelTransformerMixin:
78-
@classmethod
79-
def model_from(cls, obj: BaseModel) -> BaseModel:
80-
return cls.model_validate(obj, from_attributes=True)
81-
82-
83-
class ModelBase(ModelTransformerMixin, BaseModel):
77+
class ModelBase(BaseModel):
8478
model_config = ConfigDict(
8579
alias_generator=camelize,
8680
populate_by_name=True,
8781
from_attributes=True,
8882
)
8983

84+
@classmethod
85+
def model_from(cls, obj: BaseModel) -> BaseModel:
86+
return cls.model_validate(obj, from_attributes=True)
87+
9088
@classmethod
9189
def _field_excluded_from_model_variant(
9290
cls, field_name: str, model_variant: Literal["create", "update"]
@@ -109,7 +107,7 @@ def _field_excluded_from_model_variant(
109107
return False
110108

111109

112-
class DocumentBase(ModelTransformerMixin, Document):
110+
class DocumentBase(Document):
113111
"""Base model for all Tekst ODMs"""
114112

115113
class Settings:

Tekst-API/tekst/resources/__init__.py

Lines changed: 76 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -462,117 +462,107 @@ def init_resource_types_mgr() -> None:
462462

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

465-
# CREATE
466-
AnyResourceCreate = Union[ # noqa: UP007
467-
tuple(
468-
[
469-
rt.resource_model().create_model()
470-
for rt in resource_types_mgr.get_all().values()
471-
]
472-
)
473-
]
474-
AnyResourceCreateBody = Annotated[
475-
AnyResourceCreate,
465+
AnyResourceCreate = Annotated[
466+
Union[ # noqa: UP007
467+
tuple(
468+
[
469+
rt.resource_model().create_model()
470+
for rt in resource_types_mgr.get_all().values()
471+
]
472+
)
473+
],
476474
Body(discriminator="resource_type"),
475+
Field(discriminator="resource_type"),
477476
]
478477

479-
# READ
480-
AnyResourceRead = Union[ # noqa: UP007
481-
tuple(
482-
[
483-
rt.resource_model().read_model()
484-
for rt in resource_types_mgr.get_all().values()
485-
]
486-
)
487-
]
488-
AnyResourceReadBody = Annotated[
489-
AnyResourceRead,
478+
AnyResourceRead = Annotated[
479+
Union[ # noqa: UP007
480+
tuple(
481+
[
482+
rt.resource_model().read_model()
483+
for rt in resource_types_mgr.get_all().values()
484+
]
485+
)
486+
],
490487
Body(discriminator="resource_type"),
488+
Field(discriminator="resource_type"),
491489
]
492490

493-
# UPDATE
494-
AnyResourceUpdate = Union[ # noqa: UP007
495-
tuple(
496-
[
497-
rt.resource_model().update_model()
498-
for rt in resource_types_mgr.get_all().values()
499-
]
500-
)
501-
]
502-
AnyResourceUpdateBody = Annotated[
503-
AnyResourceUpdate,
491+
AnyResourceUpdate = Annotated[
492+
Union[ # noqa: UP007
493+
tuple(
494+
[
495+
rt.resource_model().update_model()
496+
for rt in resource_types_mgr.get_all().values()
497+
]
498+
)
499+
],
504500
Body(discriminator="resource_type"),
505-
]
506-
507-
# DOCUMENT
508-
AnyResourceDocument = Union[ # noqa: UP007
509-
tuple(
510-
[
511-
rt.resource_model().document_model()
512-
for rt in resource_types_mgr.get_all().values()
513-
]
514-
)
501+
Field(discriminator="resource_type"),
515502
]
516503

517504

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

520-
# CREATE
521-
AnyContentCreate = Union[ # noqa: UP007
522-
tuple(
523-
[
524-
rt.content_model().create_model()
525-
for rt in resource_types_mgr.get_all().values()
526-
]
527-
)
528-
]
529-
AnyContentCreateBody = Annotated[
530-
AnyContentCreate,
507+
AnyContentCreate = Annotated[
508+
Union[ # noqa: UP007
509+
tuple(
510+
[
511+
rt.content_model().create_model()
512+
for rt in resource_types_mgr.get_all().values()
513+
]
514+
)
515+
],
531516
Body(discriminator="resource_type"),
517+
Field(discriminator="resource_type"),
532518
]
533519

534-
# READ
535-
AnyContentRead = Union[ # noqa: UP007
536-
tuple(
537-
[
538-
rt.content_model().read_model()
539-
for rt in resource_types_mgr.get_all().values()
540-
]
541-
)
542-
]
543-
AnyContentReadBody = Annotated[
544-
AnyContentRead,
520+
AnyContentRead = Annotated[
521+
Union[ # noqa: UP007
522+
tuple(
523+
[
524+
rt.content_model().read_model()
525+
for rt in resource_types_mgr.get_all().values()
526+
]
527+
)
528+
],
545529
Body(discriminator="resource_type"),
530+
Field(discriminator="resource_type"),
546531
]
547532

548-
# UPDATE
549-
AnyContentUpdate = Union[ # noqa: UP007
550-
tuple(
551-
[
552-
rt.content_model().update_model()
553-
for rt in resource_types_mgr.get_all().values()
554-
]
555-
)
556-
]
557-
AnyContentUpdateBody = Annotated[
558-
AnyContentUpdate,
533+
AnyContentUpdate = Annotated[
534+
Union[ # noqa: UP007
535+
tuple(
536+
[
537+
rt.content_model().update_model()
538+
for rt in resource_types_mgr.get_all().values()
539+
]
540+
)
541+
],
559542
Body(discriminator="resource_type"),
543+
Field(discriminator="resource_type"),
560544
]
561545

562-
# DOCUMENT
563-
AnyContentDocument = Union[ # noqa: UP007
564-
tuple(
565-
[
566-
rt.content_model().document_model()
567-
for rt in resource_types_mgr.get_all().values()
568-
]
569-
)
546+
AnyContentDocument = Annotated[
547+
Union[ # noqa: UP007
548+
tuple(
549+
[
550+
rt.content_model().document_model()
551+
for rt in resource_types_mgr.get_all().values()
552+
]
553+
)
554+
],
555+
Body(discriminator="resource_type"),
556+
Field(discriminator="resource_type"),
570557
]
571558

572-
# ANY RESOURCE SEARCH QUERY
559+
560+
# ### CREATE UNION TYPE ALIASES FOR MODELS OF RESOURCE TYPE-SPECIFIC SEARCH QUERIES
561+
573562
AnyResourceSearchQuery = Annotated[
574563
Union[ # noqa: UP007
575564
tuple([rt.search_query_model() for rt in resource_types_mgr.get_all().values()])
576565
],
566+
Body(discriminator="resource_type"),
577567
Field(discriminator="resource_type"),
578568
]

Tekst-API/tekst/routers/browse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from tekst.models.resource import (
1616
ResourceBaseDocument,
1717
)
18-
from tekst.resources import AnyContentReadBody
18+
from tekst.resources import AnyContentRead
1919

2020

2121
# initialize content router
@@ -27,7 +27,7 @@
2727

2828
@router.get(
2929
"/content-siblings",
30-
response_model=list[AnyContentReadBody],
30+
response_model=list[AnyContentRead],
3131
status_code=status.HTTP_200_OK,
3232
responses=errors.responses(
3333
[

Tekst-API/tekst/routers/contents.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from tekst.models.content import ContentBaseDocument
1010
from tekst.models.resource import ResourceBaseDocument
1111
from tekst.resources import (
12-
AnyContentCreateBody,
12+
AnyContentCreate,
1313
AnyContentDocument,
14-
AnyContentReadBody,
15-
AnyContentUpdateBody,
14+
AnyContentRead,
15+
AnyContentUpdate,
1616
resource_types_mgr,
1717
)
1818
from tekst.search import set_index_ood
@@ -27,7 +27,7 @@
2727

2828
@router.post(
2929
"",
30-
response_model=AnyContentReadBody,
30+
response_model=AnyContentRead,
3131
status_code=status.HTTP_201_CREATED,
3232
responses=errors.responses(
3333
[
@@ -37,7 +37,7 @@
3737
),
3838
)
3939
async def create_content(
40-
content: AnyContentCreateBody,
40+
content: AnyContentCreate,
4141
user: UserDep,
4242
) -> AnyContentDocument:
4343
# check if the resource this content belongs to is writable by user
@@ -73,7 +73,7 @@ async def create_content(
7373

7474
@router.get(
7575
"/{id}",
76-
response_model=AnyContentReadBody,
76+
response_model=AnyContentRead,
7777
status_code=status.HTTP_200_OK,
7878
responses=errors.responses(
7979
[
@@ -101,7 +101,7 @@ async def get_content(
101101

102102
@router.patch(
103103
"/{id}",
104-
response_model=AnyContentReadBody,
104+
response_model=AnyContentRead,
105105
status_code=status.HTTP_200_OK,
106106
responses=errors.responses(
107107
[
@@ -113,7 +113,7 @@ async def get_content(
113113
)
114114
async def update_content(
115115
content_id: Annotated[PydanticObjectId, Path(alias="id")],
116-
updates: AnyContentUpdateBody,
116+
updates: AnyContentUpdate,
117117
user: UserDep,
118118
) -> AnyContentDocument:
119119
content_doc = await ContentBaseDocument.get(content_id, with_children=True)
@@ -176,7 +176,7 @@ async def delete_content(
176176

177177
@router.get(
178178
"",
179-
response_model=list[AnyContentReadBody],
179+
response_model=list[AnyContentRead],
180180
status_code=status.HTTP_200_OK,
181181
)
182182
async def find_contents(

0 commit comments

Comments
 (0)