4 Commits
1.0.0 ... 1.2.0

Author SHA1 Message Date
Matthias Weber
566183dc4a make somewhat anonymous, improve evaluation
All checks were successful
release-tag / release-image (push) Successful in 6m27s
2024-07-10 21:31:55 +02:00
e4b2d04c7b tidy up action 2024-07-03 18:13:16 +02:00
8109f121cb run build action online on new tag (version) 2024-07-03 18:10:08 +02:00
b1ba54194f remove jquery dependency
Some checks failed
release-tag / release-image (push) Has been cancelled
2024-07-03 17:06:25 +02:00
7 changed files with 104 additions and 127 deletions

View File

@@ -1,7 +1,9 @@
name: release-tag
on:
push
push:
tags:
- '*'
jobs:
release-image:
@@ -21,7 +23,7 @@ jobs:
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v2
with: # replace it with your local IP
with:
config-inline: |
[registry."git.matsewe.de"]
http = true
@@ -30,7 +32,7 @@ jobs:
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: git.matsewe.de # replace it with your local IP
registry: git.matsewe.de
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
@@ -48,6 +50,6 @@ jobs:
platforms: |
linux/amd64
push: true
tags: | # replace it with your local IP and tags
tags: |
git.matsewe.de/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
git.matsewe.de/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}

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)

View File

@@ -27,37 +27,9 @@
{% endif %}
<script src="https://open.spotify.com/embed/iframe-api/v1" async></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
const session_id = "{{ session_id }}";
function activate_session() {
$.ajax({
url: "/session/" + session_id,
method: "PUT"
})
}
function deactivate_session() {
$.ajax({
url: "/session/" + session_id,
method: "DELETE"
})
}
$(window).on("load", activate_session);
$(window).on("beforeunload", deactivate_session);
$(window).on("unload", deactivate_session);
$(window).on("pagehide", deactivate_session);
$(document).on('visibilitychange', function () {
if (document.visibilityState == 'hidden') {
deactivate_session();
} else {
activate_session()
}
});
var spotify_embed_controller;
window.onSpotifyIframeApiReady = (IFrameAPI) => {
@@ -75,12 +47,15 @@
var is_playing = -1;
function stop() {
$("#song-" + is_playing + " .cover-container .overlay img").attr("src", "/static/play.svg");
if (is_playing !== -1) {
document.querySelector("#song-" + is_playing + " .cover-container .overlay img").setAttribute("src", "/static/play.svg");
}
document.querySelector("#yt-player").style.display = "none";
document.querySelector("#spotify-player").style.display = "none";
document.querySelector("#close-player").style.display = "none";
document.querySelector("#yt-player").innerHTML = "";
$("#yt-player").css("display", "none");
$("#spotify-player").css("display", "none");
$("#close-player").css("display", "none");
$("#yt-player").html("");
spotify_embed_controller.pause();
is_playing = -1;
}
@@ -93,12 +68,13 @@
is_playing = song_id;
$("#song-" + song_id + " .cover-container .overlay img").attr("src", "/static/stop.svg");
document.querySelector("#song-" + song_id + " .cover-container .overlay img").setAttribute("src", "/static/stop.svg");
document.querySelector("#yt-player").style.display = "flex";
document.querySelector("#close-player").style.display = "block";
$("#yt-player").css("display", "flex");
$("#close-player").css("display", "block");
iframe_code = '<iframe src="https://www.youtube.com/embed/' + yt_id + '?autoplay=1" title="" width="640" height="360" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen></iframe>';
$("#yt-player").html(iframe_code);
document.querySelector("#yt-player").innerHTML = iframe_code;
}
}
@@ -110,10 +86,10 @@
is_playing = song_id;
$("#song-" + song_id + " .cover-container .overlay img").attr("src", "/static/stop.svg");
document.querySelector("#song-" + song_id + " .cover-container .overlay img").setAttribute("src", "/static/stop.svg");
$("#spotify-player").css("display", "flex");
$("#close-player").css("display", "block");
document.querySelector("#spotify-player").style.display = "flex";
document.querySelector("#close-player").style.display = "block";
spotify_embed_controller.loadUri("spotify:track:" + spfy_id);
spotify_embed_controller.play();
}
@@ -124,38 +100,42 @@
window.open(url, '_blank').focus();
}
function vote(song_id, vote) {
async function vote(song_id, vote) {
$.ajax({
url: "/songs/" + song_id + "/vote?" + $.param({ session_id: session_id, vote: vote }),
method: "POST",
success: function (data, textStatus) {
response = await fetch(
"/songs/" + song_id + "/vote?session_id=" + encodeURIComponent(session_id) + "&vote=" + vote, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
no_button = $("#song-" + song_id).find(".button-no")
yes_button = $("#song-" + song_id).find(".button-yes")
neutral_button = $("#song-" + song_id).find(".button-neutral")
if (response.ok) {
no_button = document.querySelector("#song-" + song_id + " .button-no");
yes_button = document.querySelector("#song-" + song_id + " .button-yes");
neutral_button = document.querySelector("#song-" + song_id + " .button-neutral");
no_button.removeClass("selected")
yes_button.removeClass("selected")
neutral_button.removeClass("selected")
no_button.classList.remove("selected")
yes_button.classList.remove("selected")
neutral_button.classList.remove("selected")
switch (vote) {
case 0:
neutral_button.addClass("selected")
neutral_button.classList.add("selected")
{% if veto_mode %}
$("#song-" + song_id).removeClass("not_singable")
document.querySelector("#song-" + song_id).classList.remove("not_singable")
{% endif %}
break;
case 1:
yes_button.addClass("selected")
yes_button.classList.add("selected")
{% if veto_mode %}
$("#song-" + song_id).removeClass("not_singable")
document.querySelector("#song-" + song_id).classList.remove("not_singable")
{% endif %}
break;
case -1:
no_button.addClass("selected")
no_button.classList.add("selected")
{% if veto_mode %}
$("#song-" + song_id).addClass("not_singable")
document.querySelector("#song-" + song_id).classList.add("not_singable")
{% endif %}
break;
default:
@@ -163,20 +143,19 @@
}
}
});
}
{% if veto_mode %}
function updateComment(song_id, el) {
async function updateComment(song_id, el) {
comment = el.value
$.ajax({
url: "/songs/" + song_id + "/comment?" + $.param({ session_id: session_id, comment: comment }),
method: "POST"
})
response = await fetch(
"/songs/" + song_id + "/comment?session_id=" + encodeURIComponent(session_id) + "&comment=" + encodeURIComponent(comment), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
}
{% endif %}
</script>
@@ -189,8 +168,10 @@
abgeben.
</div>
{% else %}
<h1 id="title"><img id="title-logo" src="https://choriosity.de/assets/images/logo-choriosity_weiss.svg"> Liederwahl</h1>
<div class="text">Du kannst die Liederwahl jederzeit unterbrechen und zu einem späteren Zeitpunkt weitermachen. Mit einem Klick auf das Thumbnail kannst du das Lied abspielen.
<h1 id="title"><img id="title-logo" src="https://choriosity.de/assets/images/logo-choriosity_weiss.svg"> Liederwahl
</h1>
<div class="text">Du kannst die Liederwahl jederzeit unterbrechen und zu einem späteren Zeitpunkt weitermachen. Mit
einem Klick auf das Thumbnail kannst du das Lied abspielen.
</div>
{% endif %}
<div id="songs">