organization
This commit is contained in:
174
allfence.py
174
allfence.py
@@ -1,101 +1,111 @@
|
||||
import bcrypt
|
||||
import datetime
|
||||
|
||||
def create_user(db, login_info, user_info):
|
||||
class Users:
|
||||
|
||||
"""
|
||||
Creates a new user in the database
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
login_info (dict of str:str): username and password info
|
||||
user_info (dict of str:value): name (str) and dob (datetime.date) info
|
||||
@staticmethod
|
||||
def create_user(db, login_info, user_info):
|
||||
|
||||
Returns:
|
||||
int: 0 if the username is non-unique, user id otherwise
|
||||
"""
|
||||
"""
|
||||
Creates a new user in the database
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
login_info (dict of str:str): username and password info
|
||||
user_info (dict of str:value): name (str) and dob (datetime.date) info
|
||||
|
||||
username, password = login_info["username"], login_info["password"]
|
||||
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||||
name, dob = user_info["name"], user_info["dob"]
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE username = %s", (username,))
|
||||
conflicts = curs.fetchall()
|
||||
if conflicts:
|
||||
return 0
|
||||
curs.execute("INSERT INTO Users (username, password_hash, name, dob) VALUES (%s, %s, %s, %s) RETURNING id", (username, password_hash, name, dob))
|
||||
user_id = curs.fetchone()[0]
|
||||
db.commit()
|
||||
return user_id
|
||||
Returns:
|
||||
int: 0 if the username is non-unique, user id otherwise
|
||||
"""
|
||||
|
||||
def get_user_from_username(db, username):
|
||||
username, password = login_info["username"], login_info["password"]
|
||||
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||||
name, dob = user_info["name"], user_info["dob"]
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE username = %s", (username,))
|
||||
conflicts = curs.fetchall()
|
||||
if conflicts:
|
||||
return 0
|
||||
curs.execute("INSERT INTO Users (username, password_hash, name, dob) VALUES (%s, %s, %s, %s) RETURNING id", (username, password_hash, name, dob))
|
||||
user_id = curs.fetchone()[0]
|
||||
db.commit()
|
||||
return user_id
|
||||
|
||||
"""
|
||||
Get a user id based on their username
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
username (str): the username used to query
|
||||
@staticmethod
|
||||
def get_user_from_username(db, username):
|
||||
|
||||
Returns:
|
||||
int: 0 if the username is not in the system, user id otherwise
|
||||
"""
|
||||
"""
|
||||
Get a user id based on their username
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
username (str): the username used to query
|
||||
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE username = %s", (username,))
|
||||
users = curs.fetchall()
|
||||
if not users:
|
||||
return 0
|
||||
return users[0][0]
|
||||
Returns:
|
||||
int: 0 if the username is not in the system, user id otherwise
|
||||
"""
|
||||
|
||||
def get_user_details(db, user_id):
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE username = %s", (username,))
|
||||
users = curs.fetchall()
|
||||
if not users:
|
||||
return 0
|
||||
return users[0][0]
|
||||
|
||||
"""
|
||||
Gets the details of a user based on their id
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
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))
|
||||
"""
|
||||
@staticmethod
|
||||
def get_user_details(db, user_id):
|
||||
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,))
|
||||
user = curs.fetchall()
|
||||
if not user:
|
||||
return False, {}, {}
|
||||
user = user[0]
|
||||
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
|
||||
"""
|
||||
Gets the details of a user based on their id
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
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))
|
||||
"""
|
||||
|
||||
def create_event(db, name, time, max_age):
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("SELECT * FROM Users WHERE id = %s", (user_id,))
|
||||
user = curs.fetchall()
|
||||
if not user:
|
||||
return False, {}, {}
|
||||
user = user[0]
|
||||
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
|
||||
|
||||
"""
|
||||
Creates a new event in the database
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
name (str): event name
|
||||
time (date.datetime): event time
|
||||
max_age (int): how old a fencer is allowed to be at the time of the event starting
|
||||
class Tournaments:
|
||||
|
||||
Returns:
|
||||
int: id of the event created
|
||||
"""
|
||||
@staticmethod
|
||||
def create_event(db, name, time, max_age):
|
||||
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("INSERT INTO Events (name, datetime, max_age, completed, registration, results) VALUES (%s, %s, %s, False, '', '') RETURNING id", (name, time, max_age))
|
||||
event_id = curs.fetchone()[0]
|
||||
db.commit()
|
||||
return event_id
|
||||
"""
|
||||
Creates a new event in the database
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
name (str): event name
|
||||
time (date.datetime): event time
|
||||
max_age (int): how old a fencer is allowed to be at the time of the event starting
|
||||
|
||||
def create_tournament(db, events):
|
||||
pass
|
||||
Returns:
|
||||
int: id of the event created
|
||||
"""
|
||||
|
||||
def finish_event(db, event):
|
||||
pass
|
||||
with db:
|
||||
with db.cursor() as curs:
|
||||
curs.execute("INSERT INTO Events (name, datetime, max_age, completed, registration, results) VALUES (%s, %s, %s, False, '', '') RETURNING id", (name, time, max_age))
|
||||
event_id = curs.fetchone()[0]
|
||||
db.commit()
|
||||
return event_id
|
||||
|
||||
@staticmethod
|
||||
def create_tournament(db, events):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def finish_event(db, event):
|
||||
pass
|
||||
Reference in New Issue
Block a user