diff --git a/allfence.py b/allfence.py index 9e29923..45ca469 100644 --- a/allfence.py +++ b/allfence.py @@ -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 diff --git a/app.py b/app.py index 8c1551e..5023040 100644 --- a/app.py +++ b/app.py @@ -1,46 +1,18 @@ -import allfence as af +import allfence import datetime from flask import Flask import psycopg2 as sql from textwrap import dedent -DB_NAME, USER, PASSWORD, HOST, PORT = "database", "user", "password", "localhost", "5432" -db_connection = sql.connect( - dbname=DB_NAME, - user=USER, - password=PASSWORD, - host=HOST, - port = PORT +DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT = "database", "user", "password", "localhost", "5432" +connection = allfence.connect( + db_name=DB_NAME, + user=DB_USER, + password=DB_PASSWORD, + host=DB_HOST, + 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 app = Flask(__name__)