|
| 1 | +import datetime |
1 | 2 | from flask import Flask, render_template, redirect, url_for, request, blueprints, flash |
2 | 3 | from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user |
3 | 4 | from flask_sqlalchemy import SQLAlchemy |
|
11 | 12 |
|
12 | 13 | from src.config import app, db |
13 | 14 | from src.models import User, SmptEamilPasswordConfig, DashboardSettings |
14 | | -from src.utils import read_html_file |
| 15 | +from src.utils import render_template_from_file |
15 | 16 |
|
16 | 17 | auth_bp = blueprints.Blueprint('auth', __name__) |
17 | 18 |
|
@@ -67,16 +68,35 @@ def login(): |
67 | 68 | user = User.query.filter_by(username=username).first() |
68 | 69 | if user and check_password_hash(user.password, password): |
69 | 70 | login_user(user) |
70 | | - # Get Admin Emails with Alerts Enabled: |
| 71 | + receiver_email = current_user.email |
71 | 72 | admin_email_address = get_email_addresses(user_level='admin', receive_email_alerts=True) |
| 73 | + # if receiver_email in admin_email_address don't send email to the admin |
| 74 | + # log in alert to admin |
| 75 | + |
72 | 76 | if admin_email_address: |
73 | | - login_body = read_html_file("src/templates/email_templates/login.html") |
74 | | - send_email(admin_email_address, 'Login Alert', login_body, is_html=True) |
75 | | - |
| 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) |
| 80 | + |
| 81 | + # log in alert to user |
| 82 | + if receiver_email: |
| 83 | + context = {"username": current_user.username, "login_time": datetime.datetime.now()} |
| 84 | + 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) |
76 | 86 | return redirect(url_for('dashboard')) |
77 | 87 | flash('Invalid username or password', 'danger') |
78 | 88 | return render_template('login.html') |
79 | 89 |
|
| 90 | +@app.route('/logout') |
| 91 | +def logout(): |
| 92 | + receiver_email = current_user.email |
| 93 | + if receiver_email: |
| 94 | + context = {"username": current_user.username} |
| 95 | + 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) |
| 97 | + logout_user() |
| 98 | + return redirect(url_for('login')) |
| 99 | + |
80 | 100 | @app.route('/signup', methods=['GET', 'POST']) |
81 | 101 | def signup(): |
82 | 102 | if request.method == 'POST': |
@@ -119,15 +139,6 @@ def protected(): |
119 | 139 | return f'Hello, Admin {current_user.username}! This is a protected page.' |
120 | 140 | return f'Hello, {current_user.username}! This is a protected page.' |
121 | 141 |
|
122 | | -@app.route('/logout') |
123 | | -def logout(): |
124 | | - receiver_email = get_email_addresses(user_level='admin', receive_email_alerts=True) |
125 | | - if receiver_email: |
126 | | - logout_body = read_html_file("src/templates/email_templates/logout.html") |
127 | | - send_email(receiver_email, 'Logout Alert', logout_body, is_html=True) |
128 | | - logout_user() |
129 | | - return redirect(url_for('login')) |
130 | | - |
131 | 142 | @app.route('/users') |
132 | 143 | @login_required |
133 | 144 | def view_users(): |
|
0 commit comments