From fb3c1f07d6488cfe91f3a5232224ff82e53ec339 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 25 Jun 2021 23:24:21 +0000 Subject: db things, display poll on thread page, start of voting interface --- apioforum/db.py | 26 ++++++++++++++++++++++++++ apioforum/templates/view_thread.html | 22 ++++++++++++++++++++++ apioforum/thread.py | 10 +++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/apioforum/db.py b/apioforum/db.py index e5159db..271ab75 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -84,6 +84,32 @@ CREATE TABLE thread_tags ( ALTER TABLE users ADD COLUMN bio TEXT; ALTER TABLE users ADD COLUMN joined TIMESTAMP; """, +""" +CREATE TABLE polls ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL +); +ALTER TABLE threads ADD COLUMN poll INTEGER REFERENCES polls(id); + +CREATE TABLE poll_options ( + poll INTEGER NOT NULL REFERENCES polls(id), + text TEXT NOT NULL, + option_idx INTEGER NOT NULL, + PRIMARY KEY ( poll, option_idx ) +); + +CREATE TABLE votes ( + id INTEGER PRIMARY KEY, + user TEXT NOT NULL REFERENCES users(username), + poll INTEGER NOT NULL, + option_idx INTEGER NOT NULL, + time TIMESTAMP NOT NULL, + FOREIGN KEY ( poll, option_idx ) REFERENCES poll_options(poll, option_idx) +); +ALTER TABLE posts ADD COLUMN vote INTEGER REFERENCES votes(id); +""", + + ] def init_db(): diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index fb62880..855f792 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -5,6 +5,14 @@ {% endblock %} {%block content%} +{% if poll %} +

{{poll.title}}

+ +{% endif %}
{% if g.user == thread.creator %} @@ -27,6 +35,20 @@ {% if g.user %}
+ {% if poll %} +
+ poll: {{poll.title}} +

if you want, you can submit a vote along with this post. if you have previously voted + on this poll, your previous vote will be changed

+ + + {% for opt in poll.options %} +
+ + + {% endfor %} +
+ {% endif %}
{% else %} diff --git a/apioforum/thread.py b/apioforum/thread.py index e6d3690..3dbc32d 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -27,7 +27,15 @@ def view_thread(thread_id): INNER JOIN thread_tags ON thread_tags.tag = tags.id WHERE thread_tags.thread = ? ORDER BY tags.id""",(thread_id,)).fetchall() - return render_template("view_thread.html",posts=posts,thread=thread,tags=tags) + poll = None + if thread['poll'] is not None: + poll_row = db.execute("SELECT * FROM polls where id = ?",(thread['poll'],)).fetchone() + options = db.execute("SELECT * FROM poll_options WHERE poll = ?",(poll_row['id'],)).fetchall() + poll = {} + poll.update(poll_row) + poll['options'] = options + + return render_template("view_thread.html",posts=posts,thread=thread,tags=tags,poll=poll) @bp.route("//create_post", methods=("POST",)) def create_post(thread_id): -- cgit v1.2.3