48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
from urllib.parse import quote
|
|
|
|
from pydantic import BaseModel, ConfigDict, field_validator
|
|
|
|
|
|
def encode_path(path: str) -> str:
|
|
if not path:
|
|
return ""
|
|
return "/".join(quote(part, safe="") for part in path.split("/"))
|
|
|
|
|
|
class AudioSchema(BaseModel):
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
path: str
|
|
name: str
|
|
filename: str
|
|
preview_path: str
|
|
playlist_id: int | None = None
|
|
|
|
@field_validator("path", "preview_path", mode="before")
|
|
@classmethod
|
|
def encode_urls(cls, v):
|
|
return encode_path(v)
|
|
|
|
@field_validator("path", mode="after")
|
|
@classmethod
|
|
def add_audio_to_url(cls, v):
|
|
if not v: return v
|
|
clean_v = v.lstrip('/')
|
|
if clean_v.startswith('audio/'):
|
|
return f"{clean_v}"
|
|
return f"audio/{clean_v}"
|
|
|
|
|
|
|
|
|
|
class Audios(BaseModel):
|
|
audios: list[AudioSchema]
|
|
|
|
class Playlist(BaseModel):
|
|
id: int
|
|
title: str
|
|
audios_count: int
|
|
|
|
class Playlists(BaseModel):
|
|
playlists: list[Playlist] |