From 1b2f333956ad9b0babeb4b5e3dc6609a593caade Mon Sep 17 00:00:00 2001 From: craisin Date: Thu, 27 Nov 2025 21:50:13 -0800 Subject: [PATCH] get user --- allfence.py | 36 +++++++++++++++++++++++++++--------- app.py | 8 +++++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/allfence.py b/allfence.py index 2d7b31c..2aef315 100644 --- a/allfence.py +++ b/allfence.py @@ -3,16 +3,15 @@ import bcrypt def create_user(db, login_info, user_info): """ - Creates a new user + Creates a new user in the database - Parameters: - db (psycopg2.connection): connection to postgres database - login_info (dict of str:int): username and password info - user_info (dict of str:int): name and dob info + Args: + db (psycopg2.connection): connection to postgres database + login_info (dict of str:int): username and password info + user_info (dict of str:int): name and dob info Returns: - bool: success of user creation - str: result message + bool, str: status, message """ username, password = login_info["username"], login_info["password"] @@ -28,8 +27,27 @@ def create_user(db, login_info, user_info): db.commit() return True, "Success" -def get_user(db, id): - pass +def get_user_details(db, user_id): + + """ + Gets the details of a user based on their id + + Args: + db (psycopg2.connection): connection to postgres database + id (int): user id + + Returns: + bool, dict of str to str, dict of str to str: user existance, login info (username, password), user info (name, dob) + """ + + with db: + with db.cursor() as curs: + curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,)) + user = curs.fetchall() + if not user: + return False, {}, {} + user = user[0] + return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]} def create_event(db): pass diff --git a/app.py b/app.py index c5e2f6f..751d6b4 100644 --- a/app.py +++ b/app.py @@ -11,14 +11,16 @@ db_connection = sql.connect( host=HOST, port = PORT ) + +# Create databases with db_connection.cursor() as cursor: cursor.execute(dedent(""" CREATE TABLE IF NOT EXISTS Users ( id SERIAL PRIMARY KEY, - name varchar(255), username varchar(255), - dob varchar(255), - password_hash varchar(255) + password_hash varchar(255), + name varchar(255), + dob varchar(255) ); CREATE TABLE IF NOT EXISTS Events ( id SERIAL PRIMARY KEY,