From 70c460925269f9dbf0a4e35d760c168faf1e3ed6 Mon Sep 17 00:00:00 2001 From: "matthias@matsewe.de" Date: Fri, 24 May 2024 15:51:07 +0200 Subject: [PATCH] Clean up --- app/.gitignore | 1 - app/main.py | 51 +++++---- static/colors.css | 90 ---------------- static/landing_script.js | 9 -- static/voting_script.js | 222 -------------------------------------- templates/landing.html | 37 ++++--- templates/voting-old.html | 54 ---------- 7 files changed, 49 insertions(+), 415 deletions(-) delete mode 100644 app/.gitignore delete mode 100644 static/colors.css delete mode 100644 static/landing_script.js delete mode 100644 static/voting_script.js delete mode 100644 templates/voting-old.html diff --git a/app/.gitignore b/app/.gitignore deleted file mode 100644 index c6b6498..0000000 --- a/app/.gitignore +++ /dev/null @@ -1 +0,0 @@ -secrets.py \ No newline at end of file diff --git a/app/main.py b/app/main.py index a9001a5..2df9570 100644 --- a/app/main.py +++ b/app/main.py @@ -22,32 +22,29 @@ app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") -@app.get("/", response_class=HTMLResponse) -async def root(request: Request, session_id : str = "", db: Annotated[Session, Depends(get_db)] = None): - if session_id == "": - return templates.TemplateResponse( - request=request, name="landing.html" - ) - else: - songs = [Song(**s.__dict__, vote=v) for s, v in get_songs_and_vote_for_session(db, session_id)] +@app.get("/") +async def root(request: Request) -> HTMLResponse: + return templates.TemplateResponse( + request=request, name="landing.html" + ) - songs_by_category = {} - all_categories = set() - for song in songs: - if song.main_category not in songs_by_category: - songs_by_category[song.main_category] = [] - songs_by_category[song.main_category].append(song) - all_categories.update(song.categories.keys()) - return templates.TemplateResponse( - request=request, name="voting.html", context={ - "songs_by_category": songs_by_category, - "all_categories": {c: i+1 for i, c in enumerate(all_categories)}, - "session_id": session_id - } - ) -#@app.get("/vote", response_class=HTMLResponse) -#async def vote(request: Request, session_id : str = ""): -# return templates.TemplateResponse( -# request=request, name="voting-old.html" -# ) \ No newline at end of file + +@app.get("/vote") +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_by_category = {} + all_categories = set() + for song in songs: + if song.main_category not in songs_by_category: + songs_by_category[song.main_category] = [] + songs_by_category[song.main_category].append(song) + all_categories.update(song.categories.keys()) + return templates.TemplateResponse( + request=request, name="voting.html", context={ + "songs_by_category": songs_by_category, + "all_categories": {c: i+1 for i, c in enumerate(all_categories)}, + "session_id": session_id + } + ) \ No newline at end of file diff --git a/static/colors.css b/static/colors.css deleted file mode 100644 index 4c7065e..0000000 --- a/static/colors.css +++ /dev/null @@ -1,90 +0,0 @@ -.cat-1 { - --main-color: #1f77b4; -} -.cat-2 { - --main-color: #ff7f0e; -} -.cat-3 { - --main-color: #2ca02c; -} -.cat-4 { - --main-color: #d62728; -} -.cat-5 { - --main-color: #9467bd; -} -.cat-6 { - --main-color: #8c564b; -} -.cat-7 { - --main-color: #e377c2; -} -.cat-8 { - --main-color: #7f7f7f; -} -.cat-9 { - --main-color: #bcbd22; -} -.cat-10 { - --main-color: #17becf; -} -.cat-11 { - --main-color: #a1c9f4; -} -.cat-12 { - --main-color: #ffb482; -} -.cat-13 { - --main-color: #8de5a1; -} -.cat-14 { - --main-color: #ff9f9b; -} -.cat-15 { - --main-color: #d0bbff; -} -.cat-16 { - --main-color: #debb9b; -} -.cat-17 { - --main-color: #fab0e4; -} -.cat-18 { - --main-color: #cfcfcf; -} -.cat-19 { - --main-color: #fffea3; -} -.cat-20 { - --main-color: #b9f2f0; -} -.cat-21 { - --main-color: #001c7f; -} -.cat-22 { - --main-color: #b1400d; -} -.cat-23 { - --main-color: #12711c; -} -.cat-24 { - --main-color: #8c0800; -} -.cat-25 { - --main-color: #591e71; -} -.cat-26 { - --main-color: #592f0d; -} -.cat-27 { - --main-color: #a23582; -} -.cat-28 { - --main-color: #3c3c3c; -} -.cat-29 { - --main-color: #b8850a; -} -.cat-30 { - --main-color: #006374; -} diff --git a/static/landing_script.js b/static/landing_script.js deleted file mode 100644 index c9e0de0..0000000 --- a/static/landing_script.js +++ /dev/null @@ -1,9 +0,0 @@ -$(document).ready(function () { - var s_id = localStorage.getItem('session_id') - if (s_id === null) { - s_id = window.crypto.randomUUID(); - localStorage.setItem('session_id', s_id) - } - $('.vote-from-existing').attr('href', '?session_id=' + s_id); - window.location.href = "/?session_id=" + s_id; -}); diff --git a/static/voting_script.js b/static/voting_script.js deleted file mode 100644 index 15dd5c1..0000000 --- a/static/voting_script.js +++ /dev/null @@ -1,222 +0,0 @@ -function makeScroll() { - const eles = document.getElementsByClassName('categories'); - Array.prototype.forEach.call(eles, ele => { - - - //ele.style.cursor = 'grab'; - - let pos = { top: 0, left: 0, x: 0, y: 0 }; - - const mouseDownHandler = function (e) { - ele.style.cursor = 'grabbing'; - ele.style.userSelect = 'none'; - - pos = { - left: ele.scrollLeft, - top: ele.scrollTop, - // Get the current mouse position - x: e.clientX, - y: e.clientY, - }; - - document.addEventListener('mousemove', mouseMoveHandler); - document.addEventListener('mouseup', mouseUpHandler); - }; - - const mouseMoveHandler = function (e) { - // How far the mouse has been moved - const dx = e.clientX - pos.x; - const dy = e.clientY - pos.y; - - // Scroll the element - ele.scrollTop = pos.top - dy; - ele.scrollLeft = pos.left - dx; - }; - - const mouseUpHandler = function () { - ele.style.cursor = 'grab'; - ele.style.removeProperty('user-select'); - - document.removeEventListener('mousemove', mouseMoveHandler); - document.removeEventListener('mouseup', mouseUpHandler); - }; - - // Attach the handler - ele.addEventListener('mousedown', mouseDownHandler); - }); -} - -function vote(song_id, vote) { - var session_id = getQueryParameter("session_id"); - - no_button = $("#song-" + song_id).find(".button-no") - yes_button = $("#song-" + song_id).find(".button-yes") - neutral_button = $("#song-" + song_id).find(".button-neutral") - - no_button.removeClass("selected") - yes_button.removeClass("selected") - neutral_button.removeClass("selected") - - switch (vote) { - case 0: - neutral_button.addClass("selected") - break; - case 1: - yes_button.addClass("selected") - break; - case -1: - no_button.addClass("selected") - default: - break; - } - - $.ajax({ - url: "/songs/" + song_id + "/vote?" + $.param({ user_id: session_id, vote: vote }), - method: "POST" - }) -} - -const getQueryParameter = (param) => new URLSearchParams(document.location.search.substring(1)).get(param); - -var all_songs = {} - -$(document).ready(function () { - var session_id = getQueryParameter("session_id"); - - var songTemplate = $('script[data-template="song"]').text().split(/\$\{(.+?)\}/g); - - function render(props) { - return function (tok, i) { return (i % 2) ? props[tok] : tok; }; - } - - - - $.ajax({ - url: "/songs/", - data: { user_id: session_id } - }).then(function (songs) { - song_list = {} - - cat_to_id = {} - - $.each(songs, function (key, song) { - - all_songs[song.id] = song; - - var mc = song.main_category; - - if (!song.is_aca) { - mc = "Wildcard" - } - if (!(mc in song_list)) { - song_list[mc] = "" - } - - var cats = "" - var cat_id = 1 - $.each(song.categories, function (cat_name, is_cat) { - if (is_cat) { - cats = cats + '' + cat_name + ''; - } - cat_to_id[cat_name] = cat_id - cat_id += 1 - }); - cat_to_id["Wildcard"] = cat_id - - - artist = ""; - if (song.og_artist) { - artist += song.og_artist; - if (song.aca_artist && (song.aca_artist !== song.og_artist)) { - artist += " / "; - artist += song.aca_artist; - } - } else { - artist = song.aca_artist; - } - - var s = songTemplate.map(render({ - "id": song.id, - "title": song.title, - "artist": artist, //song.og_artist + ": " + song.aca_artist - "cover_image": song.thumbnail, - "no_selected": (song.vote == -1) ? "selected" : "", - "neutral_selected": (song.vote == 0) ? "selected" : "", - "yes_selected": (song.vote == 1) ? "selected" : "", - "play_button": (song.yt_id || song.spfy_id) ? "play" : "open", - "categories": cats - })).join('') - - song_list[mc] += s - }); - $.each(cat_to_id, function (cat_name, cat_id) { - if (cat_name in song_list) { - $('#songs').append("

