non-multi-choice polls are enforced

This commit is contained in:
iou1name 2018-07-18 22:08:36 -04:00
parent ee28c2950a
commit 742ee5b3e9
4 changed files with 40 additions and 14 deletions

View File

@ -281,10 +281,10 @@ def insert_poll(post_id, quest_id, multi_choice, allow_writein):
(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.""" """Gets poll information."""
data = _DB.execute( 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 return data
@ -307,24 +307,24 @@ def get_poll_options(quest_id):
return data 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.""" """Inserts a new vote for a poll option."""
try: try:
_DB.execute( _DB.execute(
"INSERT INTO `poll_votes` " \ "INSERT INTO `poll_votes` " \
"(`option_id`, `ip_address`) VALUES (%s, %s)", "(`option_id`, `ip_address`) VALUES (%s, %s)",
(option_id, ip)) (option_id, ip_address))
except MySQLdb.IntegrityError: except MySQLdb.IntegrityError:
# this ip has already voted for this option # this ip has already voted for this option
return return
def remove_poll_vote(option_id, ip): def remove_poll_vote(option_id, ip_address):
"""Removes a vote from a poll.""" """Removes a vote from a poll."""
_DB.execute( _DB.execute(
"DELETE FROM `poll_votes` " \ "DELETE FROM `poll_votes` " \
"WHERE `option_id` = %s AND `ip_address` = %s", "WHERE `option_id` = %s AND `ip_address` = %s",
(option_id, ip)) (option_id, ip_address))
def get_poll_votes(quest_id): def get_poll_votes(quest_id):
@ -337,3 +337,14 @@ def get_poll_votes(quest_id):
+ "WHERE `quest_data`.`quest_id` = %s", + "WHERE `quest_data`.`quest_id` = %s",
(quest_id,)).fetchall() (quest_id,)).fetchall()
return data 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

View File

@ -316,14 +316,28 @@ def vote(data):
""" """
room = data.get("room") room = data.get("room")
option_id = data.get("option_id") option_id = data.get("option_id")
post_id = data.get("post_id")
polarity = data.get("polarity") polarity = data.get("polarity")
#ip_address = request.remote_addr #ip_address = request.remote_addr
ip_address = request.headers.get("X-Real-Ip") ip_address = request.headers.get("X-Real-Ip")
if polarity:
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) db.insert_poll_vote(option_id, ip_address)
else: else:
db.remove_poll_vote(option_id, ip_address) 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 = {}
data["option_id"] = option_id data["option_id"] = option_id

View File

@ -107,7 +107,7 @@ h3 {
} }
.pollOption { .pollOption {
width: 70%; width: 100%;
margin: 0.1em; margin: 0.1em;
} }
@ -123,7 +123,7 @@ h3 {
} }
.optionVotes { .optionVotes {
width: 10%; width: 4em;
text-align: center; text-align: center;
} }

View File

@ -120,8 +120,9 @@
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds()); date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
return date_str; return date_str;
} }
function pollVote(option_id) { function pollVote(post_id, option_id) {
socket.emit('vote', {option_id: option_id, polarity: document.getElementById('pollInput-' + option_id).checked, room: {{ room_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 }}});
} }
</script> </script>
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
@ -262,7 +263,7 @@
{% for option in options[quest_post[0]] %} {% for option in options[quest_post[0]] %}
<tr id="optionRow-{{ option[0] }}"> <tr id="optionRow-{{ option[0] }}">
<td class="pollCheckBox"> <td class="pollCheckBox">
<input type="checkbox" id="pollInput-{{ option[0] }}" onchange="pollVote({{ option[0] }})"/> <input type="checkbox" {% if ip_address in poll_votes[option[0]] %}checked="true"{% endif %} id="pollInput-{{ option[0] }}" onchange="pollVote({{ quest_post[0] }}, {{ option[0] }})"/>
<label for="pollInput-{{ option[0] }}"></label> <label for="pollInput-{{ option[0] }}"></label>
</td> </td>
<td class="option_text">{{ option[2] }}</td> <td class="option_text">{{ option[2] }}</td>