From d77f25483103a196e3bbae98e9452a1b1c786a20 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 26 Jul 2018 11:38:02 -0400 Subject: [PATCH] quest pages work more better --- anonkun.sql | 10 +++++++++- database.py | 23 ++++++++++++++++++++--- templates/quest.html | 8 ++++---- views.py | 5 ++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/anonkun.sql b/anonkun.sql index 4e96a1b..867634d 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -24,6 +24,13 @@ CREATE TABLE `quest_meta` ( FOREIGN KEY (`owner_id`) REFERENCES `users`(`user_id`) ) ENGINE=InnoDB CHARSET utf8mb4; +CREATE TABLE `page_titles` ( + `quest_id` SMALLINT UNSIGNED NOT NULL, + `page_num` TINYINT UNSIGNED NOT NULL, + `page_title` VARCHAR(200) NOT NULL, + FOREIGN KEY (`quest_id`) REFERENCES `quest_meta`(`quest_id`) +) ENGINE=InnoDB CHARSET utf8mb4; + CREATE TABLE `quest_data` ( `post_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `quest_id` SMALLINT UNSIGNED NOT NULL, @@ -32,7 +39,8 @@ CREATE TABLE `quest_data` ( `post` MEDIUMTEXT NOT NULL, `timestamp` INT UNSIGNED NOT NULL, PRIMARY KEY (`post_id`), - FOREIGN KEY (`quest_id`) REFERENCES `quest_meta`(`quest_id`) + FOREIGN KEY (`quest_id`) REFERENCES `quest_meta`(`quest_id`), + FOREIGN KEY (`page_num`) REFERENCES `page_titles`(`page_num`) ) ENGINE=InnoDB CHARSET utf8mb4; CREATE TABLE `dice_calls` ( diff --git a/database.py b/database.py index cf3c356..e16ea3d 100644 --- a/database.py +++ b/database.py @@ -154,12 +154,13 @@ def get_quest_meta(quest_id): return data -def get_quest_data(quest_id): +def get_quest_data(quest_id, page_num): """Retrieves all quest posts.""" data = _DB.execute( - "SELECT * FROM `quest_data` WHERE `quest_id` = %s " \ + "SELECT * FROM `quest_data` " \ + + "WHERE `quest_id` = %s AND `page_num` = %s " \ + "ORDER BY `post_id` ASC", - (quest_id,)).fetchall() + (quest_id, page_num)).fetchall() return data @@ -345,3 +346,19 @@ def get_poll_votes_voted(post_id, ip_address): + "AND `poll_votes`.`ip_address` = %s", (post_id, ip_address)).fetchall() return data + + +def insert_quest_page(quest_id, page_num, page_title): + """Inserts a new quest page.""" + _DB.execute( + "INSERT INTO `page_titles` (`quest_id`, `page_num`, `page_title`) " \ + + "VALUES (%s, %s, %s)", + (quest_id, page_num, page_title)) + + +def get_quest_pages(quest_id): + """Gets all numbers and titles for a quest.""" + data = _DB.execute( + "SELECT * FROM `page_titles` WHERE `quest_id` = %s", + (quest_id,)).fetchall() + return data diff --git a/templates/quest.html b/templates/quest.html index f453ee1..f401c2f 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -12,11 +12,11 @@ {% block header %} {% if session.get("user_id") == owner_id %}
  • Edit Quest
  • -
  • Page 1
  • {% endif %} @@ -161,5 +161,5 @@
    -
    + {% endblock %} diff --git a/views.py b/views.py index da88d97..24e21d6 100644 --- a/views.py +++ b/views.py @@ -58,7 +58,9 @@ def quest(quest_id, page_num=1): open_post_id = data[2] ip_address = request.remote_addr - quest_posts = db.get_quest_data(quest_id) + pages = db.get_quest_pages(quest_id) + + quest_posts = db.get_quest_data(quest_id, page_num) 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] @@ -131,6 +133,7 @@ def create_quest(): page_num = 1 quest_id = db.insert_quest(quest_title, owner_id) + db.insert_quest_page(quest_id, page_num, "Page 1") db.insert_quest_post(quest_id, page_num, "text", quest_body, timestamp) return redirect(url_for('.quest', quest_id=quest_id))