" + cat_name + "

"); - $('#songs').append(song_list[cat_name]); - } - }); - - makeScroll(); - }); -}); - -var is_playing = -1; - -var spotify_embed_controller; - -window.onSpotifyIframeApiReady = (IFrameAPI) => { - const element = document.getElementById('spotify-embed'); - const options = { - width: '640', - height: '360' - }; - const callback = (EmbedController) => { - spotify_embed_controller = EmbedController; - }; - IFrameAPI.createController(element, options, callback); - }; - -function stop() { - $("#song-" + is_playing + " .cover-container .overlay img").attr("src", "/static/play.svg"); - - $("#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; -} - -function play(id) { - if (is_playing == id) { - stop(); - } else { - stop(); - - is_playing = id; - - $("#song-" + id + " .cover-container .overlay img").attr("src", "/static/stop.svg"); - - song = all_songs[id]; - yt_id = song.yt_id - spotify_id = song.spfy_id - - if (yt_id) { - $("#yt-player").css("display", "flex"); - $("#close-player").css("display", "block"); - iframe_code = ''; - $("#yt-player").html(iframe_code); - } - else if (spotify_id) { - $("#spotify-player").css("display", "flex"); - $("#close-player").css("display", "block"); - spotify_embed_controller.loadUri("spotify:track:" + spotify_id); - spotify_embed_controller.play(); - } - else { - stop(); - $("#song-" + id + " .cover-container .overlay img").attr("src", "/static/open.svg"); - window.open(song.url, '_blank').focus(); - } - } -} \ No newline at end of file diff --git a/templates/landing.html b/templates/landing.html index 528b3e8..1a54399 100644 --- a/templates/landing.html +++ b/templates/landing.html @@ -1,15 +1,28 @@ - - Liederwahl - - - - -
-

Abstimmen

- -
- - + + Liederwahl + + + + + +
+

Abstimmen

+
+ + + \ No newline at end of file diff --git a/templates/voting-old.html b/templates/voting-old.html deleted file mode 100644 index 9f9f98b..0000000 --- a/templates/voting-old.html +++ /dev/null @@ -1,54 +0,0 @@ - - - - Liederwahl - - - - - - - - - - -

Hallo :)

-
Du kannst die Liederwahl jederzeit unterbrechen und zu einem späteren Zeitpunkt weitermachen.
-
-
-
-
- - - - -