Skip to content

Commit c00f0be

Browse files
feat: email function added for new signup user and create user
1 parent dd41d9b commit c00f0be

15 files changed

Lines changed: 315 additions & 55 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ chmod +x install_miniconda.sh && sudo ./install_miniconda.sh
5858
- Easy download and installation using a bash script.
5959
- Logged user and admin user will get the notification if the user kill some process manully on dashbaord.
6060

61+
## Email Feature 📧
62+
63+
| Email Alert | Is implemented | who will get the email |
64+
| ----------- | -------------- | ---------------------- |
65+
| Process Killed | Yes | Logged User |
66+
| Login | Yes | Admin User and Logged User |
67+
| Logout | Yes | Logged User |
68+
| Signup | Yes | Admin User & signed up User |
69+
| Create User | Yes | Admin User & Created User |
70+
| Delete User | No | Admin User & Deleted User |
71+
| Speed Test | Yes | Logged User |
72+
| Server Up | Yes | Admin User |
73+
| Notification Settings Change | Yes | Admin User |
74+
| Signup | Yes(few changes required) | Admin User & Logged User |
75+
76+
6177

6278
## Product Screenshots 📸
6379

app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from src import routes
44
from src.utils import render_template_from_file, get_flask_memory_usage, cpu_usage_percent, get_memory_percent
55
from src.models import User
6-
from src.scripts.email_me import send_email
6+
from src.scripts.email_me import send_smpt_email
77

88
def register_routes():
99
app.register_blueprint(routes.homepage_bp)
@@ -31,7 +31,7 @@ def server_up_email():
3131

3232
}
3333
html_body = render_template_from_file("src/templates/email_templates/server_up.html", **context)
34-
# send_email(admin_emails, subject, html_body, is_html=True)
34+
# send_smpt_email(admin_emails, subject, html_body, is_html=True)
3535
print("Server up email sent to", admin_emails)
3636

3737

src/routes/auth.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from flask import Flask, render_template, request, jsonify
99
import subprocess
1010

11-
from src.scripts.email_me import send_email
11+
from src.scripts.email_me import send_smpt_email
1212

1313
from src.config import app, db
1414
from src.models import User, SmptEamilPasswordConfig, DashboardSettings
@@ -74,15 +74,18 @@ def login():
7474
# log in alert to admin
7575

7676
if admin_email_address:
77-
context = {"username": current_user.username, "login_time": datetime.datetime.now()}
78-
login_body = render_template_from_file("src/templates/email_templates/admin_login_alert.html", **context)
79-
# send_email(admin_email_address, 'Login Alert', login_body, is_html=True)
77+
if receiver_email in admin_email_address:
78+
admin_email_address.remove(receiver_email)
79+
if admin_email_address:
80+
context = {"username": current_user.username, "login_time": datetime.datetime.now()}
81+
login_body = render_template_from_file("src/templates/email_templates/admin_login_alert.html", **context)
82+
send_smpt_email(admin_email_address, 'Login Alert', login_body, is_html=True)
8083

8184
# log in alert to user
8285
if receiver_email:
8386
context = {"username": current_user.username, "login_time": datetime.datetime.now()}
8487
login_body = render_template_from_file("src/templates/email_templates/login.html", **context)
85-
# send_email(receiver_email, 'Login Alert', login_body, is_html=True)
88+
send_smpt_email(receiver_email, 'Login Alert', login_body, is_html=True)
8689
return redirect(url_for('dashboard'))
8790
flash('Invalid username or password', 'danger')
8891
return render_template('login.html')
@@ -93,16 +96,19 @@ def logout():
9396
if receiver_email:
9497
context = {"username": current_user.username}
9598
logout_body = render_template_from_file("src/templates/email_templates/logout.html", **context)
96-
# send_email(receiver_email, 'Logout Alert', logout_body, is_html=True)
99+
send_smpt_email(receiver_email, 'Logout Alert', logout_body, is_html=True)
97100
logout_user()
98101
return redirect(url_for('login'))
99102

100103
@app.route('/signup', methods=['GET', 'POST'])
101104
def signup():
102105
if request.method == 'POST':
103106
username = request.form['username']
107+
email = request.form['email']
104108
password = request.form['password']
105109
confirm_password = request.form['confirm_password']
110+
user_level = request.form.get('user_level', 'user') # Default to 'user' if not provided
111+
receive_email_alerts = 'receive_email_alerts' in request.form # Checkbox is either checked or not
106112

107113
if password != confirm_password:
108114
flash('Passwords do not match')
@@ -114,16 +120,29 @@ def signup():
114120
return redirect(url_for('signup'))
115121

116122
hashed_password = generate_password_hash(password)
117-
new_user = User(username=username, password=hashed_password)
123+
new_user = User(username=username, email=email, password=hashed_password, user_level=user_level, receive_email_alerts=receive_email_alerts)
118124

