aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Yu <andrew@andrewyu.org>2022-12-23 04:16:29 +0100
committerAndrew Yu <andrew@andrewyu.org>2022-12-23 04:16:29 +0100
commitfe9e7573900a191c48135b5eec18dc3408e77fcc (patch)
tree637a89a8b505b6929f5dfe3036eb93dc8f7042da
parentac60c0f7ce143a96ad0a52fa2c04fb24f37b26d7 (diff)
downloadqbox-fe9e7573900a191c48135b5eec18dc3408e77fcc.tar.gz
qbox-fe9e7573900a191c48135b5eec18dc3408e77fcc.zip
Added previous question displaying.
-rw-r--r--app.py43
-rw-r--r--templates/home.html9
2 files changed, 36 insertions, 16 deletions
diff --git a/app.py b/app.py
index 614a541..be1a505 100644
--- a/app.py
+++ b/app.py
@@ -20,17 +20,21 @@
from flask import Flask
from flask import render_template, request, redirect, abort
from flask.wrappers import Response
+from html import escape
import time, os
import json
-try:
- db_file = open("db.json", "r+")
-except FileNotFoundError:
- db = []
-else:
- db = json.load(db_file)
- assert type(db) is list
- db_file.close()
+def ldb():
+ try:
+ db_file = open("db.json", "r+")
+ except FileNotFoundError:
+ db = []
+ else:
+ db = json.load(db_file)
+ assert type(db) is list
+ db_file.close()
+ return db
+db = ldb()
app = Flask(__name__)
@@ -39,10 +43,29 @@ def append_question(text, ts):
with open("db.json", "w") as db_file:
json.dump(db, db_file)
+def gpq():
+ gd = ""
+ for qs in reversed(db):
+ if not qs["a"]: continue
+ gd += "<hr />"
+ gd += "<div class=\"single-past-question\">"
+ gd += "<pre class=\"past-question-question\">"
+ gd += escape(qs["q"])
+ gd += "</pre>"
+ gd += "<span class=\"past-question-answer\">"
+ gd += qs["a"] # answers are trusted and may include HTML
+ gd += "</span>"
+ gd += "</div>"
+ return gd
+
+
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
- return render_template('home.html', bin_url = request.base_url)
+ global db
+ db = ldb()
+ return Response(open("templates/home.html", "r").read().replace("{{pq}}", gpq()), mimetype='text/html')
+ #return render_template('home.html', pq=gpq())
elif request.method == 'POST':
ts = str(time.time())
if "text" in request.form and request.form["text"].strip():
@@ -51,7 +74,7 @@ def index():
else:
return Response("Empty submissions are forbidden.", mimetype='text/plain')
return Response("Submission successful.", mimetype='text/plain')
- return "Invalid request.", 400 # Never reached if the request is valid.
+ return "Invalid request.", 400
if __name__ == "__main__":
app.run(port=8000)
diff --git a/templates/home.html b/templates/home.html
index 48dd1a1..a6b5ca1 100644
--- a/templates/home.html
+++ b/templates/home.html
@@ -12,20 +12,17 @@
<form method="POST" enctype="multipart/form-data">
What do you want to say to me or ask me?<br />
<textarea rows="10" cols="70" name="text" placeholder="Insert some text here."></textarea><br />
- By pressing ``Submit'' below, you promise that you wrote the text submitted in the text box above, you waive all copyright and related rights to the text, and you agree that the may be publicly displayed and commented upon on this Website.<br />You also agree not to use this to conduct illegal activities, including malicious attempts to hijck my server or similar misuse. I encourage you to report potential vulnerabilities responsibly by <a href="https://www.andrewyu.org/contact.html">contacting me privately</a>.<br />
+ By submitting an HTTP POST request to this URL, for example by pressing ``Submit'' below, you promise that you wrote the text submitted in the text box above, you waive all copyright and related rights to the text to the extent permitted by applicable law, and you agree that the may be publicly displayed and commented upon on this Website.<br />Do not use this to conduct illegal activities, including malicious attempts to hijck my server or similar misuse. Please report potential vulnerabilities responsibly by <a href="https://www.andrewyu.org/contact.html">contacting me privately</a>.<br />
<input type="submit" value="Submit"/ >
</form>
<h2>Previous Questions</h2>
- <div id="pq">
- Alright I haven't finished developing the ``view past answers'' part yet but I'll add that sometime soon.
- {{pq}}
- </div>
+ {{pq}}
<div id="footer">
<hr />
<p>
- The server software behind this question board is <b>qbox</b>, a simple HTML-form question board server, licensed under <a href="https://git.andrewyu.org/andrew/qbox.git/plain/COPYING">"version 3 of the GNU Affero General Public License</a>. You may find its source code at <a href="https://git.andrewyu.org/andrew/qbox.git/">https://git.andrewyu.org/andrew/qbox.git/</a>.
+ The server software behind this question board is <b>qbox</b>, a simple HTML-form question board server, licensed under <a href="https://git.andrewyu.org/andrew/qbox.git/plain/COPYING">version 3 of the GNU Affero General Public License</a>. You may find its source code at <a href="https://git.andrewyu.org/andrew/qbox.git/">https://git.andrewyu.org/andrew/qbox.git/</a>.
</p>
</div>
</body>