From f64d58b07bcb34f3a7b13cbe5922c92328443202 Mon Sep 17 00:00:00 2001 From: Ryan Chan Date: Tue, 12 Feb 2019 12:35:50 +0000 Subject: Add wiki management route to enable wiki deletion Add a new route to manage wikis. At the moment the only option available on this page is wiki deletion. --- mansrht/blueprints/html.py | 18 +++++++++++++++++- mansrht/templates/content.html | 26 ++++++++++++++------------ mansrht/templates/delete.html | 20 ++++++++++++++++++++ mansrht/templates/manage.html | 37 +++++++++++++++++++++++++++++++++++++ mansrht/wikis.py | 11 +++++++++++ 5 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 mansrht/templates/delete.html create mode 100644 mansrht/templates/manage.html diff --git a/mansrht/blueprints/html.py b/mansrht/blueprints/html.py index 2f0b354..6e99d7d 100644 --- a/mansrht/blueprints/html.py +++ b/mansrht/blueprints/html.py @@ -5,8 +5,9 @@ from srht.config import cfg from srht.flask import loginrequired from srht.markdown import markdown, extract_toc from srht.validation import Validation +from mansrht.access import UserAccess, check_access from mansrht.types import User, Wiki -from mansrht.wikis import create_wiki +from mansrht.wikis import create_wiki, delete_wiki from datetime import datetime from jinja2 import Markup from urllib.parse import urlparse, urlunparse @@ -99,6 +100,21 @@ def user_content(owner_name, wiki_name, path=None): repo = pygit2.Repository(os.path.join(wiki.path)) return content(repo, path, wiki) +@html.route("/manage/~//delete") +@loginrequired +def manage_delete(owner_name, wiki_name): + # check_access() guarantees owner and wiki are valid. + owner, wiki = check_access("~{}".format(owner_name), wiki_name, UserAccess.manage) + return render_template("delete.html", owner=owner, wiki=wiki) + +@html.route("/manage/~//delete", methods=["POST"]) +@loginrequired +def manage_delete_POST(owner_name, wiki_name): + # check_access() guarantees owner and wiki are valid. + owner, wiki = check_access("~{}".format(owner_name), wiki_name, UserAccess.manage) + delete_wiki(wiki) + return redirect("/") + @html.route("/wiki/create") @loginrequired def create_GET(): diff --git a/mansrht/templates/content.html b/mansrht/templates/content.html index 40f7f8a..0ea5dd2 100644 --- a/mansrht/templates/content.html +++ b/mansrht/templates/content.html @@ -59,18 +59,20 @@ Date: {{ctime.isoformat()}} {% endif %} - - {% if is_root and current_user %} -
- - Create a wiki {{icon("caret-right")}} - + {% if current_user %} +
+ {% if wiki and wiki.owner.username == current_user.username %} + Manage your wiki + {% endif %} + {% if is_root %} + + Create a wiki {{icon("caret-right")}} + + {% endif %} {% endif %} diff --git a/mansrht/templates/delete.html b/mansrht/templates/delete.html new file mode 100644 index 0000000..4ecd974 --- /dev/null +++ b/mansrht/templates/delete.html @@ -0,0 +1,20 @@ +{% extends "manage.html" %} +{% block content %} +
+
+

This will permanently delete the wiki. + ~{{ owner.username }}/{{ wiki.name }}. + This cannot be undone. +

+
+ {{csrf_token()}} + + Nevermind +
+{% endblock %} diff --git a/mansrht/templates/manage.html b/mansrht/templates/manage.html new file mode 100644 index 0000000..75ca3cb --- /dev/null +++ b/mansrht/templates/manage.html @@ -0,0 +1,37 @@ +{% extends "layout.html" %} +{% block body %} + +
+
+
+ {% block tabs %} + + {% endblock %} +
+
+
+
+ {% block content %}{% endblock %} +
+{% endblock %} diff --git a/mansrht/wikis.py b/mansrht/wikis.py index 47f967f..6d58477 100644 --- a/mansrht/wikis.py +++ b/mansrht/wikis.py @@ -41,3 +41,14 @@ def create_wiki(valid, owner): db.session.commit() return wiki + +def delete_wiki(wiki): + try: + shutil.rmtree(wiki.path) + except FileNotFoundError: + # If it's not found then we don't have to do anything + # to the filesystem. + pass + + db.session.delete(wiki) + db.session.commit() -- cgit v1.2.3