Admin: add title field, live preview, and delete; DB migration for title; show title in list; keep Admin button on public pages
This commit is contained in:
29
app.py
29
app.py
@@ -53,11 +53,19 @@ def create_app():
|
||||
CREATE TABLE IF NOT EXISTS pages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
uuid TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
html TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
)
|
||||
# Migration: add 'title' column for older DB versions
|
||||
try:
|
||||
cols = {row[1] for row in db.execute("PRAGMA table_info(pages)").fetchall()}
|
||||
if "title" not in cols:
|
||||
db.execute("ALTER TABLE pages ADD COLUMN title TEXT NOT NULL DEFAULT ''")
|
||||
except Exception:
|
||||
pass
|
||||
db.commit()
|
||||
|
||||
@app.before_request
|
||||
@@ -120,29 +128,42 @@ def create_app():
|
||||
login_required()
|
||||
db = get_db()
|
||||
if request.method == "POST":
|
||||
title = request.form.get("title", "").strip()
|
||||
html = request.form.get("html", "").strip()
|
||||
if not html:
|
||||
flash("HTML не может быть пустым.", "error")
|
||||
else:
|
||||
uid = uuid_lib.uuid4().hex
|
||||
db.execute(
|
||||
"INSERT INTO pages (uuid, html, created_at) VALUES (?, ?, ?)",
|
||||
(uid, html, datetime.utcnow().isoformat(timespec="seconds")),
|
||||
"INSERT INTO pages (uuid, title, html, created_at) VALUES (?, ?, ?, ?)",
|
||||
(uid, title, html, datetime.utcnow().isoformat(timespec="seconds")),
|
||||
)
|
||||
db.commit()
|
||||
flash("Страница опубликована.", "success")
|
||||
return redirect(url_for("admin"))
|
||||
|
||||
pages = db.execute(
|
||||
"SELECT id, uuid, created_at FROM pages ORDER BY id DESC"
|
||||
"SELECT id, uuid, title, created_at FROM pages ORDER BY id DESC"
|
||||
).fetchall()
|
||||
base_url = request.host_url.rstrip("/")
|
||||
return render_template("admin.html", pages=pages, base_url=base_url)
|
||||
|
||||
@app.route("/admin/delete/<int:pid>", methods=["POST"])
|
||||
def admin_delete(pid: int):
|
||||
login_required()
|
||||
db = get_db()
|
||||
cur = db.execute("DELETE FROM pages WHERE id = ?", (pid,))
|
||||
db.commit()
|
||||
if cur.rowcount:
|
||||
flash("Страница удалена.", "success")
|
||||
else:
|
||||
flash("Страница не найдена.", "error")
|
||||
return redirect(url_for("admin"))
|
||||
|
||||
@app.route("/p/<string:uid>")
|
||||
def view_page(uid: str):
|
||||
db = get_db()
|
||||
row = db.execute("SELECT html FROM pages WHERE uuid = ?", (uid,)).fetchone()
|
||||
row = db.execute("SELECT html, title FROM pages WHERE uuid = ?", (uid,)).fetchone()
|
||||
if row is None:
|
||||
abort(404)
|
||||
# Show a floating admin button for authenticated users, otherwise serve raw HTML
|
||||
|
||||
Reference in New Issue
Block a user