Skip to content

Commit d870db7

Browse files
committed
UI overhaul, user login added, removed dependence on GC
UI overhauled to a less cluttered and confusing User login section has been added to move away from using GC username as a user identifier across the app.
1 parent 0f321d2 commit d870db7

34 files changed

+2609
-4564
lines changed

src/athlete_auth.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import psycopg2
2+
from Athlete_Data_Utills import StdoutRedirection,ErrorStdoutRedirection,ProgressStdoutRedirection,ConsolidatedProgressStdoutRedirection
3+
import sys
4+
from database_ini_parser import config
5+
import os
6+
import datetime
7+
from processify import processify
8+
from db_encrypt import str2md5
9+
from werkzeug.security import generate_password_hash, check_password_hash
10+
11+
def ath_auth_register(ath_un,ath_pw,encr_pass):
12+
13+
sql_create_info_db_table = """
14+
CREATE TABLE IF NOT EXISTS public.db_info
15+
(id serial NOT NULL,
16+
ath_un character varying(300) COLLATE pg_catalog."default",
17+
ath_pw character varying(300) COLLATE pg_catalog."default",
18+
db_name character varying(300) COLLATE pg_catalog."default",
19+
db_host character varying(300) COLLATE pg_catalog."default",
20+
db_un character varying(300) COLLATE pg_catalog."default",
21+
db_pw character varying(300) COLLATE pg_catalog."default",
22+
last_synch character varying(300) COLLATE pg_catalog."default",
23+
db_auto_synch boolean,
24+
CONSTRAINT db_info_pkey PRIMARY KEY (id),
25+
CONSTRAINT db_info_unique UNIQUE (db_name))
26+
WITH (OIDS = FALSE)
27+
TABLESPACE pg_default;
28+
ALTER TABLE public.db_info OWNER to postgres;
29+
"""
30+
sql_insert_db_info_record = "INSERT INTO db_info (ath_un,ath_pw,db_name) VALUES (%s,%s,%s);"
31+
32+
ath_pw_hash = generate_password_hash(ath_pw)
33+
34+
db_name = str(str2md5(ath_un)) + '_Athlete_Data_DB'
35+
query_params = (ath_un,ath_pw_hash,db_name)
36+
37+
try:
38+
# connect to the PostgreSQL server
39+
params = config(filename="encrypted_settings.ini", section="postgresql", encr_pass=encr_pass)
40+
postgres_db = params.get("database")
41+
superuser_un = params.get("user")
42+
superuser_pw = params.get("password")
43+
44+
conn_localhost = psycopg2.connect(dbname=postgres_db, user=superuser_un, password=superuser_pw)
45+
conn_localhost.autocommit = True
46+
47+
# create a cursor
48+
cur = conn_localhost.cursor()
49+
50+
cur.execute(sql_create_info_db_table)
51+
cur.execute(sql_insert_db_info_record,query_params)
52+
53+
# close the communication with the PostgreSQL
54+
cur.close()
55+
except (Exception, psycopg2.IntegrityError) as error:
56+
print((str(datetime.datetime.now()) + ' [' + sys._getframe().f_code.co_name + ']' + ' Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + ' ' + str(error)))
57+
return None
58+
finally:
59+
if conn_localhost is not None:
60+
conn_localhost.close()
61+
return ath_un
62+
63+
def ath_auth_login(ath_un,ath_pw,encr_pass):
64+
65+
sql_select_usr_pwd = "SELECT ath_pw FROM db_info WHERE ath_un = %s;"
66+
67+
query_params = (ath_un,)
68+
69+
try:
70+
# connect to the PostgreSQL server
71+
params = config(filename="encrypted_settings.ini", section="postgresql", encr_pass=encr_pass)
72+
postgres_db = params.get("database")
73+
superuser_un = params.get("user")
74+
superuser_pw = params.get("password")
75+
76+
conn_localhost = psycopg2.connect(dbname=postgres_db, user=superuser_un, password=superuser_pw)
77+
conn_localhost.autocommit = True
78+
79+
# create a cursor
80+
cur = conn_localhost.cursor()
81+
cur.execute(sql_select_usr_pwd,query_params)
82+
result = cur.fetchone()
83+
cur.close()
84+
if check_password_hash(result[0],ath_pw):
85+
return ath_un
86+
else:
87+
return None
88+
except Exception as error:
89+
print((str(datetime.datetime.now()) + ' [' + sys._getframe().f_code.co_name + ']' + ' Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + ' ' + str(error)))
90+
return None
91+
finally:
92+
if conn_localhost is not None:
93+
conn_localhost.close()
94+
95+
def ath_auth_reset(ath_un,ath_pw,encr_pass):
96+
97+
sql_update_usr_pwd = "UPDATE db_info SET ath_pw = %s where ath_un= %s;"
98+
99+
ath_pw_hash = generate_password_hash(ath_pw)
100+
query_params = (ath_pw_hash,ath_un)
101+
102+
try:
103+
# connect to the PostgreSQL server
104+
params = config(filename="encrypted_settings.ini", section="postgresql", encr_pass=encr_pass)
105+
postgres_db = params.get("database")
106+
superuser_un = params.get("user")
107+
superuser_pw = params.get("password")
108+
109+
conn_localhost = psycopg2.connect(dbname=postgres_db, user=superuser_un, password=superuser_pw)
110+
conn_localhost.autocommit = True
111+
112+
# create a cursor
113+
cur = conn_localhost.cursor()
114+
cur.execute(sql_update_usr_pwd,query_params)
115+
116+
except Exception as error:
117+
print((str(datetime.datetime.now()) + ' [' + sys._getframe().f_code.co_name + ']' + ' Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + ' ' + str(error)))
118+
return None
119+
finally:
120+
if conn_localhost is not None:
121+
conn_localhost.close()

0 commit comments

Comments
 (0)