diff --git a/flask/app.py b/flask/app.py index eb9a2ab..445d364 100644 --- a/flask/app.py +++ b/flask/app.py @@ -36,11 +36,44 @@ def homepage(): return render_template("home.html", title="Homepage",userdata=userdata,login=logged_in,archives=archives) @app.route('/user') -@app.route('/user/') -def userpage(userid:int=0): +@app.route('/user/', methods=['GET','POST']) +def userpage(page_userid:int=0): logged_in,userdata=get_login_info(request.cookies.get('session')) if not logged_in: return make_response(redirect('/')) + if page_userid == 0: + return make_response(redirect(f"/user/{userdata[0]}")) + page_userdata=db.get_user_info(page_userid) + + # POST: Update display name or password + if request.method == 'POST': + match request.form['edit-type']: + case "password": + old_passhash=sha256(request.form['old-pass'].encode()).hexdigest() + if not old_passhash == db.get_passhash(userdata[1])[2]: + return errorpage("The old password does not match!") + new_password=request.form['new-pass'] + conf_password=request.form['conf-pass'] + if not new_password == conf_password: + return errorpage("The new passwords do not match!") + res,data=db.update_user_info(userdata[0],"PASSHASH",sha256(new_password.encode()).hexdigest().upper()) + case "dname": + res,data=db.update_user_info(userdata[0],"DNAME",request.form['display-name']) + case _: + return make_response(redirect(f"/user/{page_userid}")) + if not res: + return errorpage("Something went wrong: " + data) + return make_response(redirect('/')) + + # GET: return normal info page + return render_template("user.html", title="User Details",userdata=page_userdata,login_userid=userdata[0],userid=page_userid) + +@app.route('/user/') +def user_redirect(uname:str): + userdata=db.get_user_info_from_uname(uname) + if not userdata: + return make_response(redirect(f"/user")) + return make_response(redirect(f"/user/{userdata[0]}")) @app.route('/add', methods=['GET','POST']) def addpage(): diff --git a/flask/func.py b/flask/func.py index 0a89a87..2dcca7d 100644 --- a/flask/func.py +++ b/flask/func.py @@ -73,7 +73,7 @@ class db: );""") ## Gets the passhash from a specific user - ## OUTPUT: (If user exists) int=200, passhash:str + ## OUTPUT: (If user exists) int=200, ID:int, passhash:str ## (If user does not exist) int=400, Exception:str def get_passhash(self, username:str): self.cur.execute(f"SELECT ID,PASSHASH FROM Users WHERE UNAME='{username}'") @@ -107,6 +107,19 @@ class db: self.cur.execute(f"SELECT * FROM Users WHERE ID='{userid}'") return self.cur.fetchone() + ## like above, just with uname + ## OUTPUT: tuple=(ID:int,UNAME:str,DNAME:str,CREATED:int,STATE:text,PASSHASH:text) + def get_user_info_from_uname(self, uname:str): + self.cur.execute(f"SELECT * FROM Users WHERE UNAME='{uname}'") + return self.cur.fetchone() + + def update_user_info(self, userid, update_type:str,value): + allowed_types={"DNAME":str,"PASSHASH":str} + if update_type.upper() not in allowed_types: + return False, "Not allowed" + self.cur.execute(f"""UPDATE Users SET {update_type}={value if allowed_types[update_type]==int else f"'{value}'"} WHERE ID={userid}""") + return True, "Updated" + ## Checks information for errors and adds archive to the DB ## OUTPUT: (if successful) res:bool=True, ID:int ## (if unsuccessful) res:bool=False, str diff --git a/flask/static/user.css b/flask/static/user.css new file mode 100644 index 0000000..25bccf3 --- /dev/null +++ b/flask/static/user.css @@ -0,0 +1,9 @@ +div.grid-container { + display: grid; + grid-template-columns: max-content max-content; +} + +div.grid-container > * { + padding: 0.2em 0.5em; + border-bottom: lightgrey 0.1em solid; +} \ No newline at end of file diff --git a/flask/templates/user.html b/flask/templates/user.html new file mode 100644 index 0000000..69e69e5 --- /dev/null +++ b/flask/templates/user.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block meta %} + +{% endblock %} + +{% block content %} +
+ Display Name: + + {{userdata[2]}} + {% if userdata[0] == login_userid %} +
+ {% endif %} +
+ Username: {{userdata[1]}} + Joined: {{userdata[3]|ctime}} + Status: {% if userdata[4] %}{{userdata[4]}}{% else %}Okay{% endif %} +
+{% if userdata[0] == login_userid %} +
+
+
+ +

Change Password:

+ Old Password: + New Password: + Confirm Password: + +
+
+{% endif %} +{% endblock %} \ No newline at end of file