release nouri 0.4.0 templates suggestions and mobile sheet
This commit is contained in:
+83
-1
@@ -4,7 +4,7 @@ from flask import Blueprint, flash, g, redirect, render_template, request, url_f
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from .auth import admin_required, can_remove_last_admin, validate_admin_user_form
|
||||
from .constants import ROLE_LABELS
|
||||
from .constants import DEFAULT_CATEGORIES, ROLE_LABELS
|
||||
from .db import get_db
|
||||
|
||||
|
||||
@@ -26,6 +26,18 @@ def get_household_user(user_id: int):
|
||||
return user
|
||||
|
||||
|
||||
def fetch_household_categories():
|
||||
return get_db().execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM household_categories
|
||||
WHERE household_id = ?
|
||||
ORDER BY is_active DESC, sort_order, LOWER(name)
|
||||
""",
|
||||
(g.user["household_id"],),
|
||||
).fetchall()
|
||||
|
||||
|
||||
@admin_bp.get("/users")
|
||||
@admin_required
|
||||
def user_list():
|
||||
@@ -178,3 +190,73 @@ def user_edit(user_id: int):
|
||||
flash(error, "error")
|
||||
|
||||
return render_template("admin/user_form.html", user=user, form_data=form_data, role_labels=ROLE_LABELS)
|
||||
|
||||
|
||||
@admin_bp.route("/categories", methods=("GET", "POST"))
|
||||
@admin_required
|
||||
def category_settings():
|
||||
if request.method == "POST":
|
||||
name = request.form.get("name", "").strip()
|
||||
if not name:
|
||||
flash("Bitte einen Kategorienamen eintragen.", "error")
|
||||
else:
|
||||
existing = get_db().execute(
|
||||
"""
|
||||
SELECT id
|
||||
FROM household_categories
|
||||
WHERE household_id = ? AND LOWER(name) = LOWER(?)
|
||||
""",
|
||||
(g.user["household_id"], name),
|
||||
).fetchone()
|
||||
if existing:
|
||||
get_db().execute(
|
||||
"UPDATE household_categories SET is_active = 1 WHERE id = ?",
|
||||
(existing["id"],),
|
||||
)
|
||||
flash("Die Kategorie ist wieder aktiv.", "success")
|
||||
else:
|
||||
sort_row = get_db().execute(
|
||||
"SELECT COALESCE(MAX(sort_order), 0) AS max_sort FROM household_categories WHERE household_id = ?",
|
||||
(g.user["household_id"],),
|
||||
).fetchone()
|
||||
get_db().execute(
|
||||
"""
|
||||
INSERT INTO household_categories (household_id, name, sort_order, is_active)
|
||||
VALUES (?, ?, ?, 1)
|
||||
""",
|
||||
(g.user["household_id"], name, int(sort_row["max_sort"]) + 10),
|
||||
)
|
||||
flash("Die Kategorie wurde ergänzt.", "success")
|
||||
get_db().commit()
|
||||
return redirect(url_for("admin.category_settings"))
|
||||
|
||||
return render_template(
|
||||
"admin/categories.html",
|
||||
categories=fetch_household_categories(),
|
||||
default_categories=DEFAULT_CATEGORIES,
|
||||
)
|
||||
|
||||
|
||||
@admin_bp.post("/categories/<int:category_id>/toggle")
|
||||
@admin_required
|
||||
def category_toggle(category_id: int):
|
||||
category = get_db().execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM household_categories
|
||||
WHERE id = ? AND household_id = ?
|
||||
""",
|
||||
(category_id, g.user["household_id"]),
|
||||
).fetchone()
|
||||
if category is None:
|
||||
flash("Die Kategorie wurde nicht gefunden.", "error")
|
||||
return redirect(url_for("admin.category_settings"))
|
||||
|
||||
new_state = 0 if category["is_active"] else 1
|
||||
get_db().execute(
|
||||
"UPDATE household_categories SET is_active = ? WHERE id = ?",
|
||||
(new_state, category_id),
|
||||
)
|
||||
get_db().commit()
|
||||
flash("Die Kategorie wurde aktualisiert.", "success")
|
||||
return redirect(url_for("admin.category_settings"))
|
||||
|
||||
Reference in New Issue
Block a user