From 5f551ac0923d09b832a0ded54fec56505a4a4ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Sat, 29 Jun 2019 01:24:54 +0200 Subject: [PATCH] Add list of played songs --- .gitignore | 3 +- app/database.py | 54 +++++++++++++++++++++++++++--- app/main.py | 24 +++++++++++++ app/static/css/style.css | 13 ++++++++ app/templates/base.html | 8 +++++ app/templates/main_admin.html | 30 +++++++++++++---- app/templates/played_list.html | 61 ++++++++++++++++++++++++++++++++++ 7 files changed, 181 insertions(+), 12 deletions(-) create mode 100644 app/templates/played_list.html diff --git a/.gitignore b/.gitignore index efb242c..ca79605 100644 --- a/.gitignore +++ b/.gitignore @@ -130,5 +130,4 @@ dmypy.json !.vscode/extensions.json # Test data -test.db -config.json +data/ diff --git a/app/database.py b/app/database.py index b719cda..d7fd7b0 100644 --- a/app/database.py +++ b/app/database.py @@ -7,6 +7,7 @@ from io import StringIO song_table = "songs" entry_table = "entries" index_label = "Id" +done_table = "done_songs" def open_db(): conn = sqlite3.connect("data/test.db") @@ -28,14 +29,19 @@ def import_songs(song_csv): def create_entry_table(): conn = open_db() - t = (entry_table,) conn.execute('CREATE TABLE IF NOT EXISTS '+entry_table + ' (ID INTEGER PRIMARY KEY NOT NULL, Song_Id INTEGER NOT NULL, Name VARCHAR(255))') conn.close() + +def create_done_song_table(): + conn = open_db() + conn.execute('CREATE TABLE IF NOT EXISTS '+done_table + + ' (Song_Id INTEGER PRIMARY KEY NOT NULL, Plays INTEGER)') + conn.close() + def create_song_table(): conn = open_db() - t = (entry_table,) conn.execute("CREATE TABLE IF NOT EXISTS \""+song_table+"""\" ( "Id" INTEGER, "Title" TEXT, @@ -52,21 +58,37 @@ def create_song_table(): def create_list_view(): conn = open_db() conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS - SELECT Name, Title, Artist, entries.Id + SELECT Name, Title, Artist, entries.Id, songs.Id FROM entries, songs WHERE entries.Song_Id=songs.Id""") conn.close() + +def create_done_song_view(): + conn = open_db() + conn.execute("""CREATE VIEW IF NOT EXISTS [Abspielliste] AS + SELECT Artist || \" - \" || Title AS Song, Plays AS Wiedergaben + FROM songs, done_songs + WHERE done_songs.Song_Id=songs.Id""") + conn.close() + def get_list(): conn = open_db() cur = conn.cursor() cur.execute("SELECT * FROM Liste") return cur.fetchall() + +def get_played_list(): + conn = open_db() + cur = conn.cursor() + cur.execute("SELECT * FROM Abspielliste") + return cur.fetchall() + def get_song_list(): conn =open_db() cur = conn.cursor() - cur.execute("SELECT Title || \" - \" || Artist AS Song, Id FROM songs") + cur.execute("SELECT Artist || \" - \" || Title AS Song, Id FROM songs") return cur.fetchall() def get_song_completions(input_string): @@ -86,6 +108,30 @@ def add_entry(name,song_id): conn.close() return +def add_sung_song(entry_id): + conn = open_db() + cur = conn.cursor() + cur.execute("""SELECT Song_Id FROM entries WHERE Id=?""",(entry_id,)) + song_id = cur.fetchone()[0] + cur.execute("""INSERT OR REPLACE INTO done_songs (Song_Id, Plays) + VALUES("""+str(song_id)+""", + COALESCE( + (SELECT Plays FROM done_songs + WHERE Song_Id="""+str(song_id)+"), 0) + 1)" + ) + conn.commit() + delete_entry(entry_id) + conn.close() + return True + +def clear_played_songs(): + conn = open_db() + cur = conn.cursor() + cur.execute("DELETE FROM done_songs") + conn.commit() + conn.close() + return True + def delete_entry(id): conn = open_db() cur = conn.cursor() diff --git a/app/main.py b/app/main.py index 744a653..259ed3e 100644 --- a/app/main.py +++ b/app/main.py @@ -29,6 +29,11 @@ def enqueue(): def songlist(): return render_template('songlist.html', list=database.get_song_list(), auth=basic_auth.authenticate()) +@app.route("/plays") +@basic_auth.required +def played_list(): + return render_template('played_list.html', list=database.get_played_list(), auth=basic_auth.authenticate()) + @app.route("/api/songs") def songs(): list = database.get_song_list() @@ -63,6 +68,23 @@ def delete_entry(entry_id): else: return Response('{"status": "FAIL"}', mimetype='text/json') + +@app.route("/api/entries/mark_sung/") +@basic_auth.required +def mark_sung(entry_id): + if database.add_sung_song(entry_id): + return Response('{"status": "OK"}', mimetype='text/json') + else: + return Response('{"status": "FAIL"}', mimetype='text/json') + +@app.route("/api/played/clear") +@basic_auth.required +def clear_played_songs(): + if database.clear_played_songs(): + return Response('{"status": "OK"}', mimetype='text/json') + else: + return Response('{"status": "FAIL"}', mimetype='text/json') + @app.route("/api/entries/delete_all") @basic_auth.required def delete_all_entries(): @@ -81,7 +103,9 @@ def activate_job(): helpers.create_data_directory() database.create_entry_table() database.create_song_table() + database.create_done_song_table() database.create_list_view() + database.create_done_song_view() helpers.setup_config(app) diff --git a/app/static/css/style.css b/app/static/css/style.css index 4a64e07..80c8a73 100644 --- a/app/static/css/style.css +++ b/app/static/css/style.css @@ -38,3 +38,16 @@ main { } } +@media print { + body { + font-size: 1.3em; + } + + .footer { + display: none !important; + } + + .admincontrols { + display: none; + } +} \ No newline at end of file diff --git a/app/templates/base.html b/app/templates/base.html index 9b2ab3f..acf5a98 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -39,6 +39,11 @@ + {% if auth %} + + {% endif %}