summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Chan <ryanchanwo@gmail.com>2019-02-12 12:35:50 +0000
committerDrew DeVault <sir@cmpwn.com>2019-02-12 09:59:02 -0500
commitf64d58b07bcb34f3a7b13cbe5922c92328443202 (patch)
treeaa47a8ab7475e26c14635e566ae0b63719f8cee7
parent0c4c3d2190ad9c1b8592725eeb4c365b5e5eca9c (diff)
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.
-rw-r--r--mansrht/blueprints/html.py18
-rw-r--r--mansrht/templates/content.html26
-rw-r--r--mansrht/templates/delete.html20
-rw-r--r--mansrht/templates/manage.html37
-rw-r--r--mansrht/wikis.py11
5 files changed, 99 insertions, 13 deletions
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/~<owner_name>/<wiki_name>/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/~<owner_name>/<wiki_name>/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 %}
</dd>
</dl>
- <!--
- <div class="row">
- <div class="col-8 offset-2">
- <a href="#" class="btn btn-default btn-block">Manage your wiki</a>
- </div>
- </div>
- -->
- {% if is_root and current_user %}
- <hr />
- <a href="/wiki/create" class="btn btn-primary btn-block">
- Create a wiki {{icon("caret-right")}}
- </a>
+ {% if current_user %}
+ <hr />
+ {% if wiki and wiki.owner.username == current_user.username %}
+ <a
+ href="{{ url_for("html.manage_delete",
+ owner_name=wiki.owner.username, wiki_name=wiki.name) }}"
+ class="btn btn-default btn-block"
+ >Manage your wiki</a>
+ {% endif %}
+ {% if is_root %}
+ <a href="/wiki/create" class="btn btn-primary btn-block">
+ Create a wiki {{icon("caret-right")}}
+ </a>
+ {% endif %}
{% endif %}
</div>
</div>
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 %}
+<div class="row">
+ <div class="col-md-12">
+ <p>This will permanently delete the wiki.
+ <strong>~{{ owner.username }}/{{ wiki.name }}</strong>.
+ This cannot be undone.
+ </p>
+ <form method="POST">
+ {{csrf_token()}}
+ <button type="submit" class="btn btn-danger">
+ Proceed and delete {{icon("caret-right")}}
+ </button>
+ <a
+ class="btn btn-default"
+ href="{{ url_for("html.user_content",
+ owner_name=owner.username, wiki_name=wiki.name) }}"
+ >Nevermind</a>
+ </form>
+{% 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 %}
+<!--
+<h2>Wiki settings</h2>
+<section>
+ <p>
+ You can manage settings for
+ <strong>~{{ owner.username }}/{{ wiki.name }}</strong> here.</p>
+</section>
+-->
+<div class="container-fluid">
+ <div class="row">
+ <div class="col-md-12 header-tabbed">
+ {% block tabs %}
+ <ul class="nav nav-tabs">
+ <li class="nav-item">
+ <a class="nav-link" href="/~{{ owner.username }}/{{ wiki.name }}">
+ {{icon("caret-left")}} back
+ </a>
+ </li>
+ <li class="nav-item">
+ <!-- Only option at the moment so we assume it's active. -->
+ <a
+ class="nav-link active"
+ href="{{ url_for("html.manage_delete",
+ owner_name=owner.username, wiki_name=wiki.name) }}"
+ > delete </a>
+ </li>
+ </ul>
+ {% endblock %}
+ </div>
+ </div>
+</div>
+<div class="container">
+ {% block content %}{% endblock %}
+</div>
+{% 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()