summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323>2021-06-29 00:30:02 +0000
committerubq323 <ubq323>2021-06-29 00:30:02 +0000
commitfecd96342a07589f71cac6fc1d28e2f6d3240cbf (patch)
treeba63731206249cce530d937aaa18820c868925aa
parentedbd48340de9add59bcf34f312ae036adae8dcfd (diff)
retraction of votes
-rw-r--r--apioforum/templates/view_thread.html8
-rw-r--r--apioforum/thread.py25
2 files changed, 28 insertions, 5 deletions
diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html
index b999658..7bf253d 100644
--- a/apioforum/templates/view_thread.html
+++ b/apioforum/templates/view_thread.html
@@ -64,8 +64,16 @@
<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>
+
+ {% if has_voted %}
+ <br>
+ <input type="radio" id="retractvote" name="poll" value="retractvote">
+ <label for="retractvote">retract my vote, and go back to having no vote on this poll</label>
+ {% endif %}
+
{% for opt in poll.options %}
<br>
<input type="radio" id="option_{{opt.option_idx}}" name="poll" value="{{opt.option_idx}}">
diff --git a/apioforum/thread.py b/apioforum/thread.py
index ec50b3b..ce451ab 100644
--- a/apioforum/thread.py
+++ b/apioforum/thread.py
@@ -47,6 +47,12 @@ def view_thread(thread_id):
for post in posts:
if post['vote'] is not None:
votes[post['id']] = db.execute("SELECT * FROM votes WHERE id = ?",(post['vote'],)).fetchone()
+
+ if g.user is None:
+ has_voted = None
+ else:
+ v = db.execute("SELECT * FROM votes WHERE poll = ? AND user = ? AND current AND NOT is_retraction;",(poll['id'],g.user)).fetchone()
+ has_voted = v is not None
return render_template(
"view_thread.html",
@@ -54,13 +60,21 @@ def view_thread(thread_id):
thread=thread,
tags=tags,
poll=poll,
- votes=votes
+ votes=votes,
+ has_voted=has_voted,
)
def register_vote(thread,pollval):
if pollval is None or pollval == 'dontvote':
return
- option_idx = int(pollval)
+
+ is_retraction = pollval == 'retractvote'
+
+ if is_retraction:
+ option_idx = None
+ else:
+ option_idx = int(pollval)
+
db = get_db()
cur = db.cursor()
cur.execute("""
@@ -68,10 +82,11 @@ def register_vote(thread,pollval):
SET current = 0
WHERE poll = ? AND user = ?;
""",(thread['poll'],g.user))
+
cur.execute("""
- INSERT INTO votes (user,poll,option_idx,time,current)
- VALUES (?,?,?,current_timestamp,1);
- """,(g.user,thread['poll'],option_idx))
+ INSERT INTO votes (user,poll,option_idx,time,current,is_retraction)
+ VALUES (?,?,?,current_timestamp,1,?);
+ """,(g.user,thread['poll'],option_idx,is_retraction))
vote_id = cur.lastrowid
return vote_id