From 742ee5b3e9b053932b8bc7f189e404588e2919fb Mon Sep 17 00:00:00 2001 From: iou1name Date: Wed, 18 Jul 2018 22:08:36 -0400 Subject: [PATCH] non-multi-choice polls are enforced --- database.py | 23 +++++++++++++++++------ events.py | 20 +++++++++++++++++--- static/anonkun.css | 4 ++-- templates/quest.html | 7 ++++--- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/database.py b/database.py index 9a33738..b2bb914 100644 --- a/database.py +++ b/database.py @@ -281,10 +281,10 @@ def insert_poll(post_id, quest_id, multi_choice, allow_writein): (post_id, quest_id, multi_choice, allow_writein)) -def get_polls(quest_id): +def get_poll(post_id): """Gets poll information.""" data = _DB.execute( - "SELECT * FROM `polls` WHERE `quest_id` = %s", (quest_id,)).fetchall() + "SELECT * FROM `polls` WHERE `post_id` = %s", (post_id,)).fetchone() return data @@ -307,24 +307,24 @@ def get_poll_options(quest_id): return data -def insert_poll_vote(option_id, ip): +def insert_poll_vote(option_id, ip_address): """Inserts a new vote for a poll option.""" try: _DB.execute( "INSERT INTO `poll_votes` " \ "(`option_id`, `ip_address`) VALUES (%s, %s)", - (option_id, ip)) + (option_id, ip_address)) except MySQLdb.IntegrityError: # this ip has already voted for this option return -def remove_poll_vote(option_id, ip): +def remove_poll_vote(option_id, ip_address): """Removes a vote from a poll.""" _DB.execute( "DELETE FROM `poll_votes` " \ "WHERE `option_id` = %s AND `ip_address` = %s", - (option_id, ip)) + (option_id, ip_address)) def get_poll_votes(quest_id): @@ -337,3 +337,14 @@ def get_poll_votes(quest_id): + "WHERE `quest_data`.`quest_id` = %s", (quest_id,)).fetchall() return data + + +def get_poll_votes_voted(post_id, ip_address): + """Gets all votes made by the given ip_address for the given poll.""" + data = _DB.execute( + "SELECT `poll_votes`.* FROM `poll_votes` LEFT JOIN `poll_options` " \ + + "ON `poll_votes`.`option_id` = `poll_options`.`option_id` " \ + + "WHERE `poll_options`.`post_id` = %s " \ + + "AND `poll_votes`.`ip_address` = %s", + (post_id, ip_address)).fetchall() + return data diff --git a/events.py b/events.py index cb23f1f..2591f90 100644 --- a/events.py +++ b/events.py @@ -316,14 +316,28 @@ def vote(data): """ room = data.get("room") option_id = data.get("option_id") + post_id = data.get("post_id") polarity = data.get("polarity") #ip_address = request.remote_addr ip_address = request.headers.get("X-Real-Ip") - if polarity: - db.insert_poll_vote(option_id, ip_address) - else: + + if not polarity: db.remove_poll_vote(option_id, ip_address) + else: + poll = db.get_poll(post_id) + if poll[2]: # multi-vote allowed + db.insert_poll_vote(option_id, ip_address) + else: + votes = db.get_poll_votes_voted(post_id, ip_address) + for vote in votes: + db.remove_poll_vote(vote[0], ip_address) + data = {} + data["option_id"] = vote[0] + data["polarity"] = False + emit("vote", data, room=room) + + db.insert_poll_vote(option_id, ip_address) data = {} data["option_id"] = option_id diff --git a/static/anonkun.css b/static/anonkun.css index 21c1977..1437e3e 100644 --- a/static/anonkun.css +++ b/static/anonkun.css @@ -107,7 +107,7 @@ h3 { } .pollOption { - width: 70%; + width: 100%; margin: 0.1em; } @@ -123,7 +123,7 @@ h3 { } .optionVotes { - width: 10%; + width: 4em; text-align: center; } diff --git a/templates/quest.html b/templates/quest.html index 68f1477..ebc7465 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -120,8 +120,9 @@ date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds()); return date_str; } - function pollVote(option_id) { - socket.emit('vote', {option_id: option_id, polarity: document.getElementById('pollInput-' + option_id).checked, room: {{ room_id }}}); + function pollVote(post_id, option_id) { + let polarity = document.getElementById('pollInput-' + option_id).checked; + socket.emit('vote', {post_id: post_id, option_id: option_id, polarity: polarity, room: {{ room_id }}}); } {% if session.get("user_id") == owner_id %} @@ -262,7 +263,7 @@ {% for option in options[quest_post[0]] %} - + {{ option[2] }}