From 48254e98bff3127f6d66af1172f89f9779b34b27 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sat, 16 Jun 2018 03:17:00 -0400 Subject: [PATCH] added create_quest functionality --- README.md | 2 +- anonkun.py | 53 ++++++++++++++++++++++++++++++++++--- templates/create_quest.html | 13 +++++++++ templates/index.html | 3 ++- templates/quest.html | 29 +++++--------------- 5 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 templates/create_quest.html diff --git a/README.md b/README.md index ec13bd9..97726f6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ By popular demand, I'm building a better anonkun. It doesn't do much right now t ## Requirements Python 3.6+ MariaDB 10.2+ -Python packages: `flask flask_socketio flask-paranoid passlib argon2_cffi` +Python packages: `flask flask_socketio flask-paranoid passlib argon2_cffi bleach` ## Install ``` diff --git a/anonkun.py b/anonkun.py index 0605a01..332c26b 100644 --- a/anonkun.py +++ b/anonkun.py @@ -4,12 +4,14 @@ Simple file host using Flask. """ import os import time +import urllib from flask import Flask, session, request, abort, redirect, url_for, g, \ render_template from flask_socketio import SocketIO, emit, join_room from flask_paranoid import Paranoid import MySQLdb +import bleach class ReverseProxied(object): """ @@ -130,12 +132,47 @@ def unix2string(unix): messages = [{"name":"Anonymous", "date":1528998539, "message":"lol"}, {"name":"Namefag", "date":1528998521, "message":"kek"}] -@app.route("/quest/") -def quest(questName): +@app.route("/quest/") +def quest(quest_title): """ An arbituary quest page. """ - return render_template('quest.html', questName=questName, messages=messages) + quest_title, _, extra = quest_title.partition("/") + res = db_execute("SELECT * FROM `quests` WHERE `ident_title` = %s", + (quest_title,)) + data = res.fetchone() + if not data: + abort(404) + quest_id, quest_title, _, owner_id, quest_data = data + + res = db_execute("SELECT * FROM `chat_messages` WHERE `room_id` = '%s'", + (quest_id,)) + messages = res.fetchall() + return render_template('quest.html', + quest_title=quest_title, + quest_body=quest_data, + room_id=quest_id, + messages=messages) + + +@app.route("/create_quest", methods=["GET", "POST"]) +def create_quest(): + """ + Starts a new quest. + """ + if request.method == "GET": + return render_template("create_quest.html") + canon_title = request.form.get("quest_title") + quest_body = request.form.get("quest_body") + + ident_title = sanitize_title(canon_title) + quest_body = bleach.clean(quest_body.strip()) + quest_body = quest_body.replace("\n", "
") + + db_execute( + "INSERT INTO `quests` (`canon_title`, `ident_title`, `quest_data`) " \ + "VALUES (%s, %s, %s)", (canon_title, ident_title, quest_body)) + return redirect(url_for('quest', quest_title=ident_title)) @app.route("/") @@ -146,6 +183,16 @@ def index(): return render_template("index.html") +def sanitize_title(canon_title): + """ + Sanitizes the given canonical title for a quest and returns a + url-friendly version. + """ + ident_title = canon_title.lower().replace(" ", "-") + ident_title = urllib.parse.quote(ident_title) + return ident_title + + init() if __name__ == "__main__": app.run(host='0.0.0.0', port=5050) diff --git a/templates/create_quest.html b/templates/create_quest.html new file mode 100644 index 0000000..bccb19a --- /dev/null +++ b/templates/create_quest.html @@ -0,0 +1,13 @@ + + + + Make a new quest + + +
+
+ + +
+ + diff --git a/templates/index.html b/templates/index.html index d6694b6..9f8159b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,6 +5,7 @@

Quests 'n Shiet

- Unga Bunga Quest + Unga Bunga Quest
+ Create New Quest diff --git a/templates/quest.html b/templates/quest.html index cf3c549..6f673d7 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -1,7 +1,7 @@ - {{ questName }} + {{ quest_title }}