From 4bdadb5dee4d96a35a040312c16cd56aa2e41703 Mon Sep 17 00:00:00 2001 From: iou1name Date: Mon, 16 Jul 2018 12:56:45 -0400 Subject: [PATCH] refactor quest template database queries --- anonkun.py | 24 ------------------------ database.py | 7 ------- templates/quest.html | 10 +++------- views.py | 17 +++++++++++------ 4 files changed, 14 insertions(+), 44 deletions(-) diff --git a/anonkun.py b/anonkun.py index dfae295..cee82cb 100644 --- a/anonkun.py +++ b/anonkun.py @@ -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): """ diff --git a/database.py b/database.py index 1487942..a81ed57 100644 --- a/database.py +++ b/database.py @@ -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( diff --git a/templates/quest.html b/templates/quest.html index 95e2be2..f1e74f8 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -240,12 +240,10 @@ {% endautoescape %} {% elif quest_post[2] == "dice" %}

{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}

- {% 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]] %}
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 %}
- {% endif %} {% endfor %} {% elif quest_post[2] == "poll" %}

{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}

@@ -253,17 +251,15 @@ - {% for option in options %} - {% if option[1] == quest_post[0] %} + {% for option in options[quest_post[0]] %} {{ option[2] }} - 0 + {{ option[0]|num_votes }} - {% endif %} {% endfor %} {% endif %} diff --git a/views.py b/views.py index e232784..5c9b9da 100644 --- a/views.py +++ b/views.py @@ -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//edit_quest", methods=["GET", "POST"])