From ae3b3a4a4fb0636e38bb5b7d50797f3ececa3b98 Mon Sep 17 00:00:00 2001 From: "matthias@matsewe.de" Date: Fri, 24 May 2024 18:21:09 +0200 Subject: [PATCH] format, sort categories --- app/main.py | 20 ++++++++++++++------ app/models.py | 1 + app/routers/songs.py | 2 +- app/schemas.py | 3 ++- templates/voting.html | 8 ++++---- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 2df9570..9adaa77 100644 --- a/app/main.py +++ b/app/main.py @@ -22,6 +22,7 @@ app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") + @app.get("/") async def root(request: Request) -> HTMLResponse: return templates.TemplateResponse( @@ -29,22 +30,29 @@ async def root(request: Request) -> HTMLResponse: ) - @app.get("/vote") -async def vote(request: Request, session_id : str, db: Annotated[Session, Depends(get_db)]) -> HTMLResponse: - songs = [Song(**s.__dict__, vote=v) for s, v in get_songs_and_vote_for_session(db, session_id)] +async def vote(request: Request, session_id: str, db: Annotated[Session, Depends(get_db)]) -> HTMLResponse: + songs = [Song(**s.__dict__, vote=v) + for s, v in get_songs_and_vote_for_session(db, session_id)] songs_by_category = {} all_categories = set() + for song in songs: if song.main_category not in songs_by_category: songs_by_category[song.main_category] = [] songs_by_category[song.main_category].append(song) all_categories.update(song.categories.keys()) + + all_categories = list(all_categories) + all_categories.sort() + + print(all_categories) + return templates.TemplateResponse( request=request, name="voting.html", context={ - "songs_by_category": songs_by_category, + "songs_by_category": songs_by_category, "all_categories": {c: i+1 for i, c in enumerate(all_categories)}, "session_id": session_id - } - ) \ No newline at end of file + } + ) diff --git a/app/models.py b/app/models.py index b72a0fe..320aafa 100644 --- a/app/models.py +++ b/app/models.py @@ -34,6 +34,7 @@ class Session(Base): time_updated: Mapped[Optional[datetime] ] = mapped_column(onupdate=func.now()) + class Vote(Base): __tablename__ = 'votes' id: Mapped[int] = mapped_column(primary_key=True) diff --git a/app/routers/songs.py b/app/routers/songs.py index 3893ba2..e4d1b90 100644 --- a/app/routers/songs.py +++ b/app/routers/songs.py @@ -26,4 +26,4 @@ async def vote(song_id: str, session_id: str, vote: int, db: Annotated[Session, @router.get("/evaluation") async def get_evaluation(db: Annotated[Session, Depends(get_db)] = None) -> dict[int, dict[int, int]]: - return get_all_songs_and_votes(db) \ No newline at end of file + return get_all_songs_and_votes(db) diff --git a/app/schemas.py b/app/schemas.py index 8da0965..7ca0410 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -1,6 +1,7 @@ from typing import Optional from pydantic import BaseModel + class Song(BaseModel): id: int og_artist: Optional[str] @@ -15,4 +16,4 @@ class Song(BaseModel): categories: Optional[dict[str, bool]] main_category: Optional[str] singable: Optional[bool] - vote: Optional[int] \ No newline at end of file + vote: Optional[int] diff --git a/templates/voting.html b/templates/voting.html index d6690cb..d05efe7 100644 --- a/templates/voting.html +++ b/templates/voting.html @@ -28,11 +28,11 @@ }) } - $( window ).on("load", activate_session); + $(window).on("load", activate_session); - $( window ).on("beforeunload", deactivate_session); - $( window ).on("pagehide", deactivate_session); - $(document).on('visibilitychange', function() { + $(window).on("beforeunload", deactivate_session); + $(window).on("pagehide", deactivate_session); + $(document).on('visibilitychange', function () { if (document.visibilityState == 'hidden') { deactivate_session(); } else {