WebApp-Kategorien bearbeiten
This commit is contained in:
parent
9205d11802
commit
f1e1c247ec
3 changed files with 175 additions and 0 deletions
90
app.py
90
app.py
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue