From aa5cb822959858386d6c554d0082a5731619b3df Mon Sep 17 00:00:00 2001 From: RGalyaviev Date: Wed, 3 Sep 2025 15:06:07 +0300 Subject: [PATCH] Admin: add title field, live preview, and delete; DB migration for title; show title in list; keep Admin button on public pages --- app.py | 29 +++++++++++++++++++++++++---- templates/admin.html | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 3791359..8ee1d2b 100644 --- a/app.py +++ b/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/", 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/") 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 diff --git a/templates/admin.html b/templates/admin.html index cce2185..6017210 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -3,6 +3,10 @@

Публикация HTML-страниц

+
+ + +
@@ -11,24 +15,37 @@ После публикации создаётся ссылка с UUID. +
+
Моментальный предпросмотр
+ +
+

Опубликованные страницы

{% if pages %} + + {% for p in pages %} + + {% endfor %} @@ -36,5 +53,24 @@ {% else %}

Страниц пока нет.

{% endif %} + {% endblock %} -
IDНазвание UUID Ссылка СозданоДействия
{{ p.id }}{{ p.title or "" }} {{ p.uuid }} {{ base_url }}/p/{{ p.uuid }} {{ p.created_at }} +
+ +
+