Implement Veto Mode

This commit is contained in:
matthias@matsewe.de
2024-05-29 10:33:42 +02:00
parent 010d6fc8d6
commit 0546a88e32
9 changed files with 150 additions and 24 deletions

View File

@@ -4,10 +4,11 @@ from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from app.database import engine, Base, get_db
from app.crud import get_songs_and_vote_for_session
from app.crud import get_songs_and_vote_for_session, get_setting
from sqlalchemy.orm import Session
from typing import Annotated
from app.schemas import Song
import json
Base.metadata.create_all(engine)
@@ -31,17 +32,23 @@ 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: Session = Depends(get_db)) -> HTMLResponse:
veto_mode = get_setting(db, "veto_mode")
songs = [Song(**s.__dict__, vote=v, vote_comment=c)
for s, v, c in get_songs_and_vote_for_session(db, session_id)]
songs_by_category = {}
all_categories = set()
wildcard_songs = []
current_songs = []
other_songs = []
for song in songs:
if (not song.singable) and (not veto_mode):
continue
if song.is_current:
current_songs.append(song)
continue
@@ -50,26 +57,35 @@ async def vote(request: Request, session_id: str, db: Annotated[Session, Depends
wildcard_songs.append(song)
continue
if not song.main_category:
other_songs.append(song)
continue
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())
songs_by_category["Sonstige"] = other_songs
songs_by_category["Wildcard (nicht a cappella)"] = wildcard_songs
songs_by_category["Aktuelles Programm"] = current_songs
all_categories = list(all_categories)
all_categories.sort()
all_categories.append("Sonstige")
all_categories.append("Wildcard (nicht a cappella)")
all_categories.append("Aktuelles Programm")
print(all_categories)
# print(all_categories)
# with open('/data/songs_by_cat.json', 'w') as f:
# json.dump({cat : [s.__dict__ for s in songs] for cat, songs in songs_by_category.items()}, f)
return templates.TemplateResponse(
request=request, name="voting.html", context={
"songs_by_category": songs_by_category,
"all_categories": {c: i+1 for i, c in enumerate(all_categories)},
"session_id": session_id
"session_id": session_id,
"veto_mode": veto_mode
}
)