Switched to shared pydantic model
This commit is contained in:
1 parent
edb67f3e18
commit
2b3faa2b26
2 files changed
+65
-9
No files matched your search
+1
-1
@@ -14,7 +14,7 @@ idna==3.20
|
|||||||
lib-db-module==0.4.0
|
lib-db-module==0.4.0
|
||||||
lib-secret-manager==0.3.6
|
lib-secret-manager==0.3.6
|
||||||
lib-send-notifications==0.3.0
|
lib-send-notifications==0.3.0
|
||||||
lib-shared-models==0.2.0
|
lib-shared-models[pydantic]==1.0
|
||||||
lib-service-auth==0.1.0
|
lib-service-auth==0.1.0
|
||||||
msgspec==0.22.0
|
msgspec==0.22.0
|
||||||
mysql-connector-python==26.10.0
|
mysql-connector-python==26.10.0
|
||||||
|
|||||||
+64
-8
@@ -1,37 +1,93 @@
|
|||||||
from datetime import datetime
|
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):
|
class AnimeCreate(DocumentedModel):
|
||||||
anilist_id: int = Field(gt=0)
|
"""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)
|
custom_name: str | None = Field(default=None, max_length=255)
|
||||||
|
|
||||||
@field_validator("custom_name", mode="before")
|
@field_validator("custom_name", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def blank_to_none(cls, value):
|
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):
|
if isinstance(value, str):
|
||||||
return value.strip() or None
|
return value.strip() or None
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class AnimeRemove(BaseModel):
|
class AnimeRemove(DocumentedModel):
|
||||||
anilist_id: int
|
"""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
|
name: str
|
||||||
custom_name: str | None = None
|
custom_name: str | None = None
|
||||||
anilist_id: int
|
anilist_id: int
|
||||||
published: datetime | None = None
|
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
|
active: int
|
||||||
inactive: 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)
|
active: list[AnimeEntry] = Field(default_factory=list)
|
||||||
inactive: list[AnimeEntry] = Field(default_factory=list)
|
inactive: list[AnimeEntry] = Field(default_factory=list)
|
||||||
|
|
||||||
@computed_field
|
@computed_field
|
||||||
@property
|
@property
|
||||||
def counts(self) -> Counts:
|
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))
|
return Counts(active=len(self.active),inactive=len(self.inactive))
|
||||||
Reference in new issue
Block a user