-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_util.py
160 lines (126 loc) · 5.08 KB
/
api_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""This file will contain functions related to the API"""
import mysql.connector
import smtplib
from email.message import EmailMessage
from email.mime.base import MIMEBase
from email import encoders
import os
# Initalizes a connection to the database
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password123",
database="usersdb",
)
# Cursor to execute queries
cursor = mydb.cursor()
"""This function will search the table and return a true or false if the user exists"""
def confirm_login_credentials(username, password):
user = (username, password)
# Use this video guid for refreence: https://www.youtube.com/watch?v=x7SwgcpACng
cursor.execute("SELECT username , password FROM usersdb.userinfo")
result = (
cursor.fetchall()
) # gets all values from select statement and returns a list of tuples
# Search result for the username and password values
try:
result.index(user)
return True # return Yes if the user does exist
except ValueError:
return False # return No if the user does not exist
"""This function will check if the new user data exists"""
def check_user_exists(new_user_list):
# Use this video guid for refreence: https://www.youtube.com/watch?v=x7SwgcpACng
cursor.execute("SELECT username , password, email, name FROM usersdb.userinfo")
result = (
cursor.fetchall()
) # gets all values from select statement and returns a list of tuples
# Search result for the username and password values
try:
result.index(new_user_list)
return True # return Yes if the user does exist
except ValueError:
return False # return No if the user does not exist
"""This function will add the new user to the database"""
def add_new_user(new_user_list):
if check_user_exists(new_user_list):
return False
sqlQuery = (
"INSERT INTO userinfo (username,password,email,name) VALUES (%s,%s,%s,%s)"
)
cursor.execute(sqlQuery, new_user_list)
mydb.commit()
if check_user_exists(new_user_list):
return True
return False
"""This function will take the users information, obtain the email password and send it to the athletic trainer"""
def send_email(user_email, trainer_email, injury_form):
try:
msg = EmailMessage()
HOST = "smtp-mail.outlook.com" # Change this for all email types
PORT = 587
# users_password = get_users_password(user_email)
# if users_password == False:
# return False
PASSWORD = "ASDFGHJKL;z123"
msg["Subject"] = "Patient Outcome Reported Meaasure"
msg["From"] = EMAIL # Change this to user email
msg["To"] = trainer_email
msg.set_content(
" I finished my injury form! What days can we meet to discuss it?"
)
with open(f"uploads/{injury_form.filename}", "rb") as pdf_file:
pdf_data = pdf_file.read()
pdf_part = MIMEBase("application", "octet-stream")
pdf_part.set_payload(pdf_data)
encoders.encode_base64(pdf_part)
form_filename = injury_form.filename
pdf_part.add_header("Content-Disposition", "attachment", filename=form_filename)
msg.add_attachment(pdf_part)
except Exception as e:
print("Error: ", e)
return False
try:
print("Email message: ", msg)
smtp = smtplib.SMTP(HOST, PORT)
staus_code, response = smtp.ehlo()
print(f"Echoing the server: {staus_code} {response}")
staus_code, response = smtp.starttls()
print(f"Starting tls connection: {staus_code} {response}")
staus_code, response = smtp.login(
EMAIL, PASSWORD
) # This will need to be the users password
print(f"Logging in: {staus_code} {response}")
smtp.sendmail(str(msg["From"]), str(msg["To"]), msg.as_bytes())
print("Email sent Succesfully!")
empty_upload_folder()
return True
except Exception as e:
print("Error: ", e)
return False
"""This function will get the users email password from the Database"""
# def get_users_password(user_email):
# # Use this video guid for refreence: https://www.youtube.com/watch?v=x7SwgcpACng
# cursor.execute(f"SELECT * FROM usersdb.userinfo WHERE email is {user_email}")
# result = (
# cursor.fetchall()
# ) # gets all values from select statement and returns a list of tuples
# try:
# return result.pop(0) # Return the first password
# except:
# return False
"""This function clears the uploads folder"""
def empty_upload_folder():
folder_path = "uploads/"
files = os.listdir(folder_path)
# Iterate over each file and delete it
for file_name in files:
file_path = os.path.join(folder_path, file_name)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
os.rmdir(file_path)
except Exception as e:
print(f"Failed to delete {file_path}. Error: {e}")