From 5535add0797385b73f5fd71a8375890b37a90719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20M=C3=BCller?= Date: Tue, 18 Jul 2017 21:56:46 +0200 Subject: [PATCH] Add SQLite query for tags with simple view --- config.py.example | 1 + soundboard.py | 32 +++++++++++++++++++++++++++++--- templates/index.html | 5 +++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/config.py.example b/config.py.example index 841b7df..63b42bd 100644 --- a/config.py.example +++ b/config.py.example @@ -1 +1,2 @@ path = "/home/pi/sounds" +db = "metadata.sqlite" diff --git a/soundboard.py b/soundboard.py index efa7f7f..69a470a 100644 --- a/soundboard.py +++ b/soundboard.py @@ -1,7 +1,8 @@ import os import subprocess +import sqlite3 -from flask import Flask, render_template, request, redirect, url_for +from flask import Flask, render_template, request, redirect, url_for, g import config @@ -9,7 +10,30 @@ app = Flask(__name__) app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True -processlist = [] +# Credits: http://flask.pocoo.org/docs/0.12/patterns/sqlite3/ +def getDB(): + db = getattr(g, "_database", None) + + if db is None: + db = g._database = sqlite3.connect(config.db) + db.row_factory = sqlite3.Row + + return db + +@app.teardown_appcontext +def closeDBConnection(exception): + db = getattr(g, "_database", None) + + if db is not None: + db.close() + +def queryDB(query, args=(), one=False): + cur = getDB().execute(query, args) + result = cur.fetchall() + + cur.close() + + return (result[0] if result else None) if one else result @app.route("/") @app.route("/edit") @@ -20,6 +44,8 @@ def index(sound=None, text=None, video=None): sounds = [os.fsencode(file).decode() for file in os.listdir(config.path)] sounds = sorted(sounds) + tags = queryDB("SELECT name FROM tag") + if sound is not None and sound in sounds: subprocess.Popen(["omxplayer", os.path.join(config.path, sound).encode("utf-8")], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -54,7 +80,7 @@ def index(sound=None, text=None, video=None): else: edit = False - return render_template("index.html", sounds=sounds, edit=edit) + return render_template("index.html", sounds=sounds, tags=tags, edit=edit) @app.route("/edit/", methods=["GET", "POST"]) def edit(sound): diff --git a/templates/index.html b/templates/index.html index 84fb9ed..affb5d2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,11 @@ {% block content %}
тип
+
    + {% for tag in tags %} +
  • {{ tag.name }}
  • + {% endfor %} +
{% for sound in sounds %} {% endfor %}