From ccb0394a52050f21701c856239f999ede529f71b Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 26 Jun 2021 00:07:10 +0000 Subject: voting now works --- apioforum/db.py | 1 + apioforum/thread.py | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/apioforum/db.py b/apioforum/db.py index 271ab75..1faa167 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -104,6 +104,7 @@ CREATE TABLE votes ( poll INTEGER NOT NULL, option_idx INTEGER NOT NULL, time TIMESTAMP NOT NULL, + current INTEGER NOT NULL, FOREIGN KEY ( poll, option_idx ) REFERENCES poll_options(poll, option_idx) ); ALTER TABLE posts ADD COLUMN vote INTEGER REFERENCES votes(id); diff --git a/apioforum/thread.py b/apioforum/thread.py index 3e03a67..559f9b2 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -37,6 +37,29 @@ def view_thread(thread_id): return render_template("view_thread.html",posts=posts,thread=thread,tags=tags,poll=poll) + + + +def register_vote(thread,pollval): + if pollval is None or pollval == 'dontvote': + return + option_idx = int(pollval) + db = get_db() + cur = db.cursor() + cur.execute(""" + UPDATE votes + 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)) + vote_id = cur.lastrowid + return vote_id + + + @bp.route("//create_post", methods=("POST",)) def create_post(thread_id): if g.user is None: @@ -46,17 +69,25 @@ def create_post(thread_id): db = get_db() content = request.form['content'] thread = db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone() - print(request.form.get('poll')) if len(content.strip()) == 0: flash("you cannot post an empty message") elif not thread: flash("that thread does not exist") else: + vote_id = None + if thread['poll'] is not None: + pollval = request.form.get('poll') + try: + vote_id = register_vote(thread,pollval) + except ValueError: + flash("invalid poll form value") + return redirect(url_for('thread.view_thread',thread_id=thread_id)) + cur = db.cursor() - cur.execute( - "INSERT INTO posts (thread,author,content,created) VALUES (?,?,?,current_timestamp);", - (thread_id,g.user,content) - ) + cur.execute(""" + INSERT INTO posts (thread,author,content,created,vote) + VALUES (?,?,?,current_timestamp,?); + """,(thread_id,g.user,content,vote_id)) post_id = cur.lastrowid cur.execute( "UPDATE threads SET updated = current_timestamp WHERE id = ?;", -- cgit v1.2.3