make somewhat anonymous, improve evaluation
All checks were successful
release-tag / release-image (push) Successful in 6m27s

This commit is contained in:
Matthias Weber
2024-07-10 21:31:55 +02:00
parent e4b2d04c7b
commit 566183dc4a
5 changed files with 33 additions and 39 deletions

View File

@@ -1,9 +1,7 @@
import app.models as models
from sqlalchemy import func, and_
from sqlalchemy.orm.attributes import flag_modified
from starlette_context import context
from starlette_context.header_keys import HeaderKeys
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)
@@ -17,19 +15,20 @@ def get_songs_and_vote_for_session(db, session_name) -> list[models.Song]:
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()
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()
votes = {}
for v in _v:
if v[0] not in votes:
votes[v[0]] = {-1: 0, 0: 0, 1: 0}
votes[v[0]][v[1]] = v[2]
return votes
return [dict(r._mapping) for r in res]
def create_song(db,
@@ -101,33 +100,25 @@ def create_or_update_comment(db, song_id, session_name, comment):
def activate_session(db, session_name):
ip = context.data[HeaderKeys.forwarded_for]
user_agent = context.data[HeaderKeys.user_agent]
session_entry = db.query(models.Session).filter(and_(
models.Session.session_name == session_name)).first() # , models.Session.ip == ip, models.Session.user_agent == user_agent
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:
if ip not in session_entry.ips:
session_entry.ips.append(ip)
session_entry.active = True
else:
session_entry = models.Session(
session_name=session_name, active=True, ips=[ip]) # , ip=ip, user_agent=user_agent
session_name=session_name, active=True) # , ip=ip, user_agent=user_agent
db.add(session_entry)
flag_modified(session_entry, "active")
flag_modified(session_entry, "ips")
db.commit()
return session_entry
def deactivate_session(db, session_name):
ip = context.data[HeaderKeys.forwarded_for]
user_agent = context.data[HeaderKeys.user_agent]
session_entry = db.query(models.Session).filter(and_(
models.Session.session_name == session_name)).first() # , models.Session.ip == ip, models.Session.user_agent == user_agent
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")

View File

@@ -16,6 +16,8 @@ from starlette.middleware import Middleware
from starlette_context import plugins
from starlette_context.middleware import RawContextMiddleware
from hashlib import sha384
Base.metadata.create_all(engine)
if os.path.isfile("/tmp/first_run") and (os.environ.get("RELOAD_ON_FIRST_RUN", "").lower() == "true"):
@@ -58,7 +60,7 @@ async def vote(request: Request, session_id: str | None = None , unordered: bool
db: Session = Depends(get_db)) -> HTMLResponse:
if not session_id:
session_id = user["sub"]
session_id = sha384(str.encode(user["sub"])).hexdigest() #CryptContext(schemes=["bcrypt"], deprecated="auto").hash(user["sub"])
#print(user)

View File

@@ -32,7 +32,6 @@ class Session(Base):
id: Mapped[int] = mapped_column(primary_key=True)
session_name: Mapped[str]
active: Mapped[bool]
ips: Mapped[list[str]]
first_seen: Mapped[datetime] = mapped_column(server_default=func.now())
last_seen: Mapped[Optional[datetime]
] = mapped_column(onupdate=func.now())

View File

@@ -8,7 +8,7 @@ from sqlalchemy.orm import Session
from app.database import get_db, engine, Base
from app.security import get_current_user
from app.crud import create_song, get_setting, set_setting
from app.crud import create_song, get_setting, set_setting, get_all_songs_and_votes
router = APIRouter(
prefix="/admin",
@@ -79,11 +79,12 @@ async def create_upload_file(include_non_singable: bool = False, db: Session = D
category_names = list(song_list.iloc[0][8:17])
for i, row in song_list[1:].iterrows():
if (row[17] == "nein") and not include_non_singable:
continue
row = np.array(row)
if (row[17] == "nein") and not include_non_singable:
continue
if not row[2]: # no title
continue
@@ -126,3 +127,8 @@ async def toggle_veto_mode(db: Session = Depends(get_db)) -> bool:
else:
set_setting(db, "veto_mode", True)
return True
@router.get("/results")
async def get_evaluation(db = Depends(get_db)) -> list:
return get_all_songs_and_votes(db)

View File

@@ -1,11 +1,10 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Request
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
import app.models as models
from app.database import get_db
from app.schemas import Song
from app.crud import get_songs_and_vote_for_session, create_or_update_vote, get_all_songs_and_votes, create_or_update_comment
from app.crud import get_songs_and_vote_for_session, create_or_update_vote, create_or_update_comment
router = APIRouter(
prefix="/songs",
@@ -28,6 +27,3 @@ async def comment(song_id: str, session_id: str, comment: str, db: Annotated[Ses
create_or_update_comment(db, song_id, session_id, comment)
#create_or_update_vote(db, song_id, session_id, vote)
@router.get("/evaluation")
async def get_evaluation(db: Annotated[Session, Depends(get_db)] = None) -> dict[int, dict[int, int]]:
return get_all_songs_and_votes(db)