Store changes to tags in metadata DB

This commit is contained in:
Martin Müller 2017-11-04 16:57:05 +01:00
parent b4e795417b
commit 92f869ce6b
3 changed files with 28 additions and 8 deletions

View File

@ -13,5 +13,6 @@ CREATE TABLE IF NOT EXISTS button_tags (
fk_tag INTEGER, fk_tag INTEGER,
fk_button INTEGER, fk_button INTEGER,
FOREIGN KEY(fk_tag) REFERENCES tag(id), FOREIGN KEY(fk_tag) REFERENCES tag(id),
FOREIGN KEY(fk_button) REFERENCES button(id) FOREIGN KEY(fk_button) REFERENCES button(id),
PRIMARY KEY(fk_tag, fk_button)
); );

View File

@ -90,12 +90,9 @@ def index(sound=None, text=None, video=None):
@app.route("/edit/<sound>", methods=["GET", "POST"]) @app.route("/edit/<sound>", methods=["GET", "POST"])
def edit(sound): def edit(sound):
if request.method == "POST":
# TODO: Store changes
return redirect("/edit")
tags = queryDB("""\ tags = queryDB("""\
SELECT SELECT
tag.id,
tag.name, tag.name,
checked.id IS NOT NULL AS checked checked.id IS NOT NULL AS checked
FROM FROM
@ -120,4 +117,26 @@ LEFT OUTER JOIN (
) AS checked ) AS checked
ON tag.id = checked.id""", (sound,)) ON tag.id = checked.id""", (sound,))
if request.method == "POST":
if not request.form.get("cancel"):
buttonId = queryDB("SELECT id FROM button WHERE file = ?", (sound,), True)
if buttonId is None:
queryDB("INSERT INTO button (file) VALUES (?)", (sound,))
getDB().commit()
buttonId = queryDB("SELECT id FROM button WHERE file = ?", (sound,), True)
for tag in tags:
checkbox = 1 if request.form.get("cb-{}".format(tag["name"])) else 0
if tag["checked"] < checkbox:
queryDB("INSERT OR REPLACE INTO button_tags (fk_button, fk_tag) VALUES (?, ?)", (buttonId[0], tag["id"]))
getDB().commit()
elif tag["checked"] > checkbox:
queryDB("DELETE FROM button_tags WHERE fk_button = ? AND fk_tag = ?", (buttonId[0], tag["id"]))
getDB().commit()
return redirect("/edit")
return render_template("edit.html", sound=sound, tags=tags) return render_template("edit.html", sound=sound, tags=tags)

View File

@ -8,11 +8,11 @@
<input type="text" id="search" placeholder="Search Tag" /> <input type="text" id="search" placeholder="Search Tag" />
<ul class="tags"> <ul class="tags">
{% for tag in tags %} {% for tag in tags %}
<li class="tag"><span>{{ tag.name }}</span><input type="checkbox" {{ 'checked="true" '|safe if tag.checked }}/></li> <li class="tag"><span>{{ tag.name }}</span><input type="checkbox" name="cb-{{ tag.name }}" {{ 'checked="true" '|safe if tag.checked }}/></li>
{% endfor %} {% endfor %}
</ul> </ul>
<input type="button" value="Cancel" /> <input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Save" /> <input type="submit" name="submit" value="Save" />
</form> </form>
</div> </div>
{% endblock %} {% endblock %}