From bcd4f6688ca7c615770f10733293687a3a0912b7 Mon Sep 17 00:00:00 2001 From: cwilvx Date: Tue, 25 Feb 2025 23:57:53 +0300 Subject: [PATCH] fix: stats and plalist images --- app/api/playlist.py | 20 ++++++++++++++++---- app/db/userdata.py | 4 ++-- app/utils/stats.py | 5 ++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/api/playlist.py b/app/api/playlist.py index 99b14f55..80ddf137 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -8,9 +8,11 @@ import pathlib from typing import Any from PIL import UnidentifiedImageError, Image -from pydantic import BaseModel, Field +from pydantic_core import core_schema +from pydantic import BaseModel, Field, GetCoreSchemaHandler + from flask_openapi3 import Tag -from flask_openapi3 import APIBlueprint +from flask_openapi3 import APIBlueprint, FileStorage as _FileStorage from app import models from app.api.apischemas import GenericLimitSchema @@ -251,13 +253,23 @@ def get_playlist(path: PlaylistIDPath, query: GetPlaylistQuery): } +class FileStorage(_FileStorage): + @classmethod + def __get_pydantic_core_schema__( + cls, _source: Any, handler: GetCoreSchemaHandler + ) -> core_schema.CoreSchema: + return core_schema.with_info_plain_validator_function(cls.validate) + + class UpdatePlaylistForm(BaseModel): - image: Any = Field(..., description="The image file") + image: FileStorage = Field(description="The image file") name: str = Field(..., description="The name of the playlist") settings: str = Field( ..., description="The settings of the playlist", - example='{"has_gif": false, "banner_pos": 50, "square_img": false, "pinned": false}', + json_schema_extra={ + "example": '{"has_gif": false, "banner_pos": 50, "square_img": false, "pinned": false}' + }, ) diff --git a/app/db/userdata.py b/app/db/userdata.py index ac74eb5d..a2b4669e 100644 --- a/app/db/userdata.py +++ b/app/db/userdata.py @@ -307,7 +307,7 @@ class FavoritesTable(Base): res = next(result).scalar() if res: - return res[0] + return res return 0 @@ -410,7 +410,7 @@ class PlaylistTable(Base): playlist["userid"] = get_current_userid() result = cls.insert_one(playlist) - return next(result).lastrowid + return result.lastrowid @classmethod def check_exists_by_name(cls, name: str): diff --git a/app/utils/stats.py b/app/utils/stats.py index 97e388ff..959c4ced 100644 --- a/app/utils/stats.py +++ b/app/utils/stats.py @@ -75,7 +75,10 @@ def get_tracks_in_period(start_time: int, end_time: int, userid: int | None = No tracks: dict[str, Track] = {} duration = 0 + total = 0 + for scrobble in scrobbles: + total += 1 if scrobble.trackhash not in tracks: try: track = copy.deepcopy( @@ -92,7 +95,7 @@ def get_tracks_in_period(start_time: int, end_time: int, userid: int | None = No tracks[scrobble.trackhash].playduration += scrobble.duration duration += scrobble.duration - return list(tracks.values()), len(scrobbles), duration + return list(tracks.values()), total, duration T = TypeVar("T")