Skip to content

Commit 7667eaa

Browse files
feat: Update file paths for pre-defined users and cache in models and utils
1 parent 0d8aa16 commit 7667eaa

7 files changed

Lines changed: 45 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ A Docker image has not been created for this project because it requires access
108108
- Threshold notifications
109109
- Customizable dashboards
110110
- Plugin support to make SystemGuard even more powerful.
111+
- make server logs
111112

112113
## Acknowledgments
113114

app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from src.config import app
33
from src import routes
4-
from src.utils import render_template_from_file, get_flask_memory_usage, cpu_usage_percent, get_memory_percent
4+
from src.utils import render_template_from_file, get_flask_memory_usage, cpu_usage_percent, get_memory_percent, ROOT_DIR
55
from src.models import UserProfile
66
from src.scripts.email_me import send_smpt_email
77

@@ -30,7 +30,8 @@ def server_up_email():
3030
"app_memory_usage": get_flask_memory_usage(),
3131

3232
}
33-
html_body = render_template_from_file("src/templates/email_templates/server_up.html", **context)
33+
server_up_template = os.path.join(ROOT_DIR, "src/templates/email_templates/server_up.html")
34+
html_body = render_template_from_file(server_up_template, **context)
3435
# send_smpt_email(admin_emails, subject, html_body, is_html=True)
3536
print("Server up email sent to", admin_emails)
3637

src/routes/auth.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import datetime
23
from flask import render_template, redirect, url_for, request, blueprints, flash
34
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
@@ -6,7 +7,7 @@
67
from src.scripts.email_me import send_smpt_email
78
from src.config import app, db
89
from src.models import UserProfile, UserCardSettings, FeatureToggleSettings, UserDashboardSettings
9-
from src.utils import render_template_from_file
10+
from src.utils import render_template_from_file, ROOT_DIR
1011
from src.routes.helper import get_email_addresses
1112

1213
auth_bp = blueprints.Blueprint('auth', __name__)
@@ -37,14 +38,20 @@ def login():
3738
admin_email_address.remove(receiver_email)
3839
if admin_email_address:
3940
context = {"username": current_user.username, "login_time": datetime.datetime.now()}
40-
login_body = render_template_from_file("src/templates/email_templates/admin_login_alert.html", **context)
41-
send_smpt_email(admin_email_address, 'Login Alert', login_body, is_html=True)
41+
42+
login_alert_template = os.path.join(ROOT_DIR, "src/templates/email_templates/admin_login_alert.html")
43+
html_body = render_template_from_file(login_alert_template, **context)
44+
45+
send_smpt_email(admin_email_address, 'Login Alert', html_body, is_html=True)
4246

4347
# log in alert to user
4448
if receiver_email:
4549
context = {"username": current_user.username, "login_time": datetime.datetime.now()}
46-
login_body = render_template_from_file("src/templates/email_templates/login.html", **context)
47-
send_smpt_email(receiver_email, 'Login Alert', login_body, is_html=True)
50+
51+
login_message_template = os.path.join(ROOT_DIR, "src/templates/email_templates/login.html")
52+
html_body = render_template_from_file(login_message_template, **context)
53+
54+
send_smpt_email(receiver_email, 'Login Alert', html_body, is_html=True)
4855
return redirect(url_for('dashboard'))
4956
flash('Invalid username or password', 'danger')
5057
return render_template('login.html')
@@ -54,8 +61,9 @@ def logout():
5461
receiver_email = current_user.email
5562
if receiver_email:
5663
context = {"username": current_user.username}
57-
logout_body = render_template_from_file("src/templates/email_templates/logout.html", **context)
58-
send_smpt_email(receiver_email, 'Logout Alert', logout_body, is_html=True)
64+
logout_message_template = os.path.join(ROOT_DIR, "src/templates/email_templates/logout.html")
65+
html_body = render_template_from_file(logout_message_template, **context)
66+
send_smpt_email(receiver_email, 'Logout Alert', html_body, is_html=True)
5967
logout_user()
6068
return redirect(url_for('login'))
6169

@@ -97,7 +105,8 @@ def signup():
97105
"signup_time": datetime.datetime.now(),
98106
"user_level": new_user.user_level
99107
}
100-
html_body = render_template_from_file("src/templates/email_templates/new_user_alert.html", **context)
108+
new_user_alert_template = os.path.join(ROOT_DIR, "src/templates/email_templates/new_user_alert.html")
109+
html_body = render_template_from_file(new_user_alert_template, **context)
101110
send_smpt_email(admin_email_address, subject, html_body, is_html=True)
102111

103112
# Send email to the new user
@@ -106,7 +115,8 @@ def signup():
106115
"username": new_user.username,
107116
"email": new_user.email,
108117
}
109-
html_body = render_template_from_file("src/templates/email_templates/welcome.html", **context)
118+
welcome_template = os.path.join(ROOT_DIR, "src/templates/email_templates/welcome.html")
119+
html_body = render_template_from_file(welcome_template, **context)
110120
send_smpt_email(email, subject, html_body, is_html=True)
111121

112122
db.session.add(new_user)

src/routes/process.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask import request, render_template, redirect, url_for, flash, session, blueprints
33
from flask_login import login_required, current_user
44
from src.config import app
5-
from src.utils import get_top_processes, render_template_from_file
5+
from src.utils import get_top_processes, render_template_from_file, ROOT_DIR
66
from src.models import FeatureToggleSettings
77
from src.scripts.email_me import send_smpt_email
88

