diff --git a/royalnet/web/royalprints/wikiedit/__init__.py b/royalnet/web/royalprints/wikiedit/__init__.py index 33487c7c..bf5264cb 100644 --- a/royalnet/web/royalprints/wikiedit/__init__.py +++ b/royalnet/web/royalprints/wikiedit/__init__.py @@ -13,6 +13,36 @@ rp = Royalprint("wikiedit", __name__, url_prefix="/wikiedit", template_folder=tm required_tables={Royal, WikiPage, WikiRevision}) +@rp.route("/newpage", methods=["GET", "POST"]) +def wikiedit_newpage(): + if "royal" not in f.session: + return "Please login to edit wiki pages", 403 + + if f.request.method == "GET": + return f.render_template("wikiedit_page.html", page=None) + + elif f.request.method == "POST": + fd = f.request.form + if not ("title" in fd and "content" in fd and "css" in fd): + return "Missing field", 400 + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] + page = alchemy.WikiPage(page_id=uuid.uuid4(), + title=fd["title"], + content=fd["content"], + format="markdown", + css=fd["css"] if fd["css"] != "None" else None) + revision = alchemy.WikiRevision(revision_id=uuid.uuid4(), + page=page, + author_id=f.session["royal"]["uid"], + timestamp=datetime.datetime.now(), + reason=fd.get("reason"), + diff="\n".join(difflib.unified_diff([], page.content.split("\n")))) + alchemy_session.add(page) + alchemy_session.add(revision) + alchemy_session.commit() + return f.redirect(f.url_for("wikiview.wikiview_by_id", page_id=page.page_id, title=page.title)) + + @rp.route("/", defaults={"title": ""}, methods=["GET", "POST"]) @rp.route("//", methods=["GET", "POST"]) def wikiedit_by_id(page_id: uuid.UUID, title: str): @@ -29,11 +59,11 @@ def wikiedit_by_id(page_id: uuid.UUID, title: str): elif f.request.method == "POST": fd = f.request.form - if "content" not in fd: - return "Missing page content", 400 + if not ("title" in fd and "content" in fd and "css" in fd): + return "Missing field", 400 # Create new revision revision = alchemy.WikiRevision(revision_id=uuid.uuid4(), - page_id=page.page_id, + page=page, author_id=f.session["royal"]["uid"], timestamp=datetime.datetime.now(), reason=fd.get("reason"), @@ -41,11 +71,7 @@ def wikiedit_by_id(page_id: uuid.UUID, title: str): 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 + page.title = fd["title"] + 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)) diff --git a/royalnet/web/royalprints/wikiview/templates/wikiview_index.html b/royalnet/web/royalprints/wikiview/templates/wikiview_index.html index 4afb2780..e4d73745 100644 --- a/royalnet/web/royalprints/wikiview/templates/wikiview_index.html +++ b/royalnet/web/royalprints/wikiview/templates/wikiview_index.html @@ -12,6 +12,11 @@ </span> </div> <div class="bot"> + {% if session.royal %} + <a href="{{ url_for("wikiedit.wikiedit_newpage") }}" class="btn no-icon">Crea nuova pagina</a> + {% else %} + <button disabled title="Devi fare il login per creare nuove pagine!">Crea nuova pagina</button> + {% endif %} <ul> {% for page in pages %} <li><a href="{{ url_for("wikiview.wikiview_by_id", page_id=page.page_id|string, title=page.title) }}">{{ page.title }}</a></li> diff --git a/royalnet/web/royalprints/wikiview/templates/wikiview_page.html b/royalnet/web/royalprints/wikiview/templates/wikiview_page.html index adcd9933..6f7605e1 100644 --- a/royalnet/web/royalprints/wikiview/templates/wikiview_page.html +++ b/royalnet/web/royalprints/wikiview/templates/wikiview_page.html @@ -11,8 +11,13 @@ Wiki page </span> <span class="right"> - <a class="permalink no-icon" href="{{ url_for("wikiedit.wikiedit_by_id", page_id=page.page_id|string, title=page.title) }}">e</a> - <a class="permalink no-icon" href="{{ url_for("wikiview.wikiview_by_id", page_id=page.page_id|string, title=page.title) }}">#</a> + {% if session.royal %} + <a class="edit no-icon" href="{{ url_for("wikiedit.wikiedit_by_id", page_id=page.page_id|string, title=page.title) }}">Modifica</a> + {% else %} + <a class="edit no-icon faded" title="Devi fare il login per creare nuove pagine!">Modifica</a> + {% endif %} + | + <a class="permalink no-icon" href="{{ url_for("wikiview.wikiview_by_id", page_id=page.page_id|string, title=page.title) }}">Permalink</a> </span> </div> <div class="bot">