WebApp-Kategorien bearbeiten

This commit is contained in:
hubobel 2026-06-20 16:27:02 +02:00
parent 9205d11802
commit f1e1c247ec
3 changed files with 175 additions and 0 deletions

90
app.py
View file

@ -162,6 +162,96 @@ def add_category():
return redirect("/categories")
@app.route("/categories/edit/<category>")
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/<category>",
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/<category>/<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(

View file

@ -55,11 +55,19 @@
</td>
<td>
<a href="/categories/edit/{{ category }}">
Bearbeiten
</a>
|
<a
href="/categories/delete/{{ category }}"
onclick="return confirm('Kategorie wirklich löschen?')">
Löschen
</a>
</td>
```

View file

@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>{{ category }}</title>
</head>
<body>
<h1>{{ category }}</h1>
<a href="/categories">
Zurück
</a>
<hr>
<h2>Neues Schlüsselwort</h2>
<form
action="/categories/add_word/{{ category }}"
method="post">
```
<input
type="text"
name="word"
required>
<button type="submit">
Hinzufügen
</button>
```
</form>
<hr>
<h2>Schlüsselwörter</h2>
<table border="1" cellpadding="5">
<tr>
<th>Wort</th>
<th>Aktion</th>
</tr>
{% for word in words %}
<tr>
<td>
{{ word }}
</td>
<td>
<a
href="/categories/delete_word/{{ category }}/{{ word }}"
onclick="return confirm('Wort wirklich löschen?')">
```
Löschen
```
</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>