changed quest urls to use quest_id instead of ident_title

This commit is contained in:
iou1name 2018-07-23 20:19:43 -04:00
parent c59f2dde99
commit 3b4d24bd7c
8 changed files with 25 additions and 45 deletions

View File

@ -57,6 +57,7 @@ app.wsgi_app = ProxyFix(app.wsgi_app)
app.register_blueprint(views) app.register_blueprint(views)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['SESSION_TYPE'] = 'filesystem' app.config['SESSION_TYPE'] = 'filesystem'
app.url_map.strict_slashes = False
app.jinja_env.trim_blocks = True app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True app.jinja_env.lstrip_blocks = True
#app.jinja_env.undefined = "StrictUndefined" #app.jinja_env.undefined = "StrictUndefined"

View File

@ -17,8 +17,7 @@ CREATE TABLE `chat_messages` (
CREATE TABLE `quest_meta` ( CREATE TABLE `quest_meta` (
`quest_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `quest_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`canon_title` VARCHAR(300) DEFAULT NULL, `quest_title` VARCHAR(300) DEFAULT NULL,
`ident_title` VARCHAR(300) DEFAULT NULL,
`owner_id` SMALLINT UNSIGNED DEFAULT NULL, `owner_id` SMALLINT UNSIGNED DEFAULT NULL,
`open_post_id` SMALLINT UNSIGNED DEFAULT NULL, `open_post_id` SMALLINT UNSIGNED DEFAULT NULL,
PRIMARY KEY (`quest_id`), PRIMARY KEY (`quest_id`),

View File

@ -117,16 +117,16 @@ def get_chat_messages(room_id):
return res return res
def insert_quest(canon_title, ident_title, owner_id): def insert_quest(canon_title, owner_id):
"""Creates a new quest entry.""" """Creates a new quest entry."""
_DB.execute( _DB.execute(
"INSERT INTO `quest_meta` (`canon_title`, `ident_title`, `owner_id`) "\ "INSERT INTO `quest_meta` (`quest_title`, `owner_id`) "\
+ "VALUES (%s, %s, %s)", (canon_title, ident_title, owner_id)) + "VALUES (%s, %s)", (canon_title, owner_id))
quest_id = _DB.execute( 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", + "ORDER BY `quest_id` DESC",
(ident_title,)).fetchone()[0] (owner_id,)).fetchone()[0]
return quest_id return quest_id
@ -142,20 +142,14 @@ def insert_quest_post(quest_id, post_type, post, timestamp):
return post_id 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 Retrieves all meta info about a quest. Allows searching by either
quest_id or ident_title. quest_id or ident_title.
""" """
statement = "SELECT * FROM `quest_meta` WHERE " data = _DB.execute(
if quest_id: "SELECT * FROM `quest_meta` WHERE `quest_id` = %s",
statement += "`quest_id` = %s" (quest_id,)).fetchone()
data = _DB.execute(statement, (quest_id,)).fetchone()
elif ident_title:
statement += "`ident_title` = %s"
data = _DB.execute(statement, (ident_title,)).fetchone()
else:
return
return data return data

View File

@ -2,7 +2,7 @@
{% block title %}Edit {{ quest_title }}{% endblock %} {% block title %}Edit {{ quest_title }}{% endblock %}
{% block content %} {% block content %}
<center><h1>{{ quest_title }}</h1></center> <center><h1>{{ quest_title }}</h1></center>
<form method="post" action="{{ url_for('.quest', quest_title=ident_title + '/edit_quest') }}"> <form method="post" action="{{ url_for('.edit_quest', quest_id=quest_id,) }}">
<table> <table>
<tr> <tr>
<td>Quest Title:</td> <td>Quest Title:</td>

View File

@ -2,7 +2,7 @@
{% block title %}Index{% endblock %} {% block title %}Index{% endblock %}
{% block content %} {% block content %}
<h1>Quests 'n Shiet</h1> <h1>Quests 'n Shiet</h1>
<a href="./quest/unga-bunga-quest">Unga Bunga Quest</a><br /> <a href="./quest/1">Unga Bunga Quest</a><br />
<a href="./create_quest">Create New Quest</a><br /> <a href="./create_quest">Create New Quest</a><br />
<a href="./signup">Sign up</a><br /> <a href="./signup">Sign up</a><br />
<a href="./login">Login</a><br /> <a href="./login">Login</a><br />

View File

@ -11,7 +11,7 @@
{% endblock %} {% endblock %}
{% block header %} {% block header %}
{% if session.get("user_id") == owner_id %} {% if session.get("user_id") == owner_id %}
<li><a href="{{ url_for('.quest', quest_title=ident_title + '/edit_quest') }}">Edit Quest</a></li> <li><a href="{{ url_for('.edit_quest', quest_id=quest_id) }}">Edit Quest</a></li>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -4,7 +4,6 @@ Miscellaneous tools and help functions.
""" """
import os import os
import re import re
import urllib
import hashlib import hashlib
import magic import magic
@ -12,16 +11,6 @@ import requests
IMG_DIR = "/usr/local/www/html/img" 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): def handle_img(post):
""" """
Handles [img] tags within a quest post appropriately. Handles [img] tags within a quest post appropriately.

View File

@ -42,13 +42,12 @@ def login_required(url=None):
return actual_decorator return actual_decorator
@views.route("/quest/<path:quest_title>") @views.route("/quest/<int:quest_id>", strict_slashes=False)
def quest(quest_title): def quest(quest_id, page_num=1):
""" """
An arbituary quest page. An arbituary quest page.
""" """
ident_title, _, extra = quest_title.partition("/") data = db.get_quest_meta(quest_id)
data = db.get_quest_meta(ident_title=ident_title)
if not data: if not data:
abort(404) abort(404)
@ -79,13 +78,12 @@ def quest(quest_title):
return render_template('quest.html', **locals()) return render_template('quest.html', **locals())
@views.route("/quest/<path:quest_title>/edit_quest", methods=["GET", "POST"]) @views.route("/quest/<int:quest_id>/edit_quest", methods=["GET", "POST"])
def edit_quest(quest_title): def edit_quest(quest_id):
""" """
Allows the quest owner to edit the quest. Allows the quest owner to edit the quest.
""" """
ident_title = quest_title data = db.get_quest_meta(quest_id)
data = db.get_quest_meta(ident_title=ident_title)
if not data: if not data:
abort(404) abort(404)
quest_id = data[0] quest_id = data[0]
@ -93,8 +91,8 @@ def edit_quest(quest_title):
if request.method == "GET": if request.method == "GET":
return render_template("edit_quest.html", return render_template("edit_quest.html",
quest_title=quest_title, quest_title=quest_title,
ident_title=ident_title) quest_id=quest_id)
return redirect(url_for(".quest", quest_title=ident_title)) return redirect(url_for(".quest", quest_id=quest_id))
@views.route("/profile/<path:username>") @views.route("/profile/<path:username>")
@ -121,20 +119,19 @@ def create_quest():
""" """
if request.method == "GET": if request.method == "GET":
return render_template("create_quest.html") 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") quest_body = request.form.get("quest_body")
ident_title = tools.sanitize_title(canon_title)
quest_body = bleach.clean(quest_body.strip()) quest_body = bleach.clean(quest_body.strip())
quest_body = quest_body.replace("\n", "<br />") quest_body = quest_body.replace("\n", "<br />")
owner_id = session.get("user_id") owner_id = session.get("user_id")
timestamp = int(time.time()) 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) 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") @views.route("/set_session")