Adapt for current songs
This commit is contained in:
20
app/crud.py
20
app/crud.py
@@ -2,10 +2,12 @@ import app.models as models
|
|||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from sqlalchemy.orm.attributes import flag_modified
|
from sqlalchemy.orm.attributes import flag_modified
|
||||||
|
|
||||||
|
|
||||||
def get_songs_and_vote_for_session(db, session_name) -> list[models.Song]:
|
def get_songs_and_vote_for_session(db, session_name) -> list[models.Song]:
|
||||||
session_entry = activate_session(db, session_name)
|
session_entry = activate_session(db, session_name)
|
||||||
|
|
||||||
votes = db.query(models.Vote).filter(models.Vote.session_id == session_entry.id).subquery()
|
votes = db.query(models.Vote).filter(
|
||||||
|
models.Vote.session_id == session_entry.id).subquery()
|
||||||
|
|
||||||
songs_and_votes = db.query(
|
songs_and_votes = db.query(
|
||||||
models.Song, votes.c.vote
|
models.Song, votes.c.vote
|
||||||
@@ -17,17 +19,19 @@ def get_songs_and_vote_for_session(db, session_name) -> list[models.Song]:
|
|||||||
|
|
||||||
|
|
||||||
def get_all_songs_and_votes(db) -> dict[int, dict[int, int]]:
|
def get_all_songs_and_votes(db) -> dict[int, dict[int, int]]:
|
||||||
_v = db.query(models.Vote.song_id, models.Vote.vote, func.count(models.Vote.song_id)).group_by(models.Vote.song_id, models.Vote.vote).all()
|
_v = db.query(models.Vote.song_id, models.Vote.vote, func.count(
|
||||||
|
models.Vote.song_id)).group_by(models.Vote.song_id, models.Vote.vote).all()
|
||||||
|
|
||||||
votes = {}
|
votes = {}
|
||||||
|
|
||||||
for v in _v:
|
for v in _v:
|
||||||
if v[0] not in votes:
|
if v[0] not in votes:
|
||||||
votes[v[0]] = {-1 : 0, 0 : 0, 1 : 0}
|
votes[v[0]] = {-1: 0, 0: 0, 1: 0}
|
||||||
votes[v[0]][v[1]] = v[2]
|
votes[v[0]][v[1]] = v[2]
|
||||||
|
|
||||||
return votes
|
return votes
|
||||||
|
|
||||||
|
|
||||||
def create_song(db,
|
def create_song(db,
|
||||||
og_artist,
|
og_artist,
|
||||||
aca_artist,
|
aca_artist,
|
||||||
@@ -37,6 +41,7 @@ def create_song(db,
|
|||||||
yt_id,
|
yt_id,
|
||||||
spfy_id,
|
spfy_id,
|
||||||
thumbnail,
|
thumbnail,
|
||||||
|
is_current,
|
||||||
is_aca,
|
is_aca,
|
||||||
arng_url,
|
arng_url,
|
||||||
categories,
|
categories,
|
||||||
@@ -51,6 +56,7 @@ def create_song(db,
|
|||||||
yt_id=yt_id,
|
yt_id=yt_id,
|
||||||
spfy_id=spfy_id,
|
spfy_id=spfy_id,
|
||||||
thumbnail=thumbnail,
|
thumbnail=thumbnail,
|
||||||
|
is_current=is_current,
|
||||||
is_aca=is_aca,
|
is_aca=is_aca,
|
||||||
arng_url=arng_url,
|
arng_url=arng_url,
|
||||||
categories=categories,
|
categories=categories,
|
||||||
@@ -69,7 +75,8 @@ def create_or_update_vote(db, song_id, session_name, vote):
|
|||||||
if vote_entry:
|
if vote_entry:
|
||||||
vote_entry.vote = str(vote) # type: ignore
|
vote_entry.vote = str(vote) # type: ignore
|
||||||
else:
|
else:
|
||||||
vote_entry = models.Vote(song_id=song_id, session_id=session_entry.id, vote=vote)
|
vote_entry = models.Vote(
|
||||||
|
song_id=song_id, session_id=session_entry.id, vote=vote)
|
||||||
db.add(vote_entry)
|
db.add(vote_entry)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@@ -87,6 +94,7 @@ def activate_session(db, session_name):
|
|||||||
|
|
||||||
return session_entry
|
return session_entry
|
||||||
|
|
||||||
|
|
||||||
def deactivate_session(db, session_name):
|
def deactivate_session(db, session_name):
|
||||||
session_entry = db.query(models.Session).filter(
|
session_entry = db.query(models.Session).filter(
|
||||||
(models.Session.session_name == session_name)).first()
|
(models.Session.session_name == session_name)).first()
|
||||||
@@ -95,4 +103,4 @@ def deactivate_session(db, session_name):
|
|||||||
else:
|
else:
|
||||||
session_entry = models.Session(session_name=session_name, active=False)
|
session_entry = models.Session(session_name=session_name, active=False)
|
||||||
db.add(session_entry)
|
db.add(session_entry)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
@@ -39,8 +39,13 @@ async def vote(request: Request, session_id: str, db: Annotated[Session, Depends
|
|||||||
all_categories = set()
|
all_categories = set()
|
||||||
|
|
||||||
wildcard_songs = []
|
wildcard_songs = []
|
||||||
|
current_songs = []
|
||||||
|
|
||||||
for song in songs:
|
for song in songs:
|
||||||
|
if song.is_current:
|
||||||
|
current_songs.append(song)
|
||||||
|
continue
|
||||||
|
|
||||||
if not song.is_aca:
|
if not song.is_aca:
|
||||||
wildcard_songs.append(song)
|
wildcard_songs.append(song)
|
||||||
continue
|
continue
|
||||||
@@ -51,11 +56,13 @@ async def vote(request: Request, session_id: str, db: Annotated[Session, Depends
|
|||||||
all_categories.update(song.categories.keys())
|
all_categories.update(song.categories.keys())
|
||||||
|
|
||||||
songs_by_category["Wildcard (nicht a cappella)"] = wildcard_songs
|
songs_by_category["Wildcard (nicht a cappella)"] = wildcard_songs
|
||||||
|
songs_by_category["Aktuelles Programm"] = current_songs
|
||||||
|
|
||||||
all_categories = list(all_categories)
|
all_categories = list(all_categories)
|
||||||
all_categories.sort()
|
all_categories.sort()
|
||||||
|
|
||||||
all_categories.append("Wildcard (nicht a cappella)")
|
all_categories.append("Wildcard (nicht a cappella)")
|
||||||
|
all_categories.append("Aktuelles Programm")
|
||||||
|
|
||||||
print(all_categories)
|
print(all_categories)
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class Song(Base):
|
|||||||
yt_id: Mapped[Optional[str]]
|
yt_id: Mapped[Optional[str]]
|
||||||
spfy_id: Mapped[Optional[str]]
|
spfy_id: Mapped[Optional[str]]
|
||||||
thumbnail: Mapped[Optional[str]]
|
thumbnail: Mapped[Optional[str]]
|
||||||
|
is_current: Mapped[Optional[bool]]
|
||||||
is_aca: Mapped[Optional[bool]]
|
is_aca: Mapped[Optional[bool]]
|
||||||
arng_url: Mapped[Optional[str]]
|
arng_url: Mapped[Optional[str]]
|
||||||
categories: Mapped[Optional[dict[str, bool]]]
|
categories: Mapped[Optional[dict[str, bool]]]
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ async def create_upload_file(db: Session = Depends(get_db)):
|
|||||||
song_list = song_list.replace({np.nan: None})
|
song_list = song_list.replace({np.nan: None})
|
||||||
song_list = song_list.replace({"n/a": None})
|
song_list = song_list.replace({"n/a": None})
|
||||||
|
|
||||||
category_names = list(song_list.iloc[0][7:16])
|
category_names = list(song_list.iloc[0][8:17])
|
||||||
|
|
||||||
for i, row in song_list[1:].iterrows():
|
for i, row in song_list[1:].iterrows():
|
||||||
row = np.array(row)
|
row = np.array(row)
|
||||||
@@ -84,9 +84,9 @@ async def create_upload_file(db: Session = Depends(get_db)):
|
|||||||
spfy_id = get_spotify_id(row[3])
|
spfy_id = get_spotify_id(row[3])
|
||||||
|
|
||||||
categories = {n: v for n, v in zip(
|
categories = {n: v for n, v in zip(
|
||||||
category_names, row[7:16] != None)}
|
category_names, row[8:17] != None)}
|
||||||
|
|
||||||
if not np.any(list(categories.values())):
|
if (not np.any(list(categories.values()))) and (row[5] != "ja"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
create_song(db,
|
create_song(db,
|
||||||
@@ -98,9 +98,10 @@ async def create_upload_file(db: Session = Depends(get_db)):
|
|||||||
yt_id=yt_id,
|
yt_id=yt_id,
|
||||||
spfy_id=spfy_id,
|
spfy_id=spfy_id,
|
||||||
thumbnail=get_thumbnail(row[3]),
|
thumbnail=get_thumbnail(row[3]),
|
||||||
is_aca=row[5] == "ja",
|
is_current=row[5] == "ja",
|
||||||
arng_url=row[6],
|
is_aca=row[6] == "ja",
|
||||||
|
arng_url=row[7],
|
||||||
categories=categories,
|
categories=categories,
|
||||||
main_category=category_names[get_main_category(row[7:16])],
|
main_category=category_names[get_main_category(row[8:17])],
|
||||||
singable=row[16] != "nein"
|
singable=row[17] != "nein"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class Song(BaseModel):
|
|||||||
yt_id: Optional[str]
|
yt_id: Optional[str]
|
||||||
spfy_id: Optional[str]
|
spfy_id: Optional[str]
|
||||||
thumbnail: Optional[str]
|
thumbnail: Optional[str]
|
||||||
|
is_current: Optional[bool]
|
||||||
is_aca: Optional[bool]
|
is_aca: Optional[bool]
|
||||||
arng_url: Optional[str]
|
arng_url: Optional[str]
|
||||||
categories: Optional[dict[str, bool]]
|
categories: Optional[dict[str, bool]]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Liederwahl</title>
|
<title>Choriosity Liederwahl</title>
|
||||||
|
|
||||||
<link rel="apple-touch-icon" href="https://choriosity.de/assets/images/apple-touch-icon.png" type="image/png">
|
<link rel="apple-touch-icon" href="https://choriosity.de/assets/images/apple-touch-icon.png" type="image/png">
|
||||||
<link rel="alternate icon" href="https://choriosity.de/assets/images/favicon.png" type="image/png">
|
<link rel="alternate icon" href="https://choriosity.de/assets/images/favicon.png" type="image/png">
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
$(window).on("load", activate_session);
|
$(window).on("load", activate_session);
|
||||||
|
|
||||||
$(window).on("beforeunload", deactivate_session);
|
$(window).on("beforeunload", deactivate_session);
|
||||||
|
$(window).on("unload", deactivate_session);
|
||||||
$(window).on("pagehide", deactivate_session);
|
$(window).on("pagehide", deactivate_session);
|
||||||
$(document).on('visibilitychange', function () {
|
$(document).on('visibilitychange', function () {
|
||||||
if (document.visibilityState == 'hidden') {
|
if (document.visibilityState == 'hidden') {
|
||||||
|
|||||||
Reference in New Issue
Block a user