From 82f1eed554641141628cf77fa94c8c207b5de105 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 13 Feb 2024 23:15:32 +0000 Subject: [PATCH] REFACTOR: Changes all raw url's to url_fors --- controllers/web/endpoints.py | 4 ++-- controllers/web/product.py | 34 +++++++++++++++++----------------- controllers/web/stats.py | 6 +++--- controllers/web/user.py | 20 ++++++++++---------- templates/admin.html | 10 ++-------- templates/content.html | 4 ++-- templates/header.html | 22 +++++++++++----------- templates/index.html | 6 +++--- templates/login.html | 6 +++--- templates/new_product.html | 6 +++--- templates/product.html | 10 +++++----- templates/signup.html | 6 +++--- 12 files changed, 64 insertions(+), 70 deletions(-) diff --git a/controllers/web/endpoints.py b/controllers/web/endpoints.py index 8cdb2ed..49650ab 100644 --- a/controllers/web/endpoints.py +++ b/controllers/web/endpoints.py @@ -1,7 +1,7 @@ from models.users.user import User from controllers.database.user import UserController -from flask import redirect, Blueprint, session +from flask import redirect, Blueprint, session, url_for from . import user from . import product @@ -35,4 +35,4 @@ def get_user() -> dict[User | None]: # Function responsible for displaying the main landing page of the site @blueprint.route('/') def index(): - return redirect("/products") + return redirect(url_for('main.products.index')) diff --git a/controllers/web/product.py b/controllers/web/product.py index 58e3100..5a9bb83 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -3,7 +3,7 @@ categories and image processing. """ -from flask import render_template, session, flash, request, redirect, Blueprint +from flask import render_template, session, flash, request, redirect, Blueprint, url_for from models.products.product import Product from models.stats import Stats @@ -91,7 +91,7 @@ def category(category: str): @blueprint.route('/') -def id(id: int): +def product(id: int): """ Loads a given product based on ID """ db = ProductController() product = db.read_id(id) @@ -99,7 +99,7 @@ def id(id: int): # Check that a valid product was returned if product is None: flash(f"No Product available with id {id}", "warning") - return redirect("/") + return redirect(url_for('main.index')) # Record a view on the product db = StatsController() @@ -115,18 +115,18 @@ def id(id: int): @blueprint.route('/add') -def display_add_product(): +def display_add(): """ Launches the page to add a new product to the site """ # User needs to be logged in as a seller to view this page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) return render_template('index.html', content='new_product.html') @blueprint.post('/add') -def add_product(): +def add(): """ Server site processing to handle a request to add a new product to the site """ @@ -135,7 +135,7 @@ def add_product(): # User needs to be logged in as a seller to view this page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) file = request.files.get('image') image_filename = save_image(file) @@ -158,7 +158,7 @@ def add_product(): @blueprint.post('/update/') -def update_product(id: int): +def update(id: int): """ Processes a request to update a product in place on the site """ # Ensure that the product belongs to the current user user_id = session.get('user_id') @@ -166,14 +166,14 @@ def update_product(id: int): # User needs to be logged in as a seller to view this page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) db = ProductController() product = db.read_id(id) if product.sellerID != user_id: flash("This product does not belong to you!", "error") - return redirect("/products/ownproducts") + return redirect(url_for('main.products.own')) # Save new image file file = request.files.get('image') @@ -192,11 +192,11 @@ def update_product(id: int): db.update(product) flash("Product successfully updated", 'notice') - return redirect(f"/products/{product.id}") + return redirect(url_for('main.products.product', id=product.id)) @blueprint.post('/delete/') -def delete_product(id: int): +def delete(id: int): """ Processes a request to delete a product in place on the site """ # Ensure that the product belongs to the current user user_id = session.get('user_id') @@ -204,29 +204,29 @@ def delete_product(id: int): # User needs to be logged in as a seller to view this page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) db = ProductController() product = db.read_id(id) if product.sellerID != user_id: flash("This product does not belong to you!", "error") - return redirect("/products/ownproducts") + return redirect(url_for('main.products.display_own')) db.delete(id) flash("Product Removed!", "success") - return redirect("/products/ownproducts") + return redirect(url_for('main.products.display_own')) @blueprint.route('/ownproducts') -def display_own_products(): +def display_own(): """ Display products owned by the currently logged in seller """ user_id = session.get('user_id') # User must be logged in as seller to view page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) db = ProductController() products = db.read_user(user_id) diff --git a/controllers/web/stats.py b/controllers/web/stats.py index 8e9a3eb..96d04bf 100644 --- a/controllers/web/stats.py +++ b/controllers/web/stats.py @@ -3,7 +3,7 @@ """ from flask import Blueprint -from flask import render_template, request, flash, session, redirect +from flask import render_template, request, flash, session, redirect, url_for from controllers.database.stats import StatsController from controllers.database.product import ProductController from controllers.database.category import CategoryController @@ -31,7 +31,7 @@ def view_product_stats(id: int): # Check user is seller if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect("/") + return redirect(url_for('main.index')) db = ProductController() product = db.read_id(id) @@ -39,7 +39,7 @@ def view_product_stats(id: int): # Check user owns this product if product is None or product.sellerID is not session.get("user_id"): flash("This product does not belong to you!", "error") - return redirect("/products/ownproducts") + return redirect(url_for('main.products.display_own')) db = StatsController() diff --git a/controllers/web/user.py b/controllers/web/user.py index 8bffa53..45483f6 100644 --- a/controllers/web/user.py +++ b/controllers/web/user.py @@ -3,7 +3,7 @@ """ from flask import Blueprint -from flask import render_template, redirect, request, session, flash +from flask import render_template, redirect, request, session, flash, url_for from controllers.database.user import UserController from models.users.customer import Customer from models.users.seller import Seller @@ -11,7 +11,7 @@ from utils.user_utils import is_role from hashlib import sha512 # Blueprint to append user endpoints to -blueprint = Blueprint("users", __name__) +blueprint = Blueprint("users", __name__, url_prefix="/users") # LOGIN FUNCTIONALITY @@ -32,16 +32,16 @@ def login(): if user is None: error = "No user found with the username " + request.form['username'] flash(error, 'warning') - return redirect("/login") + return redirect(url_for('main.users.login')) # Incorrect Password if sha512(request.form['password'].encode()).hexdigest() != user.password: error = "Incorrect Password" flash(error, 'warning') - return redirect("/login") + return redirect(url_for('main.users.login')) session['user_id'] = user.id - return redirect("/") + return redirect(url_for('main.index')) # SIGNUP FUNCTIONALITY @@ -60,7 +60,7 @@ def signup(): if database.read(request.form['username']) is not None: error = "User, " + request.form['username'] + " already exists" flash(error, 'warning') - return redirect("/signup") + return redirect(url_for('main.users.signup')) # Signup as Seller or Customer if request.form.get('seller'): @@ -87,7 +87,7 @@ def signup(): database.create(user) # Code 307 Preserves the original request (POST) - return redirect("/login", code=307) + return redirect(url_for('main.users.login'), code=307) # SIGN OUT FUNCTIONALITY @@ -96,7 +96,7 @@ def logout(): """ Function responsible for handling logouts from the site """ # Clear the current user from the session if they are logged in session.pop('user_id', None) - return redirect("/") + return redirect(url_for('main.index')) # DELETE USER FUNCTIONALITY @@ -105,8 +105,8 @@ def delete(id: int): """ Function responsible for deleting users from the site """ if not is_role("Admin"): flash("You must be logged in an admin to remove users!", "error") - return redirect("/") + return redirect(url_for('main.index')) db = UserController() db.delete(id) - return redirect("/admin/users/") + return redirect(url_for('main.admin.users')) diff --git a/templates/admin.html b/templates/admin.html index 09eb90c..154bce0 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -1,5 +1,5 @@ - + {% if users != None %} @@ -52,19 +52,13 @@

