From c487ea8943c3362cbd1f0d1018d78998a39ff680 Mon Sep 17 00:00:00 2001
From: Ethan-clay03 <ethanclay2017@gmail.com>
Date: Wed, 29 Jan 2025 14:13:45 +0000
Subject: [PATCH] Fix error and success messages across application

---
 app/admin/routes.py                         |  7 +++--
 app/profile/routes.py                       | 31 +++++++++++----------
 app/templates/base.html                     | 23 ++++++++++++++-
 app/templates/bookings/index.html           |  5 ----
 app/templates/profile/login.html            |  9 +-----
 app/templates/profile/password-reset-2.html |  5 ----
 app/templates/profile/password-reset-3.html |  5 ----
 app/templates/profile/password-reset.html   |  5 ----
 8 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/app/admin/routes.py b/app/admin/routes.py
index 770b651..e0d40f0 100644
--- a/app/admin/routes.py
+++ b/app/admin/routes.py
@@ -1,4 +1,4 @@
-from flask import render_template, redirect, url_for, request, jsonify, current_app
+from flask import render_template, redirect, url_for, request, jsonify, flash
 from app import db
 from app import admin_permission, permission_required, super_admin_permission
 from app.models import Listings, ListingImages
@@ -108,6 +108,7 @@ def update_booking(id):
             )
         
     locations = Listings.get_all_locations()
+    flash('Successfully updated booking', 'success')
     return redirect(url_for('admin.manage_bookings'))
 
 
@@ -154,7 +155,7 @@ def get_bookings():
             'transport_type': listing.transport_type
         } for listing in filtered_data
     ]
-
+    
     return jsonify(result)
 
 @bp.route('delete_booking', methods=['DELETE'])
@@ -167,4 +168,4 @@ def delete_booking():
     if success:
         http_code = 200
 
-    return jsonify(success), http_code
\ No newline at end of file
+    return jsonify(success), http_code
diff --git a/app/profile/routes.py b/app/profile/routes.py
index f6a5ebb..0c534e7 100644
--- a/app/profile/routes.py
+++ b/app/profile/routes.py
@@ -20,7 +20,7 @@ def signup():
         # Validate form data
         error = validate_signup_form(form_data)
         if error:
-            flash(error)
+            flash(error, 'error')
             return redirect(url_for('profile.signup'))
 
         # Create new user and log in automatically
@@ -37,7 +37,7 @@ def signup():
             return redirect(url_for('profile.index'))
         except Exception as e:
             auth_logger.error(f"Unable to create user: {e}")
-            flash('An error occurred. Please try again.')
+            flash('An error occurred. Please try again.', 'error')
             return redirect(url_for('profile.signup'))
 
     return render_template('profile/signup.html')
@@ -76,8 +76,8 @@ def login_post():
         user = User.search_user_by_username(username_field)
 
     if not user or not check_password_hash(user.password, password):
-        flash('Please check your login details and try again.')
-        return redirect(url_for('profile.login', error=True))
+        flash('Invalid username/email and or password. Please try again or <a href="' + url_for('profile.password_reset') + '">reset your password here</a>.', 'danger')
+        return redirect(url_for('profile.login'))
 
     login_user(user, remember=remember)
 
@@ -130,9 +130,9 @@ def logout():
 def login():
     if current_user.is_authenticated:
         return redirect(url_for('profile.index'))
-    #Check if login is being sent due to error
-    error = request.args.get('error')
-    return render_template('profile/login.html', error=error)
+
+    #user not logged in
+    return render_template('profile/login.html')
 
 
 @bp.route('/password-reset')
@@ -148,11 +148,11 @@ def check_password_reset_1():
     #Search to see if username already exists
     user_exist = User.search_user_by_username(username)
     if user_exist is None:
-        return flash('Username does not exist')
+        return flash('Username does not exist', 'error')
     
     email_exist = User.search_user_by_email(email)
     if email_exist is None:
-        return flash('Email does not exist')
+        return flash('Email does not exist', 'error')
 
     session['password-reset-email'] = email
     return redirect(url_for('profile.password_reset_2'))
@@ -171,7 +171,7 @@ def check_password_reset_2():
     if code == '123456' or code == '234567':
         return redirect(url_for('profile.password_reset_3'))
     
-    return flash('Invalid 2FA Code')
+    return flash('Invalid 2FA Code', 'error')
 
 
 @bp.route('/password-reset/reset-password')
@@ -181,14 +181,17 @@ def password_reset_3():
 
 @bp.route('/password-reset/reset-password', methods=['POST'])
 def password_reset_process():
-    email = session.get('email')
-    password1 = session.get('password-1')
-    password2 = session.get('password-2')
+    email = session.get('password-reset-email')
+    password1 = request.form.get('password-1')
+    password2 = request.form.get('password-2')
     
     #Simulate 2FA code being entered
     if password1 == password2:
         User.change_user_password(email, password1)
-        return redirect(url_for('profile.password_reset_3'))
+        flash('Password was updated successfully', 'success')
+        return redirect(url_for('profile.login'))
+    flash('Passwords must match', 'error')
+    return redirect(url_for('profile.password_reset_3'))
 
 @login_required
 @bp.route('/home')
