refactor poll_votes database queries
This commit is contained in:
parent
4bdadb5dee
commit
ee28c2950a
10
anonkun.py
10
anonkun.py
|
@ -59,7 +59,7 @@ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
|||
app.config['SESSION_TYPE'] = 'filesystem'
|
||||
app.jinja_env.trim_blocks = True
|
||||
app.jinja_env.lstrip_blocks = True
|
||||
app.jinja_env.undefined = "StrictUndefined"
|
||||
#app.jinja_env.undefined = "StrictUndefined"
|
||||
Session(app)
|
||||
socketio.init_app(app)
|
||||
paranoid = Paranoid(app)
|
||||
|
@ -99,14 +99,6 @@ def get_dice_challenge(post_id):
|
|||
return db.get_dice_call(post_id)[3]
|
||||
|
||||
|
||||
@app.template_filter("num_votes")
|
||||
def num_votes(option_id):
|
||||
"""
|
||||
Returns the number of IPs who voted for this option.
|
||||
"""
|
||||
return db.get_num_votes(option_id)
|
||||
|
||||
|
||||
@app.after_request
|
||||
def minify(res):
|
||||
"""
|
||||
|
|
|
@ -75,5 +75,6 @@ CREATE TABLE `poll_options` (
|
|||
CREATE TABLE `poll_votes` (
|
||||
`option_id` MEDIUMINT UNSIGNED NOT NULL,
|
||||
`ip_address` VARCHAR(32) NOT NULL,
|
||||
FOREIGN KEY (`option_id`) REFERENCES `poll_options`(`option_id`)
|
||||
FOREIGN KEY (`option_id`) REFERENCES `poll_options`(`option_id`),
|
||||
UNIQUE (`option_id`, `ip_address`)
|
||||
) ENGINE=InnoDB CHARSET utf8mb4;
|
||||
|
|
49
database.py
49
database.py
|
@ -296,39 +296,44 @@ def insert_poll_option(post_id, option_text):
|
|||
(post_id, option_text))
|
||||
|
||||
|
||||
def get_poll_options(quest_id=None, post_id=None):
|
||||
def get_poll_options(quest_id):
|
||||
"""Gets all relevent poll options for a given quest or post."""
|
||||
sql = "SELECT * FROM `poll_options` WHERE "
|
||||
if quest_id:
|
||||
sql += "`quest_id` = %s"
|
||||
ins = (quest_id,)
|
||||
elif post_id:
|
||||
sql += "`post_id` = %s"
|
||||
ins = (post_id,)
|
||||
else:
|
||||
return
|
||||
data = _DB.execute(sql, ins).fetchall()
|
||||
data = _DB.execute(
|
||||
"SELECT DISTINCT " \
|
||||
+ "`poll_options`.* FROM `poll_options`, `quest_data` " \
|
||||
+ "WHERE `quest_data`.`post_id` = `poll_options`.`post_id` " \
|
||||
+ "AND `quest_data`.`quest_id` = %s",
|
||||
(quest_id,)).fetchall()
|
||||
return data
|
||||
|
||||
|
||||
def insert_poll_vote(option_id, ip):
|
||||
"""Inserts a new vote for a poll option."""
|
||||
res = _DB.execute(
|
||||
"INSERT ")
|
||||
try:
|
||||
_DB.execute(
|
||||
"INSERT INTO `poll_votes` " \
|
||||
"(`option_id`, `ip_address`) VALUES (%s, %s)",
|
||||
(option_id, ip))
|
||||
except MySQLdb.IntegrityError:
|
||||
# this ip has already voted for this option
|
||||
return
|
||||
|
||||
|
||||
def insert_poll_vote(option_id, ip):
|
||||
def remove_poll_vote(option_id, ip):
|
||||
"""Removes a vote from a poll."""
|
||||
pass
|
||||
_DB.execute(
|
||||
"DELETE FROM `poll_votes` " \
|
||||
"WHERE `option_id` = %s AND `ip_address` = %s",
|
||||
(option_id, ip))
|
||||
|
||||
|
||||
def get_poll_votes(quest_id):
|
||||
pass
|
||||
|
||||
|
||||
def get_num_votes(option_id):
|
||||
"""Returns the number of votes for an option."""
|
||||
"""Gets all poll votes for the given quest_id."""
|
||||
data = _DB.execute(
|
||||
"SELECT COUNT(`ip_address`) FROM `poll_votes` WHERE `option_id` = %s",
|
||||
(option_id,)).fetchone()[0]
|
||||
"SELECT `poll_votes`.* FROM `poll_votes` LEFT JOIN `poll_options` " \
|
||||
+ "ON `poll_votes`.`option_id` = `poll_options`.`option_id` " \
|
||||
+ "LEFT JOIN `quest_data` " \
|
||||
+ "ON `poll_options`.`post_id`=`quest_data`.`post_id` " \
|
||||
+ "WHERE `quest_data`.`quest_id` = %s",
|
||||
(quest_id,)).fetchall()
|
||||
return data
|
||||
|
|
13
events.py
13
events.py
|
@ -314,7 +314,18 @@ def vote(data):
|
|||
"""
|
||||
Called when a user changes their vote on a poll.
|
||||
"""
|
||||
print(data)
|
||||
room = data.get("room")
|
||||
option_id = data.get("option_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:
|
||||
db.remove_poll_vote(option_id, ip_address)
|
||||
|
||||
data = {}
|
||||
data["option_id"] = option_id
|
||||
data["polarity"] = polarity
|
||||
emit("vote", data, room=room)
|
||||
|
|
|
@ -90,6 +90,14 @@
|
|||
table.getElementsByTagName("col")[0].style.visibility = '';
|
||||
}
|
||||
});
|
||||
socket.on('vote', function(data) {
|
||||
let row = document.getElementById('optionRow-' + data.option_id);
|
||||
if (data.polarity) {
|
||||
row.cells[2].textContent = Number(row.cells[2].textContent) + 1;
|
||||
} else {
|
||||
row.cells[2].textContent = Number(row.cells[2].textContent) - 1;
|
||||
}
|
||||
});
|
||||
let mtarea = document.getElementById('messageTextArea');
|
||||
mtarea.addEventListener('keypress', function(event) {
|
||||
if (event.key == 'Enter' && !event.shiftKey) {
|
||||
|
@ -252,13 +260,13 @@
|
|||
<col/>
|
||||
<col/>
|
||||
{% for option in options[quest_post[0]] %}
|
||||
<tr>
|
||||
<tr id="optionRow-{{ option[0] }}">
|
||||
<td class="pollCheckBox">
|
||||
<input type="checkbox" id="pollInput-{{ option[0] }}" onchange="pollVote({{ option[0] }})"/>
|
||||
<label for="pollInput-{{ option[0] }}"></label>
|
||||
</td>
|
||||
<td class="option_text">{{ option[2] }}</td>
|
||||
<td class="optionVotes">{{ option[0]|num_votes }}</td>
|
||||
<td class="optionVotes">{{ poll_votes[option[0]]|length }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
6
views.py
6
views.py
|
@ -56,6 +56,7 @@ def quest(quest_title):
|
|||
quest_title = data[1]
|
||||
owner_id = data[3]
|
||||
open_post_id = data[4]
|
||||
ip_address = request.remote_addr
|
||||
|
||||
quest_posts = db.get_quest_data(quest_id)
|
||||
dice_rolls_raw = db.get_dice_rolls(quest_id)
|
||||
|
@ -68,6 +69,11 @@ def quest(quest_title):
|
|||
_ = [options[opt[1]].append(opt) for opt in options_raw]
|
||||
del options_raw
|
||||
|
||||
poll_votes_raw = db.get_poll_votes(quest_id)
|
||||
poll_votes = {vote[0]: [] for vote in poll_votes_raw}
|
||||
_ = [poll_votes[vote[0]].append(vote[1]) for vote in poll_votes_raw]
|
||||
del poll_votes_raw
|
||||
|
||||
messages = db.get_chat_messages(quest_id)
|
||||
|
||||
return render_template('quest.html', **locals())
|
||||
|
|
Loading…
Reference in New Issue
Block a user