From 2b3faa2b26657f250fe903a6ca673df46cc277ff Mon Sep 17 00:00:00 2001 From: Florian Date: Sat, 28 Nov 2026 18:48:02 +0100 Subject: [PATCH] Switched to shared pydantic model --- requirements.txt | 2 +- src/models.py | 72 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index d82d6cd..17db401 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ idna==3.20 lib-db-module==0.4.0 lib-secret-manager==0.3.6 lib-send-notifications==0.3.0 -lib-shared-models==0.2.0 +lib-shared-models[pydantic]==1.0 lib-service-auth==0.1.0 msgspec==0.22.0 mysql-connector-python==26.10.0 diff --git a/src/models.py b/src/models.py index fe692fb..3927675 100644 --- a/src/models.py +++ b/src/models.py @@ -1,37 +1,93 @@ from datetime import datetime -from pydantic import BaseModel, Field, computed_field, field_validator +from pydantic import Field, computed_field, field_validator +from shared_models.pydantic_base import DocumentedModel -class AnimeCreate(BaseModel): - anilist_id: int = Field(gt=0) +class AnimeCreate(DocumentedModel): + """Request to subscribe to an anime. + + Attributes: + seadex_id: Id of the anime on SeaDex. SeaDex mirrors the AniList ids, + so this is also the anime's AniList id. Must be positive. + custom_name: Name the subscriber wants to see in the notifications + for this anime, at most 255 characters. If it is not set, the + name stored from AniList is used. Blank input becomes None. + """ + seadex_id: int = Field(gt=0) custom_name: str | None = Field(default=None, max_length=255) @field_validator("custom_name", mode="before") @classmethod def blank_to_none(cls, value): + """Strips the custom name and turns a blank one into None. + + Runs before validation, so whitespace does not count towards the + length limit. + + Args: + value: The supplied custom name. Anything that is not a string + is passed on unchanged for the normal validation to handle. + + Returns: + The stripped name, or None if the string was blank. Other values + are returned as they are. + """ if isinstance(value, str): return value.strip() or None return value -class AnimeRemove(BaseModel): - anilist_id: int +class AnimeRemove(DocumentedModel): + """Request to unsubscribe from an anime. -class AnimeEntry(BaseModel): + Attributes: + seadex_id: Id of the anime on SeaDex, which is also its AniList id. + """ + seadex_id: int + +class AnimeEntry(DocumentedModel): + """A subscribed anime, as returned to the subscriber. + + Attributes: + name: The anime's name, as stored from AniList. + custom_name: Name chosen by the subscriber for the notifications, + if any. When it is None, ``name`` is what notifications show. + anilist_id: AniList id of the anime. SeaDex uses the same ids. + published: When the best torrent SeaDex currently lists for this + anime was published, if known. + """ name: str custom_name: str | None = None anilist_id: int published: datetime | None = None -class Counts(BaseModel): +class Counts(DocumentedModel): + """Number of subscriptions per state. + + Attributes: + active: Number of active subscriptions. + inactive: Number of inactive subscriptions. + """ active: int inactive: int -class AnimeList(BaseModel): +class AnimeList(DocumentedModel): + """All subscriptions, split by state. + + Attributes: + active: Active subscriptions. + inactive: Inactive subscriptions. + counts: Computed. The size of each list. + """ active: list[AnimeEntry] = Field(default_factory=list) inactive: list[AnimeEntry] = Field(default_factory=list) @computed_field @property def counts(self) -> Counts: + """Counts the entries in each list. + + Returns: + A Counts with the number of active and inactive subscriptions. + """ return Counts(active=len(self.active),inactive=len(self.inactive)) \ No newline at end of file