add tournament creation
This commit is contained in:
62
allfence.py
62
allfence.py
@@ -1,6 +1,7 @@
|
|||||||
import bcrypt
|
import bcrypt
|
||||||
import datetime
|
import datetime
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
def create_database_connection(db_name, user, password, host, port=5432):
|
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(
|
connection = psycopg2.connect(
|
||||||
db_name=db_name,
|
dbname=db_name,
|
||||||
user=user,
|
user=user,
|
||||||
password=password,
|
password=password,
|
||||||
host=host,
|
host=host,
|
||||||
port=port
|
port=port
|
||||||
)
|
)
|
||||||
with connection:
|
with connection:
|
||||||
with connection as curs:
|
with connection.cursor() as curs:
|
||||||
curs.execute(dedent("""
|
curs.execute(dedent("""
|
||||||
CREATE TABLE IF NOT EXISTS Users (
|
CREATE TABLE IF NOT EXISTS Users (
|
||||||
id SERIAL PRIMARY KEY,
|
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 (
|
CREATE TABLE IF NOT EXISTS Events (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name varchar(255),
|
name varchar(255),
|
||||||
datetime datetime,
|
datetime timestamp,
|
||||||
max_age int,
|
max_age int,
|
||||||
completed boolean,
|
completed boolean,
|
||||||
registration varchar(255),
|
registration varchar(255),
|
||||||
@@ -111,7 +112,7 @@ class Users:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (psycopg2.connection): connection to postgres database
|
db (psycopg2.connection): connection to postgres database
|
||||||
id (int): user id
|
user_id (int): user id
|
||||||
|
|
||||||
Returns:
|
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))
|
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,))
|
curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,))
|
||||||
user = curs.fetchall()
|
user = curs.fetchall()
|
||||||
if not user:
|
if not user:
|
||||||
return False, {}, {}
|
return False, None, None
|
||||||
user = user[0]
|
user = user[0]
|
||||||
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
|
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
|
||||||
|
|
||||||
@@ -151,9 +152,56 @@ class Tournaments:
|
|||||||
return event_id
|
return event_id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_tournament(db, events):
|
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, 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
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def finish_event(db, event):
|
def add_event_results(db, event):
|
||||||
pass
|
pass
|
||||||
2
app.py
2
app.py
@@ -5,7 +5,7 @@ import psycopg2 as sql
|
|||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT = "database", "user", "password", "localhost", "5432"
|
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,
|
db_name=DB_NAME,
|
||||||
user=DB_USER,
|
user=DB_USER,
|
||||||
password=DB_PASSWORD,
|
password=DB_PASSWORD,
|
||||||
|
|||||||
Reference in New Issue
Block a user