non-multi-choice polls are enforced
This commit is contained in:
parent
ee28c2950a
commit
742ee5b3e9
23
database.py
23
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
|
||||
|
|
18
events.py
18
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:
|
||||
|
||||
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:
|
||||
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["option_id"] = option_id
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }}});
|
||||
}
|
||||
</script>
|
||||
{% if session.get("user_id") == owner_id %}
|
||||
|
@ -262,7 +263,7 @@
|
|||
{% for option in options[quest_post[0]] %}
|
||||
<tr id="optionRow-{{ option[0] }}">
|
||||
<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>
|
||||
</td>
|
||||
<td class="option_text">{{ option[2] }}</td>
|
||||
|
|
Loading…
Reference in New Issue
Block a user