Compare commits

...

2 Commits

Author SHA1 Message Date
355ff7aac8 added profile page 2018-06-21 11:00:24 -04:00
124adf5580 made views in url_for relative to current blueprint 2018-06-21 09:06:23 -04:00
9 changed files with 67 additions and 22 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

View File

@ -9,12 +9,12 @@
if (document.getElementById('header').style.display == 'initial') { if (document.getElementById('header').style.display == 'initial') {
document.getElementById('header').style.display = 'none'; document.getElementById('header').style.display = 'none';
document.getElementById('headerHidden').style.display = 'initial'; document.getElementById('headerHidden').style.display = 'initial';
xhr.open('GET', '{{ url_for("views.set_session", hide_header="on") }}', true); xhr.open('GET', '{{ url_for(".set_session", hide_header="on") }}', true);
} }
else { else {
document.getElementById('header').style.display = 'initial'; document.getElementById('header').style.display = 'initial';
document.getElementById('headerHidden').style.display = 'none'; document.getElementById('headerHidden').style.display = 'none';
xhr.open('GET', '{{ url_for("views.set_session", hide_header="off") }}', true); xhr.open('GET', '{{ url_for(".set_session", hide_header="off") }}', true);
} }
xhr.send(); xhr.send();
} }
@ -24,7 +24,7 @@
<body> <body>
<ul id="header" class="header" style="{% if session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif %}"> <ul id="header" class="header" style="{% if session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif %}">
<li><a onclick="toggleHeader();" href="javascript:void(0);">^</a></li> <li><a onclick="toggleHeader();" href="javascript:void(0);">^</a></li>
<li><a href="{{ url_for('views.index') }}">Home</a></li> <li><a href="{{ url_for('.index') }}">Home</a></li>
{% block header %}{% endblock %} {% block header %}{% endblock %}
</ul> </ul>
<ul id="headerHidden" class="header" style="{% if session.get("hide_header") == True %}display:initial;{% else %}display:none;{% endif %}"> <ul id="headerHidden" class="header" style="{% if session.get("hide_header") == True %}display:initial;{% else %}display:none;{% endif %}">

View File

@ -2,7 +2,7 @@
{% block title %}Start a new quest{% endblock %} {% block title %}Start a new quest{% endblock %}
{% block content %} {% block content %}
<h1>New Quest</h1> <h1>New Quest</h1>
<form method="post" action="{{ url_for('views.create_quest') }}"> <form method="post" action="{{ url_for('.create_quest') }}">
<input type="text" placeholder="Quest Title" name="quest_title" maxlength="300" required/><br/> <input type="text" placeholder="Quest Title" name="quest_title" maxlength="300" required/><br/>
<textarea id="create_textarea" name="quest_body"></textarea> <textarea id="create_textarea" name="quest_body"></textarea>
<input type="submit" name="submit" value="Submit"/> <input type="submit" name="submit" value="Submit"/>

View File

@ -2,7 +2,7 @@
{% block title %}Login{% endblock %} {% block title %}Login{% endblock %}
{% block content %} {% block content %}
<h1>Login</h1> <h1>Login</h1>
<form method="post" action="{{ url_for('views.login') }}"> <form method="post" action="{{ url_for('.login') }}">
<input type="text" placeholder="Username" name="user" maxlength="20" required/><br /> <input type="text" placeholder="Username" name="user" maxlength="20" required/><br />
<input type="password" placeholder="Password" name="pass" maxlength="1024" required/><br /> <input type="password" placeholder="Password" name="pass" maxlength="1024" required/><br />
<input type="submit" value="Log in" name="submit"/> <input type="submit" value="Log in" name="submit"/>

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

@ -60,7 +60,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('views.edit_quest') }}">Edit Quest</a></li> <li><a href="{{ url_for('.edit_quest') }}">Edit Quest</a></li>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -13,7 +13,7 @@
<li>Must be between 8 and 1024 characters</li> <li>Must be between 8 and 1024 characters</li>
</ul> </ul>
</div> </div>
<form method="post" action="{{ url_for('views.signup') }}"> <form method="post" action="{{ url_for('.signup') }}">
<input type="text" placeholder="Username" name="user" maxlength="20" required/><br /> <input type="text" placeholder="Username" name="user" maxlength="20" required/><br />
<input type="password" placeholder="Password" name="pass" maxlength="1024" required/><br /> <input type="password" placeholder="Password" name="pass" maxlength="1024" required/><br />
<input type="password" placeholder="Verify password" name="pass_verify" maxlength="1024" required/><br /> <input type="password" placeholder="Verify password" name="pass_verify" maxlength="1024" required/><br />

View File

@ -71,8 +71,24 @@ 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("views.login") @login_required(".login")
def create_quest(): def create_quest():
""" """
Starts a new quest. Starts a new quest.
@ -92,7 +108,7 @@ def create_quest():
quest_id = db.insert_quest(canon_title, ident_title, owner_id) quest_id = db.insert_quest(canon_title, ident_title, owner_id)
db.insert_quest_post(quest_id, quest_body, timestamp) db.insert_quest_post(quest_id, quest_body, timestamp)
return redirect(url_for('views.quest', quest_title=ident_title)) return redirect(url_for('.quest', quest_title=ident_title))
@views.route("/edit_quest") @views.route("/edit_quest")
@ -129,7 +145,7 @@ def login():
if db.verify_password(username, password): if db.verify_password(username, password):
session["username"] = username session["username"] = username
return redirect(url_for("views.index")) return redirect(url_for(".index"))
else: else:
abort(401) abort(401)
@ -147,25 +163,26 @@ 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())
return redirect(url_for("views.index")) res = db.add_user(username, password, timestamp)
return redirect(url_for(".index"))
@views.route("/") @views.route("/")