format, sort categories
This commit is contained in:
14
app/main.py
14
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,18 +30,25 @@ 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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Song(BaseModel):
|
||||
id: int
|
||||
og_artist: Optional[str]
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user