diff --git a/app/templates/base.html b/app/templates/base.html
index 76cdacc..72c4c33 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -67,8 +67,29 @@
             </div>
             {% endblock %}
             <div class="content">
+                {% with messages = get_flashed_messages(with_categories=true) %}
+                    {% if messages %}
+                        {% for category, message in messages %}
+                            <div class="row justify-content-center" style="margin-top: 25px">
+                                <div class="col-md-8">
+                                    {% if category == 'error' %} <!-- Bootstrap calls error, danger. Added if to handle case  -->
+                                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
+                                            {{ message | safe }}
+                                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
+                                        </div>
+                                    {% else %}
+                                        <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
+                                            {{ message | safe }}
+                                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
+                                        </div>
+                                    {% endif %}
+                                </div>
+                            </div>
+                        {% endfor %}
+                    {% endif %}
+                {% endwith %}
                 {% block content %}{% endblock %}
-            </div>
+            </div>                
         </div>
         {% block footer %}
         <footer>
diff --git a/app/templates/bookings/index.html b/app/templates/bookings/index.html
index 0dbd92f..511b121 100644
--- a/app/templates/bookings/index.html
+++ b/app/templates/bookings/index.html
@@ -10,11 +10,6 @@
     <div class="deals_text"><span class="deals_underline">Deals for you</span></div>
     <div class="swiffy-slider slider-item-show2 slider-item-reveal slider-nav-outside slider-nav-round slider-nav-visible slider-indicators-outside slider-indicators-round slider-indicators-dark slider-nav-animation slider-nav-animation-fadein slider-item-first-visible">
         <ul class="slider-container py-4">
-            {% if get_flashed_messages() %}
-            <div class="alert alert-danger" role="alert">
-                get_flashed_messages()
-            </div>
-            {% endif %}
             {% for listing in top_listings %}
             <li class="slide-visible">
                 <div class="card shadow h-100">
diff --git a/app/templates/profile/login.html b/app/templates/profile/login.html
index 7bc4f19..8425a68 100644
--- a/app/templates/profile/login.html
+++ b/app/templates/profile/login.html
@@ -1,13 +1,6 @@
 {% extends 'base.html' %}
 {% block content %}
 <div class="column is-4 is-offset-4">
-
-{% if get_flashed_messages() %}
-<div class="alert alert-danger" role="alert">
-    Invalid username/email and or password. Please try again or 
-    <a href="{{ url_for('profile.password_reset') }}">reset your password here</a>.
-</div>
-{% endif %}
     <div id="login-box" class="form_box_30" style="margin-top: 30px;">
         <div class="profile_form_background">
             <h2 class="form_header">Login</h2>
@@ -22,7 +15,7 @@
                     <input type="password" class="form-control" name="password">
                 </div>
                 <div class="mb-3">
-                    <a class="clear-hyperlink" href="{{ url_for('profile.login_post') }}">Forgot Password?</a>
+                    <a class="clear-hyperlink" href="{{ url_for('profile.password_reset') }}">Forgot Password?</a>
                 </div>
                 <div class="mb-3 form-check">
                     <input type="checkbox" class="form-check-input" id="remember" name="remember">
diff --git a/app/templates/profile/password-reset-2.html b/app/templates/profile/password-reset-2.html
index b2e8a1b..1cfde74 100644
--- a/app/templates/profile/password-reset-2.html
+++ b/app/templates/profile/password-reset-2.html
@@ -2,11 +2,6 @@
 
 {% block content %}
 <div class="form_box_30 background_1">
-    {% if get_flashed_messages() %}
-    <div class="alert alert-danger" role="alert">
-        get_flashed_messages()
-    </div>
-    {% endif %}
     <div class="profile_form_background">
         <h3 class="form_header">2FA Code</h3>
         <form method="POST" action="{{ url_for('profile.check_password_reset_2') }}">
diff --git a/app/templates/profile/password-reset-3.html b/app/templates/profile/password-reset-3.html
index e24c164..353dd8c 100644
--- a/app/templates/profile/password-reset-3.html
+++ b/app/templates/profile/password-reset-3.html
@@ -2,11 +2,6 @@
 
 {% block content %}
 <div class="form_box_30 background_1">
-    {% if get_flashed_messages() %}
-    <div class="alert alert-danger" role="alert">
-        get_flashed_messages()
-    </div>
-    {% endif %}
     <div class="profile_form_background">
         <h3 class="form_header">Password Reset</h3>
         <form method="POST" action="{{ url_for('profile.password_reset_process') }}">
diff --git a/app/templates/profile/password-reset.html b/app/templates/profile/password-reset.html
index 589fd05..14c86e6 100644
--- a/app/templates/profile/password-reset.html
+++ b/app/templates/profile/password-reset.html
@@ -2,11 +2,6 @@
 
 {% block content %}
 <div class="form_box_30 background_1">
-    {% if get_flashed_messages() %}
-    <div class="alert alert-danger" role="alert">
-        get_flashed_messages()
-    </div>
-    {% endif %}
     <div class="profile_form_background">
         <h3 class="form_header">Password Reset</h3>
         <form method="POST" action="{{ url_for('profile.check_password_reset_1') }}">
-- 
GitLab