119125
# Get Admin Emails with Alerts Enabled:
120126
admin_email_address = get_email_addresses(user_level='admin', receive_email_alerts=True)
121-
# extends the signup user to send an email to the admin
122127
if admin_email_address:
123-
send_email(admin_email_address, 'New User Alert', f'{username} has signed up to the system.')
124-
125-
# send email to the new user
126-
send_email([new_user.email], 'Welcome to the system', f'Hello {new_user.username}, welcome to the system.')
128+
subject = "New User Alert"
129+
context = {
130+
"username": new_user.username,
131+
"email": new_user.email,
132+
"signup_time": datetime.datetime.now(),
133+
"user_level": new_user.user_level
134+
}
135+
html_body = render_template_from_file("src/templates/email_templates/new_user_alert.html", **context)
136+
send_smpt_email(admin_email_address, subject, html_body, is_html=True)
137+
138+
# Send email to the new user
139+
subject = "Welcome to the systemGuard"
140+
context = {
141+
"username": new_user.username,
142+
"email": new_user.email,
143+
}
144+
html_body = render_template_from_file("src/templates/email_templates/welcome.html", **context)
145+
send_smpt_email(email, subject, html_body, is_html=True)
127146

128147
db.session.add(new_user)
129148
db.session.commit()
@@ -263,7 +282,7 @@ def send_email_page():
263282
attachment_path = f"/tmp/{attachment.filename}"
264283
attachment.save(attachment_path)
265284
try:
266-
respose = send_email(receiver_email, subject, body, attachment_path)
285+
respose = send_smpt_email(receiver_email, subject, body, attachment_path)
267286
print(respose)
268287
if respose and respose.get("status") == "success":
269288
flash(respose.get("message"), "success")
@@ -293,7 +312,11 @@ def terminal():
293312
return render_template('terminal.html')
294313

295314
@app.route('/add_user', methods=['GET', 'POST'])
315+
@login_required
296316
def add_user():
317+
if current_user.user_level != 'admin':
318+
flash("Your account does not have permission to view this page.", "danger")
319+
return render_template("error/permission_denied.html")
297320
if request.method == 'POST':
298321
username = request.form['username']
299322
email = request.form['email']
@@ -313,6 +336,28 @@ def add_user():
313336
user_level=user_level,
314337
receive_email_alerts=receive_email_alerts
315338
)
339+
340+
admin_email_address = get_email_addresses(user_level='admin', receive_email_alerts=True)
341+
if admin_email_address:
342+
subject = "New User Alert"
343+
context = {
344+
"current_user": current_user.username,
345+
"username": new_user.username,
346+
"email": new_user.email,
347+
"signup_time": datetime.datetime.now(),
348+
"user_level": new_user.user_level
349+
}
350+
html_body = render_template_from_file("src/templates/email_templates/new_user_create.html", **context)
351+
send_smpt_email(admin_email_address, subject, html_body, is_html=True)
352+
353+
subject = "Welcome to the systemGuard"
354+
context = {
355+
"username": new_user.username,
356+
"email": new_user.email,
357+
}
358+
html_body = render_template_from_file("src/templates/email_templates/welcome.html", **context)
359+
send_smpt_email(email, subject, html_body, is_html=True)
360+
316361
db.session.add(new_user)
317362
db.session.commit()
318363
flash('User created successfully!', 'success')

src/routes/process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from src.config import app
55
from src.utils import get_top_processes, render_template_from_file
66
from src.models import DashboardSettings
7-
from src.scripts.email_me import send_email
7+
from src.scripts.email_me import send_smpt_email
88

99
process_bp = blueprints.Blueprint("process", __name__)
1010

@@ -36,7 +36,7 @@ def process():
3636
subject = f"Process '{process_name}' (PID {pid_to_kill}) killed successfully."
3737
context = {"process_name": process_name, "pid_to_kill": pid_to_kill, "username": current_user.username}
3838
html_body = render_template_from_file("src/templates/email_templates/process_killed.html", **context)
39-
send_email(receiver_email, subject, html_body, is_html=True)
39+
send_smpt_email(receiver_email, subject, html_body, is_html=True)
4040
except Exception as e:
4141
flash(f"Failed to kill process '{process_name}' (PID {pid_to_kill}). Error: {e}", "danger")
4242
return redirect(url_for("process")) # Refresh the page after killing process

src/routes/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from src.models import DashboardSettings, User
55
from flask_login import login_required, current_user
66
from src.utils import render_template_from_file
7-
from src.scripts.email_me import send_email
7+
from src.scripts.email_me import send_smpt_email
88

99
settings_bp = blueprints.Blueprint("settings", __name__)
1010

