Skip to content

Commit 8f72254

Browse files
authored
PI Access Request form sends email, connected to firebase (#60)
2 parents 6a83398 + 627efde commit 8f72254

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

flaskApp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ firebase/serviceAccountKey.json
44
__pycache__/
55
*.pyc
66
*.log
7+
email_credentials.py

flaskApp/app.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import bcrypt
55
from firebase.config import Config
66
from db_utils import upload_file_to_db, connect_to_database
7+
from flask_mail import Mail, Message
8+
import email_credentials
79

810
app = Flask(__name__)
911
app.config.from_object(Config)
@@ -22,6 +24,15 @@ def initialize_firebase():
2224
initialize_firebase()
2325
db = 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("/")
2738
def 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():
6172
def 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'])
66113
def 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():
96143
def 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")
100151
def homepage():
101152
return render_template('home.html')
@@ -139,3 +190,4 @@ def view_files():
139190

140191
if __name__ == "__main__":
141192
app.run(debug=True, host='0.0.0.0', port=5001, threaded=False)
193+
19 Bytes
Binary file not shown.

flaskApp/templates/home.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ <h3>Enter Access Code</h3>
1717
<button class="submit-btn" id="submitCode">Submit</button>
1818
</div>
1919
</div>
20+
<div>
21+
<div>
22+
<a href="{{ url_for('pi_access_request') }}">Request PI Access</a>
23+
</div>
24+
</div>
2025
</body>
2126
</html>
2227

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Bootstrap Login Form</title>
7+
<!-- Bootstrap CSS -->
8+
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
9+
</head>
10+
<body>
11+
<div class="container mt-5">
12+
<div class="row justify-content-center">
13+
<div class="col-md-4">
14+
<h2 class="text-center">Request PI Access</h2>
15+
<p>To request PI access, you must create an account
16+
and submit a request to the administrators.</p>
17+
<p>Already have a pre-approved access code?</p>
18+
<div class="d-flex justify-content-between mt-3">
19+
<a href="{{ url_for('pre_approved_access_code') }}">Enter PI Access Code</a></div>
20+
21+
<!-- Flash message for invalid credentials -->
22+
{% with messages = get_flashed_messages() %}
23+
{% if messages %}
24+
<div class="alert alert-danger" role="alert">
25+
{{ messages[0] }}
26+
</div>
27+
{% endif %}
28+
{% endwith %}
29+
30+
<form method="POST" action="/pi_access_request">
31+
<!-- Name input -->
32+
<div class="form-group">
33+
<label for="formName">Name</label>
34+
<input type="text" id="formName" name="name" class="form-control" placeholder="Enter name" required>
35+
</div>
36+
37+
<!-- Email input -->
38+
<div class="form-group">
39+
<label for="formEmail">Email address</label>
40+
<input type="email" id="formEmail" name="email" class="form-control" placeholder="Enter email" required>
41+
</div>
42+
43+
<!-- Institution input -->
44+
<div class="form-group">
45+
<label for="formInstitution">Institution</label>
46+
<input type="text" id="formInstitution" name="institution" class="form-control" placeholder="Enter Institution" required>
47+
</div>
48+
49+
<!-- Submit button -->
50+
<button type="submit" class="btn btn-primary btn-block">Request PI Access</button>
51+
</form>
52+
<div>
53+
<p>Once reviewed, you will receive an email with your request status: access granted or denied. </p>
54+
<p>If access is granted, the next time you login you will have PI access. </p>
55+
</div>
56+
</div>
57+
</div>
58+
</div>
59+
60+
<!-- Bootstrap JS -->
61+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
62+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
63+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
64+
</body>
65+
</html>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Bootstrap Login Form</title>
7+
<!-- Bootstrap CSS -->
8+
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
9+
</head>
10+
<body>
11+
<div class="container mt-5">
12+
<div class="row justify-content-center">
13+
<div class="col-md-4">
14+
<h2 class="text-center">Enter Pre-Approved Access Code</h2>
15+
16+
<!-- Flash message for invalid credentials -->
17+
{% with messages = get_flashed_messages() %}
18+
{% if messages %}
19+
<div class="alert alert-danger" role="alert">
20+
{{ messages[0] }}
21+
</div>
22+
{% endif %}
23+
{% endwith %}
24+
25+
<form method="POST" action="/pre_approved_access_code">
26+
<!-- PI Access Code input -->
27+
<div class="form-group">
28+
<label for="formName">PI Access Code</label>
29+
<input type="text" id="formPIAccessCode" name="PIAccessCode" class="form-control" placeholder="Enter PI Access code" required>
30+
</div>
31+
32+
<!-- Submit button -->
33+
<button type="submit" class="btn btn-primary btn-block">Submit Access Code</button>
34+
</form>
35+
</div>
36+
</div>
37+
</div>
38+
39+
<!-- Bootstrap JS -->
40+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
41+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
42+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
43+
</body>
44+
</html>

0 commit comments

Comments
 (0)