Files
liederwahl/app/crud.py
Matthias Weber 566183dc4a
All checks were successful
release-tag / release-image (push) Successful in 6m27s
make somewhat anonymous, improve evaluation
2024-07-10 21:31:55 +02:00

150 lines
4.5 KiB
Python

import app.models as models
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.sql import text
def get_songs_and_vote_for_session(db, session_name) -> list[models.Song]:
session_entry = activate_session(db, session_name)
votes = db.query(models.Vote).filter(
models.Vote.session_id == session_entry.id).subquery()
songs_and_votes = db.query(
models.Song, votes.c.vote, votes.c.comment
).join(votes, isouter=True).filter().all()
return songs_and_votes
def get_all_songs_and_votes(db) -> list:
res = db.execute(text("""SELECT
songs.og_artist,
songs.aca_artist,
songs.title,
songs.url,
COUNT(vote) FILTER (where vote = -1) as "nein" ,
COUNT(vote) FILTER (where vote = 0) as "neutral",
COUNT(vote) FILTER (where vote = 1) as "ja"
FROM votes INNER JOIN songs ON votes.song_id = songs.id
GROUP BY song_id ORDER BY song_id
""")).fetchall()
return [dict(r._mapping) for r in res]
def create_song(db,
og_artist,
aca_artist,
title,
url,
source,
yt_id,
spfy_id,
thumbnail,
is_current,
is_aca,
arng_url,
categories,
main_category,
singable,
comment
):
s = models.Song(og_artist=og_artist,
aca_artist=aca_artist,
title=title,
url=url,
source=source,
yt_id=yt_id,
spfy_id=spfy_id,
thumbnail=thumbnail,
is_current=is_current,
is_aca=is_aca,
arng_url=arng_url,
categories=categories,
main_category=main_category,
singable=singable,
comment=comment)
db.add(s)
db.commit()
def create_or_update_vote(db, song_id, session_name, vote):
session_entry = activate_session(db, session_name)
vote_entry = db.query(models.Vote).filter(
(models.Vote.session_id == session_entry.id) & (models.Vote.song_id == song_id)).first()
if vote_entry:
vote_entry.vote = str(vote) # type: ignore
else:
vote_entry = models.Vote(
song_id=song_id, session_id=session_entry.id, vote=vote)
db.add(vote_entry)
db.commit()
def create_or_update_comment(db, song_id, session_name, comment):
session_entry = activate_session(db, session_name)
if comment == "":
comment = None
vote_entry = db.query(models.Vote).filter(
(models.Vote.session_id == session_entry.id) & (models.Vote.song_id == song_id)).first()
if vote_entry:
vote_entry.comment = comment # type: ignore
else:
vote_entry = models.Vote(
song_id=song_id, session_id=session_entry.id, comment=comment)
db.add(vote_entry)
db.commit()
def activate_session(db, session_name):
session_entry = db.query(models.Session).filter(
models.Session.session_name == session_name).first() # , models.Session.ip == ip, models.Session.user_agent == user_agent
if session_entry:
session_entry.active = True
else:
session_entry = models.Session(
session_name=session_name, active=True) # , ip=ip, user_agent=user_agent
db.add(session_entry)
flag_modified(session_entry, "active")
db.commit()
return session_entry
def deactivate_session(db, session_name):
session_entry = db.query(models.Session).filter(
models.Session.session_name == session_name).first() # , models.Session.ip == ip, models.Session.user_agent == user_agent
if session_entry:
session_entry.active = False
flag_modified(session_entry, "active")
db.commit()
else:
pass
# session_entry = models.Session(session_name=session_name, ip=ip, active=False)
# db.add(session_entry)
def get_setting(db, key):
entry = db.query(models.Config.value).filter(
models.Config.key == key).first()
if entry:
return entry[0]
else:
return None
def set_setting(db, key, value):
setting_entry = db.query(models.Config).filter(
models.Config.key == key).first()
if setting_entry:
setting_entry.value = value
else:
setting_entry = models.Config(key=key, value=value)
db.add(setting_entry)
db.commit()