shift db connection to allfence

This commit is contained in:
2025-11-28 13:10:42 -08:00
parent 61f94e53ca
commit 6bee823c46
2 changed files with 64 additions and 44 deletions

View File

@@ -1,13 +1,64 @@
import bcrypt
import datetime
import psycopg2
def create_database_connection(db_name, user, password, host, port=5432):
"""Create a database connection, then ensure that tables are created
Args:
db_name (str): name of database
user (str): username of database
password (str): password to database
host (str): host address of database
port (int): port of database
Returns:
psycopg2.connection: connection to database
"""
connection = psycopg2.connect(
db_name=db_name,
user=user,
password=password,
host=host,
port=port
)
with connection:
with connection as curs:
curs.execute(dedent("""
CREATE TABLE IF NOT EXISTS Users (
id SERIAL PRIMARY KEY,
username varchar(255),
password_hash varchar(255),
name varchar(255),
dob date
);
CREATE TABLE IF NOT EXISTS Events (
id SERIAL PRIMARY KEY,
name varchar(255),
datetime datetime,
max_age int,
completed boolean,
registration varchar(255),
results varchar(255)
);
CREATE TABLE IF NOT EXISTS Tournaments (
id SERIAL PRIMARY KEY,
name varchar(255),
start_date date,
end_date date,
events varchar(255)
);
"""))
return connection
class Users:
@staticmethod
def create_user(db, login_info, user_info):
"""
Creates a new user in the database
"""Creates a new user in the database
Args:
db (psycopg2.connection): connection to postgres database
@@ -35,8 +86,7 @@ class Users:
@staticmethod
def get_user_from_username(db, username):
"""
Get a user id based on their username
"""Get a user id based on their username
Args:
db (psycopg2.connection): connection to postgres database
@@ -57,8 +107,7 @@ class Users:
@staticmethod
def get_user_details(db, user_id):
"""
Gets the details of a user based on their id
"""Gets the details of a user based on their id
Args:
db (psycopg2.connection): connection to postgres database
@@ -82,8 +131,7 @@ class Tournaments:
@staticmethod
def create_event(db, name, time, max_age):
"""
Creates a new event in the database
"""Creates a new event in the database
Args:
db (psycopg2.connection): connection to postgres database