forked from server/soundboard
Add SQLite query for tags with simple view
This commit is contained in:
parent
600a1875f6
commit
5535add079
@ -1 +1,2 @@
|
|||||||
path = "/home/pi/sounds"
|
path = "/home/pi/sounds"
|
||||||
|
db = "metadata.sqlite"
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
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
|
import config
|
||||||
|
|
||||||
@ -9,7 +10,30 @@ app = Flask(__name__)
|
|||||||
app.jinja_env.trim_blocks = True
|
app.jinja_env.trim_blocks = True
|
||||||
app.jinja_env.lstrip_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("/")
|
||||||
@app.route("/edit")
|
@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 = [os.fsencode(file).decode() for file in os.listdir(config.path)]
|
||||||
sounds = sorted(sounds)
|
sounds = sorted(sounds)
|
||||||
|
|
||||||
|
tags = queryDB("SELECT name FROM tag")
|
||||||
|
|
||||||
if sound is not None and sound in sounds:
|
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)
|
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:
|
else:
|
||||||
edit = False
|
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"])
|
@app.route("/edit/<sound>", methods=["GET", "POST"])
|
||||||
def edit(sound):
|
def edit(sound):
|
||||||
|
|||||||
@ -2,6 +2,11 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<section id="sounds">
|
<section id="sounds">
|
||||||
<div><input type="text" id="search" /><span class="reset">⨯</span></div>
|
<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 %}
|
{% 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>
|
<div class="sound{{ ' edit' if edit }}"><a href="{{ '/' + ('edit' if edit else 'play') + '/' + sound | urlencode }}">{{ sound.split('.', 1)[0] }}</a></div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user