format, sort categories
This commit is contained in:
12
app/main.py
12
app/main.py
@@ -22,6 +22,7 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|||||||
|
|
||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root(request: Request) -> HTMLResponse:
|
async def root(request: Request) -> HTMLResponse:
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
@@ -29,18 +30,25 @@ async def root(request: Request) -> HTMLResponse:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/vote")
|
@app.get("/vote")
|
||||||
async def vote(request: Request, session_id: str, db: Annotated[Session, Depends(get_db)]) -> HTMLResponse:
|
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 = [Song(**s.__dict__, vote=v)
|
||||||
|
for s, v in get_songs_and_vote_for_session(db, session_id)]
|
||||||
|
|
||||||
songs_by_category = {}
|
songs_by_category = {}
|
||||||
all_categories = set()
|
all_categories = set()
|
||||||
|
|
||||||
for song in songs:
|
for song in songs:
|
||||||
if song.main_category not in songs_by_category:
|
if song.main_category not in songs_by_category:
|
||||||
songs_by_category[song.main_category] = []
|
songs_by_category[song.main_category] = []
|
||||||
songs_by_category[song.main_category].append(song)
|
songs_by_category[song.main_category].append(song)
|
||||||
all_categories.update(song.categories.keys())
|
all_categories.update(song.categories.keys())
|
||||||
|
|
||||||
|
all_categories = list(all_categories)
|
||||||
|
all_categories.sort()
|
||||||
|
|
||||||
|
print(all_categories)
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request=request, name="voting.html", context={
|
request=request, name="voting.html", context={
|
||||||
"songs_by_category": songs_by_category,
|
"songs_by_category": songs_by_category,
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ class Session(Base):
|
|||||||
time_updated: Mapped[Optional[datetime]
|
time_updated: Mapped[Optional[datetime]
|
||||||
] = mapped_column(onupdate=func.now())
|
] = mapped_column(onupdate=func.now())
|
||||||
|
|
||||||
|
|
||||||
class Vote(Base):
|
class Vote(Base):
|
||||||
__tablename__ = 'votes'
|
__tablename__ = 'votes'
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class Song(BaseModel):
|
class Song(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
og_artist: Optional[str]
|
og_artist: Optional[str]
|
||||||
|
|||||||
Reference in New Issue
Block a user