refactor quest template database queries

This commit is contained in:
iou1name 2018-07-16 12:56:45 -04:00
parent 75fdbd6bd7
commit 4bdadb5dee
4 changed files with 14 additions and 44 deletions

View File

@ -107,30 +107,6 @@ def num_votes(option_id):
return db.get_num_votes(option_id)
@app.template_filter("split_options")
def split_options(options):
"""
Splits a polls options into a list.
"""
return options.split("\n")
@app.template_filter("get_rolls")
def get_rolls(post_id):
"""
Gets the dice rolls for the template.
"""
return db.get_dice_rolls(post_id=post_id)
@app.template_filter("get_options")
def get_options(post_id):
"""
Gets the poll options for the template.
"""
return db.get_poll_options(post_id=post_id)
@app.after_request
def minify(res):
"""

View File

@ -288,13 +288,6 @@ def get_polls(quest_id):
return data
def get_poll(post_id):
"""Gets poll information."""
data = _DB.execute(
"SELECT * FROM `polls` WHERE `post_id` = %s", (post_id,)).fetchall()
return data
def insert_poll_option(post_id, option_text):
"""Insert a new poll option. ips_voted will be NULL to start."""
_DB.execute(

View File

@ -240,12 +240,10 @@
{% endautoescape %}
{% elif quest_post[2] == "dice" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% for dice_roll in quest_post[0]|get_rolls %}
{% if dice_roll[2] == quest_post[0] %}
{% for dice_roll in dice_rolls[quest_post[0]] %}
<div id="questRollId-{{ dice_roll[0] }}">
<b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if dice_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}</b>
</div>
{% endif %}
{% endfor %}
{% elif quest_post[2] == "poll" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
@ -253,17 +251,15 @@
<col{% if quest_post[0] != open_post_id %} style="visibility: collapse;"{% endif %}/>
<col/>
<col/>
{% for option in options %}
{% if option[1] == quest_post[0] %}
{% for option in options[quest_post[0]] %}
<tr>
<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">0</td>
<td class="optionVotes">{{ option[0]|num_votes }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endif %}

View File

@ -47,7 +47,6 @@ def quest(quest_title):
"""
An arbituary quest page.
"""
then = time.time()
ident_title, _, extra = quest_title.partition("/")
data = db.get_quest_meta(ident_title=ident_title)
if not data:
@ -59,13 +58,19 @@ def quest(quest_title):
open_post_id = data[4]
quest_posts = db.get_quest_data(quest_id)
dice_rolls = db.get_dice_rolls(quest_id)
options = db.get_poll_options(quest_id=quest_id)
dice_rolls_raw = db.get_dice_rolls(quest_id)
dice_rolls = {roll[2]: [] for roll in dice_rolls_raw}
_ = [dice_rolls[roll[2]].append(roll) for roll in dice_rolls_raw]
del dice_rolls_raw
options_raw = db.get_poll_options(quest_id=quest_id)
options = {opt[1]: [] for opt in options_raw}
_ = [options[opt[1]].append(opt) for opt in options_raw]
del options_raw
messages = db.get_chat_messages(quest_id)
temp = render_template('quest.html', **locals())
print(time.time() - then)
return temp
return render_template('quest.html', **locals())
@views.route("/quest/<path:quest_title>/edit_quest", methods=["GET", "POST"])