From 3adea7b46a1658c9b46be3d48c92bc7ce45e6d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Sun, 26 May 2019 00:28:26 +0200 Subject: [PATCH] Add admin "login" and functionality, fix Umlauts in search --- .vscode/launch.json | 18 ++++++ database.py | 33 ++++++++++- main.py | 41 ++++++++++---- static/css/style.css | 5 -- templates/base.html | 9 ++- templates/main_admin.html | 115 +++++++++++++++++++++++++++++--------- templates/songlist.html | 6 +- 7 files changed, 178 insertions(+), 49 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index bead6cb..30242bc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,8 +5,26 @@ "version": "0.2.0", "configurations": [ + {"name":"Python: Flask","type":"python","request":"launch","module":"flask","env":{"FLASK_APP":"main.py","FLASK_ENV":"development","FLASK_DEBUG":"1"},"args":["run","--no-debugger","--no-reload"],"jinja":true}, {"name":"Python: Flask (with reload)","type":"python","request":"launch","module":"flask","env":{"FLASK_APP":"main.py","FLASK_ENV":"development","FLASK_DEBUG":"1"},"args":["run","--no-debugger"],"jinja":true}, + { + "name": "Python: Flask (with reload, externally reachable)", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "main.py", + "FLASK_ENV": "development", + "FLASK_DEBUG": "1" + }, + "args": [ + "run", + "--no-debugger", + "--host='0.0.0.0'" + ], + "jinja": true + }, { "name": "Python: Current File (Integrated Terminal)", "type": "python", diff --git a/database.py b/database.py index 6c8a0af..58e1438 100644 --- a/database.py +++ b/database.py @@ -1,3 +1,5 @@ +# -*- coding: utf_8 -*- + import sqlite3 import pandas from io import StringIO @@ -8,6 +10,7 @@ index_label = "Id" def open_db(): conn = sqlite3.connect("test.db") + conn.execute('PRAGMA encoding = "UTF-8";') return conn def import_songs(song_csv): @@ -16,6 +19,7 @@ def import_songs(song_csv): df.to_sql(song_table, conn, if_exists='replace', index=False) conn.close() + print("Imported songs") return def create_entry_table(): @@ -25,6 +29,22 @@ def create_entry_table(): ' (ID INTEGER PRIMARY KEY NOT NULL, Song_Id INTEGER NOT NULL, Name VARCHAR(255))') 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, + "Artist" TEXT, + "Year" INTEGER, + "Duo" INTEGER, + "Explicit" INTEGER, + "Date Added" TEXT, + "Styles" TEXT, + "Languages" TEXT + )""") + conn.close() + def create_list_view(): conn = open_db() conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS @@ -48,7 +68,9 @@ def get_song_list(): def get_song_completions(input_string): conn = open_db() cur = conn.cursor() - cur.execute("SELECT Title || \" - \" || Artist AS Song, Id FROM songs WHERE Song LIKE '%"+input_string+"%'") + # Don't look, it burns... + cur.execute( + "SELECT Title || \" - \" || Artist AS Song, Id FROM songs WHERE Song LIKE REPLACE(REPLACE(REPLACE(REPLACE(UPPER('%"+input_string+"%'),'ö','Ö'),'ü','Ü'),'ä','Ä'),'ß','ẞ')") return cur.fetchall() def add_entry(name,song_id): @@ -67,3 +89,12 @@ def delete_entry(id): conn.commit() conn.close() return True + + +def delete_all_entries(): + conn = open_db() + cur = conn.cursor() + cur.execute("DELETE FROM entries") + conn.commit() + conn.close() + return True diff --git a/main.py b/main.py index 358be1b..fa092d6 100644 --- a/main.py +++ b/main.py @@ -37,11 +37,23 @@ def songs(): list = database.get_song_list() return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') +@app.route("/api/songs/update") +def update_songs(): + database.delete_all_entries() + database.import_songs(helpers.get_songs(helpers.get_catalog_url())) + return Response('{"status":"OK"}', mimetype='text/json') -@app.route("/api/songs/compl/") -def get_song_completions(input_string): - list = database.get_song_completions(input_string=input_string) - return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') + +@app.route("/api/songs/compl") +def get_song_completions(input_string=""): + input_string = request.args.get('search',input_string) + if input_string!="": + print(input_string) + list = database.get_song_completions(input_string=input_string) + return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') + + else: + return 400 @app.route("/api/entries/delete/") @@ -52,19 +64,24 @@ def delete_entry(entry_id): else: return Response('{"status": "FAIL"}', mimetype='text/json') +@app.route("/api/entries/delete_all") +@basic_auth.required +def delete_all_entries(): + if database.delete_all_entries(): + return Response('{"status": "OK"}', mimetype='text/json') + else: + return Response('{"status": "FAIL"}', mimetype='text/json') + @app.route("/login") @basic_auth.required def admin(): return redirect("/", code=303) -if __name__ == "__main__": - """try: - os.remove("test.db") - print("removed database") - except OSError: - print("failed to remove database") - pass""" +@app.before_first_request +def activate_job(): database.create_entry_table() + database.create_song_table() database.create_list_view() - database.import_songs(helpers.get_songs(helpers.get_catalog_url())) + +if __name__ == "__main__": app.run(debug=True, host='0.0.0.0') diff --git a/static/css/style.css b/static/css/style.css index dfbc5ee..145209b 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -6,11 +6,6 @@ html, body { height: 100%; } -footer a, footer span{ - display: inline-flex; - vertical-align: middle; -} - .site { height: auto; min-height: 100%; diff --git a/templates/base.html b/templates/base.html index 2ad38c0..0118a31 100644 --- a/templates/base.html +++ b/templates/base.html @@ -52,11 +52,14 @@