summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323>2021-06-25 23:24:21 +0000
committerubq323 <ubq323>2021-06-25 23:24:21 +0000
commitfb3c1f07d6488cfe91f3a5232224ff82e53ec339 (patch)
tree22c2f034e54d049df051946768df20dd0ccb8109
parent3c2a773095e23a75d2fcb1ca92699228313f6715 (diff)
db things, display poll on thread page, start of voting interface
-rw-r--r--apioforum/db.py26
-rw-r--r--apioforum/templates/view_thread.html22
-rw-r--r--apioforum/thread.py10
3 files changed, 57 insertions, 1 deletions
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 %}
+<p>{{poll.title}}</p>
+<ul>
+ {%for opt in poll.options%}
+ <li>#{{opt.option_idx}} - {{opt.text}}</li>
+ {%endfor%}
+</ul>
+{% endif %}
<div class="thread-top-bar">
<span class="thread-top-bar-a">
{% if g.user == thread.creator %}
@@ -27,6 +35,20 @@
{% if g.user %}
<form class="new-post" action="{{url_for('thread.create_post',thread_id=thread.id)}}" method="POST">
<textarea class="new-post-box" placeholder="your post here..." name="content"></textarea>
+ {% if poll %}
+ <fieldset>
+ <legend>poll: {{poll.title}}</legend>
+ <p>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</p>
+ <input type="radio" id="dontvote" name="poll" value="dontvote" checked>
+ <label for="dontvote">do not submit any vote at the moment</label>
+ {% for opt in poll.options %}
+ <br>
+ <input type="radio" id="option_{{opt.option_idx}}" name="poll" value="{{opt.option_idx}}">
+ <label for="option_{{opt.option_idx}}">#{{opt.option_idx}} - {{opt.text}}</label>
+ {% endfor %}
+ </fieldset>
+ {% endif %}
<input type="submit" value="yes">
</form>
{% 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("/<int:thread_id>/create_post", methods=("POST",))
def create_post(thread_id):