From 3b4d24bd7c72079979f587f151f8ec6827da932b Mon Sep 17 00:00:00 2001 From: iou1name Date: Mon, 23 Jul 2018 20:19:43 -0400 Subject: [PATCH] changed quest urls to use quest_id instead of ident_title --- anonkun.py | 1 + anonkun.sql | 3 +-- database.py | 24 +++++++++--------------- templates/edit_quest.html | 2 +- templates/index.html | 2 +- templates/quest.html | 2 +- tools.py | 11 ----------- views.py | 25 +++++++++++-------------- 8 files changed, 25 insertions(+), 45 deletions(-) diff --git a/anonkun.py b/anonkun.py index e60eeb5..411acd7 100644 --- a/anonkun.py +++ b/anonkun.py @@ -57,6 +57,7 @@ app.wsgi_app = ProxyFix(app.wsgi_app) app.register_blueprint(views) app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 app.config['SESSION_TYPE'] = 'filesystem' +app.url_map.strict_slashes = False app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True #app.jinja_env.undefined = "StrictUndefined" diff --git a/anonkun.sql b/anonkun.sql index d39e8cf..d109125 100644 --- a/anonkun.sql +++ b/anonkun.sql @@ -17,8 +17,7 @@ CREATE TABLE `chat_messages` ( CREATE TABLE `quest_meta` ( `quest_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, - `canon_title` VARCHAR(300) DEFAULT NULL, - `ident_title` VARCHAR(300) DEFAULT NULL, + `quest_title` VARCHAR(300) DEFAULT NULL, `owner_id` SMALLINT UNSIGNED DEFAULT NULL, `open_post_id` SMALLINT UNSIGNED DEFAULT NULL, PRIMARY KEY (`quest_id`), diff --git a/database.py b/database.py index 7d142c0..9952d1c 100644 --- a/database.py +++ b/database.py @@ -117,16 +117,16 @@ def get_chat_messages(room_id): return res -def insert_quest(canon_title, ident_title, owner_id): +def insert_quest(canon_title, owner_id): """Creates a new quest entry.""" _DB.execute( - "INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\ - + "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id)) + "INSERT INTO `quest_meta` (`quest_title`, `owner_id`) "\ + + "VALUES (%s, %s)", (canon_title, owner_id)) quest_id = _DB.execute( - "SELECT `quest_id` FROM `quest_meta` WHERE `ident_title` = %s" \ + "SELECT `quest_id` FROM `quest_meta` WHERE `owner_id` = %s" \ + "ORDER BY `quest_id` DESC", - (ident_title,)).fetchone()[0] + (owner_id,)).fetchone()[0] return quest_id @@ -142,20 +142,14 @@ def insert_quest_post(quest_id, post_type, post, timestamp): return post_id -def get_quest_meta(quest_id=None, ident_title=None): +def get_quest_meta(quest_id): """ Retrieves all meta info about a quest. Allows searching by either quest_id or ident_title. """ - statement = "SELECT * FROM `quest_meta` WHERE " - if quest_id: - statement += "`quest_id` = %s" - data = _DB.execute(statement, (quest_id,)).fetchone() - elif ident_title: - statement += "`ident_title` = %s" - data = _DB.execute(statement, (ident_title,)).fetchone() - else: - return + data = _DB.execute( + "SELECT * FROM `quest_meta` WHERE `quest_id` = %s", + (quest_id,)).fetchone() return data diff --git a/templates/edit_quest.html b/templates/edit_quest.html index 83bb9ee..0a4774b 100644 --- a/templates/edit_quest.html +++ b/templates/edit_quest.html @@ -2,7 +2,7 @@ {% block title %}Edit {{ quest_title }}{% endblock %} {% block content %}

{{ quest_title }}

-
+ diff --git a/templates/index.html b/templates/index.html index c5713a9..301e37a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ {% block title %}Index{% endblock %} {% block content %}

Quests 'n Shiet

- Unga Bunga Quest
+ Unga Bunga Quest
Create New Quest
Sign up
Login
diff --git a/templates/quest.html b/templates/quest.html index bc74bb0..90bcd67 100644 --- a/templates/quest.html +++ b/templates/quest.html @@ -11,7 +11,7 @@ {% endblock %} {% block header %} {% if session.get("user_id") == owner_id %} -
  • Edit Quest
  • +
  • Edit Quest
  • {% endif %} {% endblock %} {% block content %} diff --git a/tools.py b/tools.py index 8c3a9a7..937c771 100644 --- a/tools.py +++ b/tools.py @@ -4,7 +4,6 @@ Miscellaneous tools and help functions. """ import os import re -import urllib import hashlib import magic @@ -12,16 +11,6 @@ import requests IMG_DIR = "/usr/local/www/html/img" -def sanitize_title(canon_title): - """ - Sanitizes the given canonical title for a quest and returns a - url-friendly version. - """ - ident_title = canon_title.lower().replace(" ", "-") - ident_title = urllib.parse.quote(ident_title) - return ident_title - - def handle_img(post): """ Handles [img] tags within a quest post appropriately. diff --git a/views.py b/views.py index 07efc80..8231066 100644 --- a/views.py +++ b/views.py @@ -42,13 +42,12 @@ def login_required(url=None): return actual_decorator -@views.route("/quest/") -def quest(quest_title): +@views.route("/quest/", strict_slashes=False) +def quest(quest_id, page_num=1): """ An arbituary quest page. """ - ident_title, _, extra = quest_title.partition("/") - data = db.get_quest_meta(ident_title=ident_title) + data = db.get_quest_meta(quest_id) if not data: abort(404) @@ -79,13 +78,12 @@ def quest(quest_title): return render_template('quest.html', **locals()) -@views.route("/quest//edit_quest", methods=["GET", "POST"]) -def edit_quest(quest_title): +@views.route("/quest//edit_quest", methods=["GET", "POST"]) +def edit_quest(quest_id): """ Allows the quest owner to edit the quest. """ - ident_title = quest_title - data = db.get_quest_meta(ident_title=ident_title) + data = db.get_quest_meta(quest_id) if not data: abort(404) quest_id = data[0] @@ -93,8 +91,8 @@ def edit_quest(quest_title): if request.method == "GET": return render_template("edit_quest.html", quest_title=quest_title, - ident_title=ident_title) - return redirect(url_for(".quest", quest_title=ident_title)) + quest_id=quest_id) + return redirect(url_for(".quest", quest_id=quest_id)) @views.route("/profile/") @@ -121,20 +119,19 @@ def create_quest(): """ if request.method == "GET": return render_template("create_quest.html") - canon_title = request.form.get("quest_title") + quest_title = request.form.get("quest_title") quest_body = request.form.get("quest_body") - ident_title = tools.sanitize_title(canon_title) quest_body = bleach.clean(quest_body.strip()) quest_body = quest_body.replace("\n", "
    ") owner_id = session.get("user_id") timestamp = int(time.time()) - quest_id = db.insert_quest(canon_title, ident_title, owner_id) + quest_id = db.insert_quest(quest_title, owner_id) db.insert_quest_post(quest_id, "text", quest_body, timestamp) - return redirect(url_for('.quest', quest_title=ident_title)) + return redirect(url_for('.quest', quest_id=quest_id)) @views.route("/set_session")
    Quest Title: