Add SQLite query for tags with simple view

This commit is contained in:
Martin Müller 2017-07-18 21:56:46 +02:00
parent 600a1875f6
commit 5535add079
3 changed files with 35 additions and 3 deletions

View File

@ -1 +1,2 @@
path = "/home/pi/sounds"
db = "metadata.sqlite"

View File

@ -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/<sound>", methods=["GET", "POST"])
def edit(sound):

View File

@ -2,6 +2,11 @@
{% block content %}
<section id="sounds">
<div><input type="text" id="search" /><span class="reset"></span></div>
<ul>
{% for tag in tags %}
<li>{{ tag.name }}</li>
{% endfor %}
</ul>
{% for sound in sounds %}
<div class="sound{{ ' edit' if edit }}"><a href="{{ '/' + ('edit' if edit else 'play') + '/' + sound | urlencode }}">{{ sound.split('.', 1)[0] }}</a></div>
{% endfor %}