aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2018-05-02 14:50:09 +0100
committerGravatar Gareth Coles <[email protected]>2018-05-02 14:50:09 +0100
commit9dc05b5324b478ab84276f6f0ac993c56cc13c3a (patch)
tree8fdeb978e5529d4f631e72bf6e752e594762b6bb
parent[All Pages] Fix links (diff)
[Wiki] Implement page deletions and improve sidebar construction
-rw-r--r--pysite/views/wiki/delete.py67
-rw-r--r--templates/wiki/base.html87
-rw-r--r--templates/wiki/page_delete.html26
-rw-r--r--templates/wiki/page_edit.html2
4 files changed, 146 insertions, 36 deletions
diff --git a/pysite/views/wiki/delete.py b/pysite/views/wiki/delete.py
new file mode 100644
index 00000000..0e3bcbbf
--- /dev/null
+++ b/pysite/views/wiki/delete.py
@@ -0,0 +1,67 @@
+import datetime
+
+import requests
+from flask import redirect, url_for
+from werkzeug.exceptions import NotFound
+
+from pysite.base_route import RouteView
+from pysite.constants import EDITOR_ROLES, WIKI_AUDIT_WEBHOOK
+from pysite.decorators import csrf, require_roles
+from pysite.mixins import DBMixin
+
+
+class EditView(RouteView, DBMixin):
+ path = "/delete/<path:page>" # "path" means that it accepts slashes
+ name = "delete"
+ table_name = "wiki"
+ revision_table_name = "wiki_revisions"
+
+ @require_roles(*EDITOR_ROLES)
+ def get(self, page):
+ obj = self.db.get(self.table_name, page)
+
+ if obj:
+ title = obj.get("title", "")
+
+ if obj.get("lock_expiry") and obj.get("lock_user") != self.user_data.get("user_id"):
+ lock_time = datetime.datetime.fromtimestamp(obj["lock_expiry"])
+ if datetime.datetime.utcnow() < lock_time:
+ return self.render("wiki/page_in_use.html", page=page)
+
+ return self.render("wiki/page_delete.html", page=page, title=title)
+ else:
+ raise NotFound()
+
+ @require_roles(*EDITOR_ROLES)
+ @csrf
+ def post(self, page):
+ obj = self.db.get(self.table_name, page)
+
+ if not obj:
+ raise NotFound()
+
+ self.db.delete(self.table_name, page)
+ self.db.delete(self.revision_table_name, page)
+
+ self.audit_log(obj)
+
+ return redirect(url_for("wiki.page", page="home"), code=303) # Redirect, ensuring a GET
+
+ def audit_log(self, obj):
+ if WIKI_AUDIT_WEBHOOK: # If the audit webhook is not configured there is no point processing it
+ audit_payload = {
+ "username": "Wiki Updates",
+ "embeds": [
+ {
+ "title": "Page Deletion",
+ "description": f"**{obj['title']}** was deleted by **{self.user_data.get('username')}**",
+ "color": 4165079,
+ "timestamp": datetime.datetime.utcnow().isoformat(),
+ "thumbnail": {
+ "url": "https://pythondiscord.com/static/logos/logo_discord.png"
+ }
+ }
+ ]
+ }
+
+ requests.post(WIKI_AUDIT_WEBHOOK, json=audit_payload)
diff --git a/templates/wiki/base.html b/templates/wiki/base.html
index 28431324..eec0ecb8 100644
--- a/templates/wiki/base.html
+++ b/templates/wiki/base.html
@@ -70,24 +70,47 @@
<i class="uk-icon fas fa-fw fa-cube"></i> &nbsp;Minecraft
</a></li>
- <li class="uk-nav-divider"></li>
+ {% set ACTIONABLE_PAGES = ["page", "edit", "history.show", "history.compare", "source", "delete"] %}
+ {% set actionable = current_page in ACTIONABLE_PAGES %}
- {% if (can_edit or debug) and current_page != "edit" %}
- <li>
- <a href="{{ url_for("wiki.edit", page=page) }}">
- <i class="uk-icon fas fa-fw fa-pencil-alt"></i> &nbsp;Edit
- </a>
- </li>
- {% elif current_page == "edit" %}
- <li>
- <a href="{{ url_for("wiki.page", page=page) }}">
- <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
- </a>
- </li>
- {% endif %}
+ {% if actionable %}
+ <li class="uk-nav-divider"></li>
+
+ {% if current_page == "edit" %}
+ <li>
+ <a href="{{ url_for("wiki.page", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
+ </a>
+ </li>
+ {% else %}
+ <li>
+ <a href="{{ url_for("wiki.edit", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-pencil-alt"></i> &nbsp;Edit
+ </a>
+ </li>
+ {% endif %}
+
+ {% if current_page == "delete" %}
+ <li>
+ <a href="{{ url_for("wiki.page", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
+ </a>
+ </li>
+ {% else %}
+ <li>
+ <a href="{{ url_for("wiki.delete", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-trash"></i> &nbsp;Delete
+ </a>
+ </li>
+ {% endif %}
- {% if current_page != "history.show" %}
- {% if current_page == "history.compare" %}
+ {% if current_page == "history.show" %}
+ <li>
+ <a href="{{ url_for("wiki.page", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
+ </a>
+ </li>
+ {% elif current_page == "history.compare" %}
<li>
<a href="{{ url_for("wiki.history.show", page=slug) }}">
<i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
@@ -100,26 +123,20 @@
</a>
</li>
{% endif %}
- {% else %}
- <li>
- <a href="{{ url_for("wiki.page", page=page) }}">
- <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
- </a>
- </li>
- {% endif %}
- {% if current_page != "source" %}
- <li>
- <a href="{{ url_for("wiki.source", page=page) }}">
- <i class="uk-icon fas fa-fw fa-code"></i> &nbsp;Source
- </a>
- </li>
- {% else %}
- <li>
- <a href="{{ url_for("wiki.page", page=page) }}">
- <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
- </a>
- </li>
+ {% if current_page == "source" %}
+ <li>
+ <a href="{{ url_for("wiki.page", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-arrow-left"></i> &nbsp;Back
+ </a>
+ </li>
+ {% else %}
+ <li>
+ <a href="{{ url_for("wiki.source", page=page) }}">
+ <i class="uk-icon fas fa-fw fa-code"></i> &nbsp;Source
+ </a>
+ </li>
+ {% endif %}
{% endif %}
<li class="uk-nav-divider"></li>
diff --git a/templates/wiki/page_delete.html b/templates/wiki/page_delete.html
new file mode 100644
index 00000000..f4d52653
--- /dev/null
+++ b/templates/wiki/page_delete.html
@@ -0,0 +1,26 @@
+{% extends "wiki/base.html" %}
+{% block title %}Wiki | Delete: {{ page }}{% endblock %}
+{% block og_title %}Wiki | Delete: {{ page }}{% endblock %}
+{% block og_description %}{% endblock %}
+{% block extra_head %}
+<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/ace.js" type="application/javascript"></script>
+{% endblock %}
+{% block content %}
+ <div uk-alert class="uk-alert-danger">
+ <h3>Delete Page: {{ page }}</h3>
+ <p>
+ Are you sure you want to delete this page?
+ </p>
+
+ <form uk-grid class="uk-grid-small" action="{{ url_for("wiki.delete", page=page) }}" method="post">
+ <div class="uk-width-1-2">
+ <a href="{{ url_for("wiki.page", page=page) }}" class="uk-button uk-button-primary uk-width-1-1" type="button" value="Cancel" id="cancel">Cancel</a>
+ </div>
+ <div class="uk-width-1-2">
+ <input class="uk-button uk-button-secondary uk-width-1-1" type="submit" id="delete" value="Delete" />
+ </div>
+
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
+ </form>
+ </div>
+{% endblock %}
diff --git a/templates/wiki/page_edit.html b/templates/wiki/page_edit.html
index a2d709e2..51ce70db 100644
--- a/templates/wiki/page_edit.html
+++ b/templates/wiki/page_edit.html
@@ -1,7 +1,7 @@
{% extends "wiki/base.html" %}
{% block title %}Wiki | Edit: {{ page }}{% endblock %}
{% block og_title %}Wiki | Edit: {{ page }}{% endblock %}
-{% block og_description %}Landing page for the wiki{% endblock %}
+{% block og_description %}{% endblock %}
{% block extra_head %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/ace.js" type="application/javascript"></script>
{% endblock %}