Compare commits
2 Commits
1b2f333956
...
4584b1f58f
| Author | SHA1 | Date | |
|---|---|---|---|
| 4584b1f58f | |||
| cc62093cdb |
64
allfence.py
64
allfence.py
@@ -1,4 +1,5 @@
|
||||
import bcrypt
|
||||
import datetime
|
||||
|
||||
def create_user(db, login_info, user_info):
|
||||
|
||||
@@ -7,11 +8,11 @@ def create_user(db, login_info, user_info):
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
login_info (dict of str:int): username and password info
|
||||
user_info (dict of str:int): name and dob info
|
||||
login_info (dict of str:str): username and password info
|
||||
user_info (dict of str:value): name (str) and dob (datetime.date) info
|
||||
|
||||
Returns:
|
||||
bool, str: status, message
|
||||
int: 0 if the username is non-unique, user id otherwise
|
||||
"""
|
||||
|
||||
username, password = login_info["username"], login_info["password"]
|
||||
@@ -22,10 +23,32 @@ def create_user(db, login_info, user_info):
|
||||
curs.execute("SELECT * FROM Users WHERE username = %s", (username,))
|
||||
conflicts = curs.fetchall()
|
||||
if conflicts:
|
||||
return False, "Duplicate username"
|
||||
curs.execute("INSERT INTO Users (username, password_hash, name, dob) VALUES (%s, %s, %s, %s)", (username, password_hash, name, dob))
|
||||
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 True, "Success"
|
||||
return user_id
|
||||
|
||||
def get_user_from_username(db, username):
|
||||
|
||||
"""
|
||||
Get a user id based on their username
|
||||
|
||||
Args:
|
||||
db (psycopg2.connection): connection to postgres database
|
||||
username (str): the username used to query
|
||||
|
||||
Returns:
|
||||
int: 0 if the username is not in the system, user id otherwise
|
||||
"""
|
||||
|
||||
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]
|
||||
|
||||
def get_user_details(db, user_id):
|
||||
|
||||
@@ -37,7 +60,7 @@ def get_user_details(db, user_id):
|
||||
id (int): user id
|
||||
|
||||
Returns:
|
||||
bool, dict of str to str, dict of str to str: user existance, login info (username, password), user info (name, dob)
|
||||
bool, dict of str to str, dict of str to value: user existance, login info (username, password), user info (name (str), dob (datetime.date))
|
||||
"""
|
||||
|
||||
with db:
|
||||
@@ -49,11 +72,30 @@ def get_user_details(db, user_id):
|
||||
user = user[0]
|
||||
return True, {"username": user[1], "password_hash": user[2]}, {"name": user[3], "dob": user[4]}
|
||||
|
||||
def create_event(db):
|
||||
def create_event(db, name, time, max_age):
|
||||
|
||||
"""
|
||||
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
|
||||
|
||||
Returns:
|
||||
int: id of the event created
|
||||
"""
|
||||
|
||||
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
|
||||
|
||||
def create_tournament(db, events):
|
||||
pass
|
||||
|
||||
def finish_event(db, event):
|
||||
pass
|
||||
|
||||
def create_tournament(db, events):
|
||||
pass
|
||||
13
app.py
13
app.py
@@ -1,4 +1,5 @@
|
||||
import allfence as af
|
||||
import datetime
|
||||
from flask import Flask
|
||||
import psycopg2 as sql
|
||||
from textwrap import dedent
|
||||
@@ -20,20 +21,22 @@ with db_connection.cursor() as cursor:
|
||||
username varchar(255),
|
||||
password_hash varchar(255),
|
||||
name varchar(255),
|
||||
dob varchar(255)
|
||||
dob date
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS Events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name varchar(255),
|
||||
datetime varchar(255),
|
||||
users 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 varchar(255),
|
||||
end_date varchar(255),
|
||||
start_date date,
|
||||
end_date date,
|
||||
events varchar(255)
|
||||
);
|
||||
"""))
|
||||
|
||||
Reference in New Issue
Block a user