added profile page

This commit is contained in:
iou1name 2018-06-21 11:00:24 -04:00
parent 124adf5580
commit 355ff7aac8
4 changed files with 56 additions and 11 deletions

View File

@ -2,6 +2,7 @@ CREATE TABLE `users` (
`user_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL, `username` VARCHAR(20) NOT NULL,
`password_hash` CHAR(73) NOT NULL, `password_hash` CHAR(73) NOT NULL,
`signup_date` INT UNSIGNED NOT NULL,
PRIMARY KEY (`user_id`) PRIMARY KEY (`user_id`)
) ENGINE=InnoDB CHARSET utf8mb4; ) ENGINE=InnoDB CHARSET utf8mb4;

View File

@ -41,7 +41,7 @@ class Database():
_DB = Database() _DB = Database()
def add_user(username, password): def add_user(username, password, timestamp):
""" """
Adds a user to the database. Adds a user to the database.
""" """
@ -52,8 +52,8 @@ def add_user(username, password):
pw_hash = argon2.hash(password) pw_hash = argon2.hash(password)
_DB.execute( _DB.execute(
"INSERT INTO `users` (`username`, `password_hash`) VALUES (%s, %s)", "INSERT INTO `users` (`username`, `password_hash`, `signup_date`) " \
(username, pw_hash)) + "VALUES (%s, %s, %s)", (username, pw_hash, timestamp))
return "success" return "success"
@ -151,3 +151,23 @@ def get_quest_data(quest_id):
data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s", data = _DB.execute("SELECT * FROM `quest_data` WHERE `quest_id` = %s",
(quest_id,)).fetchall() (quest_id,)).fetchall()
return data return data
def get_user_info(username):
"""
Retrives relevant user data.
"""
data = _DB.execute(
"SELECT `user_id`, `signup_date` FROM `users` WHERE `username` = %s",
(username,)).fetchone()
return data
def get_user_quests(user_id):
"""
Retrieves all quests ran by a particular user_id.
"""
data = _DB.execute(
"SELECT * FROM `quest_meta` WHERE `owner_id` = %s",
(user_id,)).fetchall()
return data

7
templates/profile.html Normal file
View File

@ -0,0 +1,7 @@
{% extends "base.html" %}
{% block title %}{{ username }}{% endblock %}
{% block content %}
<h1>{{ username }}'s profile</h1>
Signed up: {{ signup_date | strftime }}<br />
Num. quests ran: {{ num_quests }}<br />
{% endblock %}

View File

@ -71,6 +71,22 @@ def quest(quest_title):
messages=messages) messages=messages)
@views.route("/profile/<path:username>")
def profile(username):
"""
Shows the profile page of the specified user.
"""
data = db.get_user_info(username)
if not data:
return "user_not_found"
user_id, signup_date = data
quests = db.get_user_quests(user_id)
return render_template("profile.html",
username=username,
signup_date=signup_date,
num_quests=len(quests))
@views.route("/create_quest", methods=["GET", "POST"]) @views.route("/create_quest", methods=["GET", "POST"])
@login_required(".login") @login_required(".login")
def create_quest(): def create_quest():
@ -147,24 +163,25 @@ def signup():
password_verify = request.form.get("pass_verify") password_verify = request.form.get("pass_verify")
if len(username) > 20: if len(username) > 20:
"username_too_long" return "username_too_long"
elif len(username) < 3: elif len(username) < 3:
"username_too_short" return "username_too_short"
chrs = [c not in string.ascii_letters + string.digits for c in username] chrs = [c not in string.ascii_letters + string.digits for c in username]
if any(chrs): if any(chrs):
"username_bad_chars" return "username_bad_chars"
if db.verify_username(username): if db.verify_username(username):
"username_taken" return "username_taken"
if len(password) > 1024: if len(password) > 1024:
"password_too_long" return "password_too_long"
elif len(password) < 8: elif len(password) < 8:
"password_too_short" return "password_too_short"
if password != password_verify: if password != password_verify:
"passwords_dont_match" return "passwords_dont_match"
res = db.add_user(username, password) timestamp = int(time.time())
res = db.add_user(username, password, timestamp)
return redirect(url_for(".index")) return redirect(url_for(".index"))