diff --git a/royalnet/web/royalprints/wikiedit/__init__.py b/royalnet/web/royalprints/wikiedit/__init__.py index 0d918fef..33487c7c 100644 --- a/royalnet/web/royalprints/wikiedit/__init__.py +++ b/royalnet/web/royalprints/wikiedit/__init__.py @@ -2,6 +2,8 @@ import flask as f import uuid import os +import datetime +import difflib from ... import Royalprint from ....database.tables import Royal, WikiPage, WikiRevision @@ -14,6 +16,9 @@ rp = Royalprint("wikiedit", __name__, url_prefix="/wikiedit", template_folder=tm @rp.route("/", defaults={"title": ""}, methods=["GET", "POST"]) @rp.route("//", methods=["GET", "POST"]) def wikiedit_by_id(page_id: uuid.UUID, title: str): + if "royal" not in f.session: + return "Please login to edit wiki pages", 403 + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none() if page is None: @@ -23,4 +28,24 @@ def wikiedit_by_id(page_id: uuid.UUID, title: str): return f.render_template("wikiedit_page.html", page=page) elif f.request.method == "POST": - return "" \ No newline at end of file + fd = f.request.form + if "content" not in fd: + return "Missing page content", 400 + # Create new revision + revision = alchemy.WikiRevision(revision_id=uuid.uuid4(), + page_id=page.page_id, + author_id=f.session["royal"]["uid"], + timestamp=datetime.datetime.now(), + reason=fd.get("reason"), + diff="\n".join(difflib.unified_diff(page.content.split("\n"), fd["content"].split("\n")))) + alchemy_session.add(revision) + # Apply changes + page.content = fd["content"] + if "title" in fd: + page.title = fd["title"] + if "format" in fd: + page.format = fd["format"] + if "css" in fd: + page.css = fd["css"] if fd["css"] != "None" else None + alchemy_session.commit() + return f.redirect(f.url_for("wikiview.wikiview_by_id", page_id=page.page_id, title=page.title))