@@ -35,7 +35,8 @@ def process():
3535
receiver_email = current_user.email
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}
38-
html_body = render_template_from_file("src/templates/email_templates/process_killed.html", **context)
38+
process_killed_template = os.path.join(ROOT_DIR, "src/templates/email_templates/process_killed.html")
39+
html_body = render_template_from_file(process_killed_template, **context)
3940
send_smpt_email(receiver_email, subject, html_body, is_html=True)
4041
except Exception as e:
4142
flash(f"Failed to kill process '{process_name}' (PID {pid_to_kill}). Error: {e}", "danger")

src/routes/settings.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import os
12
import datetime
23
from flask import render_template, request, flash, blueprints, redirect, url_for
34

45
from src.config import app, db
56
from src.models import UserCardSettings, UserDashboardSettings, UserProfile, ApplicationGeneralSettings, FeatureToggleSettings
67
from flask_login import login_required, current_user
7-
from src.utils import render_template_from_file
8+
from src.utils import render_template_from_file, ROOT_DIR
89
from src.scripts.email_me import send_smpt_email
910

1011
settings_bp = blueprints.Blueprint("settings", __name__)
@@ -37,8 +38,8 @@ def general_settings():
3738
"notifications_enabled": general_settings.enable_alerts,
3839
"current_user": current_user.username
3940
}
40-
html_body = render_template_from_file("src/templates/email_templates/notification_alert.html", **context)
41-
print("Notification enabled:", general_settings.enable_alerts)
41+
notficiation_alert_template = os.path.join(ROOT_DIR, "src/templates/email_templates/notification_alert.html")
42+
html_body = render_template_from_file(notficiation_alert_template, **context)
4243
send_smpt_email(admin_emails, subject, html_body, is_html=True, bypass_alerts=True)
4344
db.session.commit()
4445
flash('General settings updated successfully!', 'success')

src/routes/speedtest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import os
12
import datetime
23
from flask import render_template, Blueprint
34
from flask_login import login_required, current_user
45
from src.config import app, db
56
from src.models import UserDashboardSettings, NetworkSpeedTestResult
6-
from src.utils import run_speedtest, render_template_from_file
7+
from src.utils import run_speedtest, render_template_from_file, ROOT_DIR
78
from src.scripts.email_me import send_smpt_email
89

910
speedtest_bp = Blueprint("speedtest", __name__)
@@ -43,8 +44,11 @@ def speedtest():
4344
receiver_email = current_user.email
4445
subject = "Speedtest Result"
4546
context = {"speedtest_result": current_speedtest_result}
46-
body = render_template_from_file("src/templates/email_templates/speedtest_result.html", **context)
47-
send_smpt_email(receiver_email, subject, body, is_html=True)
47+
speedtest_result_template = os.path.join(
48+
ROOT_DIR, "src/templates/email_templates/speedtest_result.html"
49+
)
50+
html_body = render_template_from_file(speedtest_result_template, **context)
51+
send_smpt_email(receiver_email, subject, html_body, is_html=True)
4852

4953
return render_template(
5054
"speedtest_result.html",

src/routes/user.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import os
12
import datetime
23
from flask import render_template, redirect, url_for, request, blueprints, flash, blueprints
34
from flask_login import login_required, current_user
45
from werkzeug.security import generate_password_hash
56

67
from src.config import app, db
78
from src.models import UserProfile, UserDashboardSettings, UserCardSettings, FeatureToggleSettings
8-
from src.utils import render_template_from_file
9+
from src.utils import render_template_from_file, ROOT_DIR
910
from src.scripts.email_me import send_smpt_email
1011
from src.routes.helper import get_email_addresses
1112

@@ -48,7 +49,8 @@ def add_user():
4849
"signup_time": datetime.datetime.now(),
4950
"user_level": new_user.user_level
5051
}
51-
html_body = render_template_from_file("src/templates/email_templates/new_user_create.html", **context)
52+
new_user_alert_template = os.path.join(ROOT_DIR, "src/templates/email_templates/new_user_create.html")
53+
html_body = render_template_from_file(new_user_alert_template, **context)
5254
send_smpt_email(admin_email_address, subject, html_body, is_html=True)
5355

5456
# Send welcome email to new user
@@ -57,7 +59,8 @@ def add_user():
5759
"username": new_user.username,
5860
"email": new_user.email,
5961
}
60-
html_body = render_template_from_file("src/templates/email_templates/welcome.html", **context)
62+
welcome_email_template = os.path.join(ROOT_DIR, "src/templates/email_templates/welcome.html")
63+
html_body = render_template_from_file(welcome_email_template, **context)
6164
send_smpt_email(email, subject, html_body, is_html=True)
6265

6366
# Add and commit the new user to get the correct user ID
@@ -131,7 +134,8 @@ def delete_user(username):
131134
"deletion_time": datetime.datetime.now(),
132135
"current_user": current_user.username,
133136
}
134-
html_body = render_template_from_file("src/templates/email_templates/deletion_email.html", **context)
137+
deletion_email_template = os.path.join(ROOT_DIR, "src/templates/email_templates/deletion_email.html")
138+
html_body = render_template_from_file(deletion_email_template, **context)
135139
send_smpt_email(admin_email_address, subject, html_body, is_html=True)
136140

137141
db.session.delete(user)

0 commit comments

Comments
 (0)