@@ -40,7 +40,7 @@ def general_settings():
4040
}
4141
html_body = render_template_from_file("src/templates/email_templates/notification_alert.html", **context)
4242
print("Notification enabled:", settings.enable_alerts)
43-
send_email(admin_emails, subject, html_body, is_html=True, bypass_alerts=True)
43+
send_smpt_email(admin_emails, subject, html_body, is_html=True, bypass_alerts=True)
4444
db.session.commit()
4545
flash('General settings updated successfully!', 'success')
4646
return redirect(url_for('general_settings'))

src/routes/speedtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from src.config import app, db
55
from src.models import DashboardSettings, SpeedTestResult
66
from src.utils import run_speedtest, render_template_from_file
7-
from src.scripts.email_me import send_email
7+
from src.scripts.email_me import send_smpt_email
88

99
speedtest_bp = blueprints.Blueprint("speedtest", __name__)
1010

@@ -40,7 +40,7 @@ def speedtest():
4040
subject = "Speedtest Result"
4141
context = {"speedtest_result": speedtest_result}
4242
body = render_template_from_file("src/templates/email_templates/speedtest_result.html", **context)
43-
send_email(receiver_email, subject, body, is_html=True)
43+
send_smpt_email(receiver_email, subject, body, is_html=True)
4444
return render_template(
4545
"speedtest_result.html",
4646
speedtest_result=speedtest_result,

src/scripts/battery_tracking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import psutil
22
import os
33
from datetime import datetime, timedelta
4-
from src.utils import send_email
4+
from src.utils.email_me import send_smpt_email
55

66
STATUS_FILE = 'battery_status.txt'
77
NOTIFICATION_INTERVAL = timedelta(hours=1) # Minimum time between notifications

src/scripts/email_me.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
system_name = os.uname().sysname
1212

13-
def send_email(receiver_email, subject, body, attachment_path=None, is_html=False, bypass_alerts=False):
13+
def send_smpt_email(receiver_email, subject, body, attachment_path=None, is_html=False, bypass_alerts=False):
1414

1515
if isinstance(receiver_email, str):
1616
receiver_email = [receiver_email] # Convert single address to list

src/static/css/signup.css

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,64 @@
11
.signup-container {
2-
max-width: 400px;
3-
margin: 0 auto;
4-
padding: 20px;
5-
background-color: #f8f9fa;
2+
max-width: 600px;
3+
margin: 50px auto;
4+
background-color: #ffffff;
5+
border: 1px solid #ddd;
66
border-radius: 8px;
7+
padding: 20px;
78
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
8-
text-align: center;
9-
margin-top: 50px;
109
}
1110

1211
.signup-title {
1312
font-size: 24px;
14-
color: #343a40;
13+
font-weight: bold;
1514
margin-bottom: 20px;
15+
text-align: center;
1616
}
1717

1818
.signup-form {
1919
display: flex;
2020
flex-direction: column;
21-
gap: 15px;
2221
}
2322

2423
.form-group {
25-
display: flex;
26-
flex-direction: column;
27-
align-items: flex-start;
2824
margin-bottom: 15px;
2925
}
3026

3127
.form-group label {
32-
font-weight: 600;
33-
color: #495057;
28+
display: block;
29+
font-weight: bold;
3430
margin-bottom: 5px;
3531
}
3632

37-
.form-group input {
38-
padding: 10px;
39-
border: 1px solid #ced4da;
40-
border-radius: 4px;
41-
background-color: #ffffff;
42-
font-size: 16px;
33+
.form-group input,
34+
.form-group select {
4335
width: 100%;
36+
padding: 8px;
37+
border: 1px solid #ccc;
38+
border-radius: 4px;
39+
}
40+
41+
.form-group input[type="checkbox"] {
42+
width: auto;
4443
}
4544

4645
.btn-primary {
47-
padding: 10px 20px;
4846
background-color: #007bff;
4947
color: #ffffff;
48+
padding: 10px;
5049
border: none;
5150
border-radius: 4px;
5251
cursor: pointer;
53-
font-size: 16px;
5452
text-align: center;
55-
transition: background-color 0.3s ease;
5653
}
5754

5855
.btn-primary:hover {
5956
background-color: #0056b3;
6057
}
6158

6259
.login-link {
60+
text-align: center;
6361
margin-top: 20px;
64-
font-size: 14px;
65-
color: #6c757d;
6662
}
6763

6864
.login-link a {

src/templates/base.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
{% block extra_head %}{% endblock %}
1414
</head>
1515

16-
<body>{% include 'ext/navbar.html' %} <div class="container">{% block content %}{% endblock %} </div>
17-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
18-
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
16+
<body>{% include 'ext/navbar.html' %}
17+
<div class="container">{% block content %}{% endblock %} </div>
18+
<!-- jQuery -->
19+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
20+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
1921
<script src="{{ url_for('static', filename='js/script.js')}}"></script>
2022
{% block extra_scripts %}{% endblock %}
2123
</body>

0 commit comments

Comments
 (0)