add tournament creation

This commit is contained in:
2025-11-28 22:05:35 -08:00
parent 6bee823c46
commit fb175c38fe
2 changed files with 56 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
import bcrypt
import datetime
import psycopg2
from textwrap import dedent
def create_database_connection(db_name, user, password, host, port=5432):
@@ -18,14 +19,14 @@ def create_database_connection(db_name, user, password, host, port=5432):
"""
connection = psycopg2.connect(
db_name=db_name,
dbname=db_name,
user=user,
password=password,
host=host,
port=port
)
with connection:
with connection as curs:
with connection.cursor() as curs:
curs.execute(dedent("""
CREATE TABLE IF NOT EXISTS Users (
id SERIAL PRIMARY KEY,
@@ -37,7 +38,7 @@ def create_database_connection(db_name, user, password, host, port=5432):
CREATE TABLE IF NOT EXISTS Events (
id SERIAL PRIMARY KEY,
name varchar(255),
datetime datetime,
datetime timestamp,
max_age int,
completed boolean,
registration varchar(255),
@@ -111,7 +112,7 @@ class Users:
Args:
db (psycopg2.connection): connection to postgres database
id (int): user id
user_id (int): user id
Returns:
bool, dict of str to str, dict of str to value: user existance, login info (username, password), user info (name (str), dob (datetime.date))
@@ -122,7 +123,7 @@ class Users:
curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,))
user = curs.fetchall()
if not user:
return False, {}, {}
return False, None, None
user = user[0]
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
@@ -149,11 +150,58 @@ class Tournaments:
event_id = curs.fetchone()[0]
db.commit()
return event_id
@staticmethod
def get_event(db, event_id):
"""Gets the details of an event based on its id
Args:
db (psycopg2.connection): connection to postgres database
event_id (int): event id
Returns:
bool, str, datetime.datetime, int, bool, list of ints, list of ints: existance of event, name, datetime of event, max age, event completion, registration, results
"""
with db:
with db.cursor() as curs:
curs.execute("SELECT * FROM Events WHERE id = %s", (event_id,))
event = curs.fetchall()
if not event:
return False, None, None, None, None, None, None
event = event[0]
return True, event[1], event[2], event[3], event[4], list(map(int, event[5].split())), list(map(int, event[6].split()))
@staticmethod
def create_tournament(db, events):
def create_tournament(db, name, event_ids):
"""Create a new tournament in the database
Args:
db (psycopg2.connection): connection to postgres database
name (str): name of tournament
event_ids (list of ints): list of events in the tournament
Returns:
int: id of event
"""
event_str = " ".join(map(str, event_ids))
events = [Tournaments.get_event(db, event_id)[2] for event_id in event_ids]
print(events)
start_date, end_date = min(events).date(), max(events).date()
with db:
with db.cursor() as curs:
curs.execute("INSERT INTO Tournaments (name, start_date, end_date, events) VALUES (%s, %s, %s, %s) RETURNING id", (name, start_date, end_date, event_str))
tournament_id = curs.fetchone()[0]
db.commit()
return tournament_id
@staticmethod
def add_event_registration(db, event_id, user_id):
pass
@staticmethod
def finish_event(db, event):
def add_event_results(db, event):
pass

2
app.py
View File

@@ -5,7 +5,7 @@ import psycopg2 as sql
from textwrap import dedent
DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT = "database", "user", "password", "localhost", "5432"
connection = allfence.connect(
connection = allfence.create_database_connection(
db_name=DB_NAME,
user=DB_USER,
password=DB_PASSWORD,