Confirm Delete

Are you sure you want to delete {{user.role}} {{user.username}}

-
+
- {% endfor %} diff --git a/templates/content.html b/templates/content.html index 081736a..cebc1bd 100644 --- a/templates/content.html +++ b/templates/content.html @@ -4,10 +4,10 @@

Showing results for {{products|count}} products

{% for product in products %} - +
{{product.name}}
- Brake Disks + Brake Disks
£{{product.cost}}
diff --git a/templates/header.html b/templates/header.html index eb998a0..75e4cd9 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,16 +1,16 @@
diff --git a/templates/index.html b/templates/index.html index d5cf321..dc539c0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,9 +3,9 @@ - - - + + + diff --git a/templates/login.html b/templates/login.html index fdb5b57..686452e 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,8 +1,8 @@ - +

Login

-
+
@@ -17,6 +17,6 @@
-

Not a member? Create Account

+

Not a member? Create Account

diff --git a/templates/new_product.html b/templates/new_product.html index 70b7e44..0681781 100644 --- a/templates/new_product.html +++ b/templates/new_product.html @@ -1,8 +1,8 @@ - +

Create New Product

-
+
@@ -31,6 +31,6 @@
-

Want to view all of your products? Click Here

+

Want to view all of your products? Click Here

diff --git a/templates/product.html b/templates/product.html index f66018d..4c57ee3 100644 --- a/templates/product.html +++ b/templates/product.html @@ -4,8 +4,8 @@ {% if product != None %} {% if user.id == product.sellerID %} -
- Brake Disks + + Brake Disks