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 bcrypt
import datetime 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: class Users:
@staticmethod @staticmethod
def create_user(db, login_info, user_info): def create_user(db, login_info, user_info):
""" """Creates a new user in the database
Creates a new user in the database
Args: Args:
db (psycopg2.connection): connection to postgres database db (psycopg2.connection): connection to postgres database
@@ -35,8 +86,7 @@ class Users:
@staticmethod @staticmethod
def get_user_from_username(db, username): def get_user_from_username(db, username):
""" """Get a user id based on their username
Get a user id based on their username
Args: Args:
db (psycopg2.connection): connection to postgres database db (psycopg2.connection): connection to postgres database
@@ -57,8 +107,7 @@ class Users:
@staticmethod @staticmethod
def get_user_details(db, user_id): 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: Args:
db (psycopg2.connection): connection to postgres database db (psycopg2.connection): connection to postgres database
@@ -82,8 +131,7 @@ class Tournaments:
@staticmethod @staticmethod
def create_event(db, name, time, max_age): def create_event(db, name, time, max_age):
""" """Creates a new event in the database
Creates a new event in the database
Args: Args:
db (psycopg2.connection): connection to postgres database db (psycopg2.connection): connection to postgres database

44
app.py
View File

@@ -1,46 +1,18 @@
import allfence as af import allfence
import datetime import datetime
from flask import Flask from flask import Flask
import psycopg2 as sql import psycopg2 as sql
from textwrap import dedent from textwrap import dedent
DB_NAME, USER, PASSWORD, HOST, PORT = "database", "user", "password", "localhost", "5432" DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT = "database", "user", "password", "localhost", "5432"
db_connection = sql.connect( connection = allfence.connect(
dbname=DB_NAME, db_name=DB_NAME,
user=USER, user=DB_USER,
password=PASSWORD, password=DB_PASSWORD,
host=HOST, host=DB_HOST,
port = PORT port=DB_PORT
) )
# Create databases
with db_connection.cursor() as cursor:
cursor.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)
);
"""))
HOST, PORT = "0.0.0.0", 8000 HOST, PORT = "0.0.0.0", 8000
app = Flask(__name__) app = Flask(__name__)