44import bcrypt
55from firebase .config import Config
66from db_utils import upload_file_to_db , connect_to_database
7+ from flask_mail import Mail , Message
8+ import email_credentials
79
810app = Flask (__name__ )
911app .config .from_object (Config )
@@ -22,6 +24,15 @@ def initialize_firebase():
2224initialize_firebase ()
2325db = firestore .client ()
2426
27+ # Configure Flask-Mail email settings
28+ app .config ['MAIL_SERVER' ] = 'smtp.gmail.com' # SMTP email server
29+ app .config ['MAIL_PORT' ] = 587
30+ app .config ['MAIL_USERNAME' ] = email_credentials .hua_email
31+ app .config ['MAIL_PASSWORD' ] = email_credentials .hua_password
32+ app .config ['MAIL_USE_TLS' ] = True
33+ app .config ['MAIL_USE_SSL' ] = False
34+ mail = Mail (app )
35+
2536
2637@app .route ("/" )
2738def home ():
@@ -50,7 +61,7 @@ def register():
5061 'email' : email ,
5162 'password' : hashed_password .decode ('utf-8' )
5263 })
53- return redirect (url_for ('homepage' ))
64+ return redirect (url_for ('homepage' , email = email ))
5465
5566 except Exception as e :
5667 flash (f"An error occurred: { str (e )} " , "danger" )
@@ -61,6 +72,42 @@ def register():
6172def forgot_password ():
6273 return render_template ('forgot_password.html' )
6374
75+ @app .route ('/pi_access_request' , methods = ['GET' , 'POST' ])
76+ def pi_access_request ():
77+ name = request .form .get ('name' )
78+ email = request .form .get ('email' )
79+ institution = request .form .get ('institution' )
80+ if request .method == 'POST' :
81+ try :
82+ # Add request to HUA firebase
83+ requests_ref = db .collection ('request' )
84+ requests_doc = requests_ref .add ({"username" :email })
85+
86+ # Send email
87+ recipients = email_credentials .recipients
88+
89+ emailMessage = Message ("Request for PI Access" , sender = email ,recipients = recipients )
90+ emailMessage .body = f"Hello Bob and Donna,\n \n { name } is requesting admin access. { name } is from { institution } and reachable at { email } .\n \n You will find their request on the View Requests for Access page in the Heard and Understood App."
91+ mail .send (emailMessage )
92+ print ('email sent successfully!' )
93+ except Exception as e :
94+ print ('problem sending email' )
95+ print (e )
96+
97+ return redirect (url_for ('homepage' ))
98+ return render_template ('pi_access_request.html' )
99+
100+ @app .route ('/pre_approved_access_code' , methods = ['GET' , 'POST' ])
101+ def pre_approved_access_code ():
102+ access_code = request .form .get ('PIAccessCode' )
103+ if request .method == 'POST' :
104+ try :
105+ print (f"pre-approved code { access_code } entered" )
106+ except :
107+ print ("issue with pre-approved code" )
108+ return redirect (url_for ('homepage' ))
109+ return render_template ('pre_approved_access_code.html' )
110+
64111
65112@app .route ('/login' , methods = ['GET' , 'POST' ])
66113def login ():
@@ -78,7 +125,7 @@ def login():
78125
79126 # Check if the password matches
80127 if bcrypt .checkpw (password .encode ('utf-8' ), stored_hashed_password .encode ('utf-8' )):
81- return redirect (url_for ('homepage' ))
128+ return redirect (url_for ('homepage' , email = email ))
82129 else :
83130 flash ("Invalid password" , "danger" )
84131 return redirect (url_for ('login' ))
@@ -96,6 +143,10 @@ def login():
96143def dashboard ():
97144 return render_template ("dashboard.html" )
98145
146+ @app .route ("/ground_truthing" )
147+ def ground_truthing ():
148+ return render_template ("ground_truthing.html" )
149+
99150@app .route ("/homepage" )
100151def homepage ():
101152 return render_template ('home.html' )
@@ -139,3 +190,4 @@ def view_files():
139190
140191if __name__ == "__main__" :
141192 app .run (debug = True , host = '0.0.0.0' , port = 5001 , threaded = False )
193+
0 commit comments