diff --git a/app.py b/app.py index 258062f..302826b 100644 --- a/app.py +++ b/app.py @@ -162,6 +162,96 @@ def add_category(): return redirect("/categories") +@app.route("/categories/edit/") +def edit_category(category): + + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + + if category not in categories: + return redirect("/categories") + + return render_template( + "edit_category.html", + category=category, + words=categories[category] + ) +@app.route( + "/categories/add_word/", + methods=["POST"] +) +def add_word(category): + + word = request.form["word"].strip() + + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + + if ( + category in categories + and word + and word not in categories[category] + ): + categories[category].append(word) + + with open( + CATEGORIES_FILE, + "w", + encoding="utf-8" + ) as f: + + json.dump( + categories, + f, + ensure_ascii=False, + indent=2 + ) + + return redirect( + f"/categories/edit/{category}" + ) +@app.route( + "/categories/delete_word//" +) +def delete_word(category, word): + + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + + if ( + category in categories + and word in categories[category] + ): + categories[category].remove(word) + + with open( + CATEGORIES_FILE, + "w", + encoding="utf-8" + ) as f: + + json.dump( + categories, + f, + ensure_ascii=False, + indent=2 + ) + + return redirect( + f"/categories/edit/{category}" + ) if __name__ == "__main__": app.run( diff --git a/templates/categories.html b/templates/categories.html index 9cc323a..80b824e 100644 --- a/templates/categories.html +++ b/templates/categories.html @@ -55,11 +55,19 @@ + + + Bearbeiten + + + | + Löschen + ``` diff --git a/templates/edit_category.html b/templates/edit_category.html new file mode 100644 index 0000000..ab16f63 --- /dev/null +++ b/templates/edit_category.html @@ -0,0 +1,77 @@ + + + + + + {{ category }} + + + +

{{ category }}

+ + + Zurück + + +
+ +

Neues Schlüsselwort

+ +
+ +``` + + + +``` + +
+ +
+ +

Schlüsselwörter

+ + + + + + + + +{% for word in words %} + + + + + + + + + +{% endfor %} + +
WortAktion
+{{ word }} + + + + +``` +Löschen +``` + + + +
+ + +