REFACTOR: Changes all raw url's to url_fors

This commit is contained in:
Luke Else 2024-02-13 23:15:32 +00:00
parent 3dc7a1f861
commit 82f1eed554
12 changed files with 64 additions and 70 deletions

View File

@ -1,7 +1,7 @@
from models.users.user import User from models.users.user import User
from controllers.database.user import UserController 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 user
from . import product from . import product
@ -35,4 +35,4 @@ def get_user() -> dict[User | None]:
# Function responsible for displaying the main landing page of the site # Function responsible for displaying the main landing page of the site
@blueprint.route('/') @blueprint.route('/')
def index(): def index():
return redirect("/products") return redirect(url_for('main.products.index'))

View File

@ -3,7 +3,7 @@
categories and image processing. 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.products.product import Product
from models.stats import Stats from models.stats import Stats
@ -91,7 +91,7 @@ def category(category: str):
@blueprint.route('/<int:id>') @blueprint.route('/<int:id>')
def id(id: int): def product(id: int):
""" Loads a given product based on ID """ """ Loads a given product based on ID """
db = ProductController() db = ProductController()
product = db.read_id(id) product = db.read_id(id)
@ -99,7 +99,7 @@ def id(id: int):
# Check that a valid product was returned # Check that a valid product was returned
if product is None: if product is None:
flash(f"No Product available with id {id}", "warning") flash(f"No Product available with id {id}", "warning")
return redirect("/") return redirect(url_for('main.index'))
# Record a view on the product # Record a view on the product
db = StatsController() db = StatsController()
@ -115,18 +115,18 @@ def id(id: int):
@blueprint.route('/add') @blueprint.route('/add')
def display_add_product(): def display_add():
""" Launches the page to add a new product to the site """ """ Launches the page to add a new product to the site """
# User needs to be logged in as a seller to view this page # User needs to be logged in as a seller to view this page
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") 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') return render_template('index.html', content='new_product.html')
@blueprint.post('/add') @blueprint.post('/add')
def add_product(): def add():
""" Server site processing to handle a request to add a """ Server site processing to handle a request to add a
new product to the site 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 # User needs to be logged in as a seller to view this page
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") 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') file = request.files.get('image')
image_filename = save_image(file) image_filename = save_image(file)
@ -158,7 +158,7 @@ def add_product():
@blueprint.post('/update/<int:id>') @blueprint.post('/update/<int:id>')
def update_product(id: int): def update(id: int):
""" Processes a request to update a product in place on the site """ """ Processes a request to update a product in place on the site """
# Ensure that the product belongs to the current user # Ensure that the product belongs to the current user
user_id = session.get('user_id') 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 # User needs to be logged in as a seller to view this page
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") flash("You must be logged in as a seller to view this page!", "error")
return redirect("/") return redirect(url_for('main.index'))
db = ProductController() db = ProductController()
product = db.read_id(id) product = db.read_id(id)
if product.sellerID != user_id: if product.sellerID != user_id:
flash("This product does not belong to you!", "error") flash("This product does not belong to you!", "error")
return redirect("/products/ownproducts") return redirect(url_for('main.products.own'))
# Save new image file # Save new image file
file = request.files.get('image') file = request.files.get('image')
@ -192,11 +192,11 @@ def update_product(id: int):
db.update(product) db.update(product)
flash("Product successfully updated", 'notice') flash("Product successfully updated", 'notice')
return redirect(f"/products/{product.id}") return redirect(url_for('main.products.product', id=product.id))
@blueprint.post('/delete/<int:id>') @blueprint.post('/delete/<int:id>')
def delete_product(id: int): def delete(id: int):
""" Processes a request to delete a product in place on the site """ """ Processes a request to delete a product in place on the site """
# Ensure that the product belongs to the current user # Ensure that the product belongs to the current user
user_id = session.get('user_id') 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 # User needs to be logged in as a seller to view this page
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") flash("You must be logged in as a seller to view this page!", "error")
return redirect("/") return redirect(url_for('main.index'))
db = ProductController() db = ProductController()
product = db.read_id(id) product = db.read_id(id)
if product.sellerID != user_id: if product.sellerID != user_id:
flash("This product does not belong to you!", "error") flash("This product does not belong to you!", "error")
return redirect("/products/ownproducts") return redirect(url_for('main.products.display_own'))
db.delete(id) db.delete(id)
flash("Product Removed!", "success") flash("Product Removed!", "success")
return redirect("/products/ownproducts") return redirect(url_for('main.products.display_own'))
@blueprint.route('/ownproducts') @blueprint.route('/ownproducts')
def display_own_products(): def display_own():
""" Display products owned by the currently logged in seller """ """ Display products owned by the currently logged in seller """
user_id = session.get('user_id') user_id = session.get('user_id')
# User must be logged in as seller to view page # User must be logged in as seller to view page
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") flash("You must be logged in as a seller to view this page!", "error")
return redirect("/") return redirect(url_for('main.index'))
db = ProductController() db = ProductController()
products = db.read_user(user_id) products = db.read_user(user_id)

View File

@ -3,7 +3,7 @@
""" """
from flask import Blueprint 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.stats import StatsController
from controllers.database.product import ProductController from controllers.database.product import ProductController
from controllers.database.category import CategoryController from controllers.database.category import CategoryController
@ -31,7 +31,7 @@ def view_product_stats(id: int):
# Check user is seller # Check user is seller
if not is_role("Seller"): if not is_role("Seller"):
flash("You must be logged in as a seller to view this page!", "error") flash("You must be logged in as a seller to view this page!", "error")
return redirect("/") return redirect(url_for('main.index'))
db = ProductController() db = ProductController()
product = db.read_id(id) product = db.read_id(id)
@ -39,7 +39,7 @@ def view_product_stats(id: int):
# Check user owns this product # Check user owns this product
if product is None or product.sellerID is not session.get("user_id"): if product is None or product.sellerID is not session.get("user_id"):
flash("This product does not belong to you!", "error") flash("This product does not belong to you!", "error")
return redirect("/products/ownproducts") return redirect(url_for('main.products.display_own'))
db = StatsController() db = StatsController()

View File

@ -3,7 +3,7 @@
""" """
from flask import Blueprint 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 controllers.database.user import UserController
from models.users.customer import Customer from models.users.customer import Customer
from models.users.seller import Seller from models.users.seller import Seller
@ -11,7 +11,7 @@ from utils.user_utils import is_role
from hashlib import sha512 from hashlib import sha512
# Blueprint to append user endpoints to # Blueprint to append user endpoints to
blueprint = Blueprint("users", __name__) blueprint = Blueprint("users", __name__, url_prefix="/users")
# LOGIN FUNCTIONALITY # LOGIN FUNCTIONALITY
@ -32,16 +32,16 @@ def login():
if user is None: if user is None:
error = "No user found with the username " + request.form['username'] error = "No user found with the username " + request.form['username']
flash(error, 'warning') flash(error, 'warning')
return redirect("/login") return redirect(url_for('main.users.login'))
# Incorrect Password # Incorrect Password
if sha512(request.form['password'].encode()).hexdigest() != user.password: if sha512(request.form['password'].encode()).hexdigest() != user.password:
error = "Incorrect Password" error = "Incorrect Password"
flash(error, 'warning') flash(error, 'warning')
return redirect("/login") return redirect(url_for('main.users.login'))
session['user_id'] = user.id session['user_id'] = user.id
return redirect("/") return redirect(url_for('main.index'))
# SIGNUP FUNCTIONALITY # SIGNUP FUNCTIONALITY
@ -60,7 +60,7 @@ def signup():
if database.read(request.form['username']) is not None: if database.read(request.form['username']) is not None:
error = "User, " + request.form['username'] + " already exists" error = "User, " + request.form['username'] + " already exists"
flash(error, 'warning') flash(error, 'warning')
return redirect("/signup") return redirect(url_for('main.users.signup'))
# Signup as Seller or Customer # Signup as Seller or Customer
if request.form.get('seller'): if request.form.get('seller'):
@ -87,7 +87,7 @@ def signup():
database.create(user) database.create(user)
# Code 307 Preserves the original request (POST) # Code 307 Preserves the original request (POST)
return redirect("/login", code=307) return redirect(url_for('main.users.login'), code=307)
# SIGN OUT FUNCTIONALITY # SIGN OUT FUNCTIONALITY
@ -96,7 +96,7 @@ def logout():
""" Function responsible for handling logouts from the site """ """ Function responsible for handling logouts from the site """
# Clear the current user from the session if they are logged in # Clear the current user from the session if they are logged in
session.pop('user_id', None) session.pop('user_id', None)
return redirect("/") return redirect(url_for('main.index'))
# DELETE USER FUNCTIONALITY # DELETE USER FUNCTIONALITY
@ -105,8 +105,8 @@ def delete(id: int):
""" Function responsible for deleting users from the site """ """ Function responsible for deleting users from the site """
if not is_role("Admin"): if not is_role("Admin"):
flash("You must be logged in an admin to remove users!", "error") flash("You must be logged in an admin to remove users!", "error")
return redirect("/") return redirect(url_for('main.index'))
db = UserController() db = UserController()
db.delete(id) db.delete(id)
return redirect("/admin/users/") return redirect(url_for('main.admin.users'))

View File

@ -59,12 +59,6 @@
</form> </form>
</div> </div>
</div> </div>
<!-- <a href="/products/{{user.id}}" class="product product-link">
<div class="product-title">{{user.username}}</div>
<div class="product-content-container">
</div>
<input type="submit" class="product-add-to-cart" value="Add to Cart" />
</a> -->
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>

View File

@ -4,7 +4,7 @@
<p>Showing results for {{products|count}} products</p> <p>Showing results for {{products|count}} products</p>
<div class="product-container"> <div class="product-container">
{% for product in products %} {% for product in products %}
<a href="/products/{{product.id}}" class="product product-link"> <a href="{{ url_for('main.products.product', id=product.id) }}" class="product product-link">
<div class="product-title">{{product.name}}</div> <div class="product-title">{{product.name}}</div>
<div class="product-content-container"> <div class="product-content-container">
<img class="product-image-preview" src="{{ url_for('static', filename='assets/img/products/' + product.image) }}" alt="Brake Disks" /> <img class="product-image-preview" src="{{ url_for('static', filename='assets/img/products/' + product.image) }}" alt="Brake Disks" />

View File

@ -19,8 +19,8 @@
{% if user.role == "Seller" %} {% if user.role == "Seller" %}
<div class="categories"> <div class="categories">
{# List all available Seller tools #} {# List all available Seller tools #}
<a href="{{url_for('main.products.display_add_product')}}" class="category">Create Products</a> <a href="{{ url_for('main.products.display_add') }}" class="category">Create Products</a>
<a href="{{url_for('main.products.display_own_products')}}" class="category">View My Products</a> <a href="{{ url_for('main.products.display_own') }}" class="category">View My Products</a>
<a href="{{ url_for('main.stats.index') }}" class="category">View Seller Stats</a> <a href="{{ url_for('main.stats.index') }}" class="category">View Seller Stats</a>
</div> </div>
{% elif user.role == "Admin" %} {% elif user.role == "Admin" %}

View File

@ -2,7 +2,7 @@
<div id="input-form-wrap"> <div id="input-form-wrap">
<h2>Login</h2> <h2>Login</h2>
<form class="input-form" method="POST"> <form class="input-form" method="POST" action="{{ url_for('main.users.login') }}">
<div class="input-form-row"> <div class="input-form-row">
<input type="text" id="username" name="username" placeholder="Username" required> <input type="text" id="username" name="username" placeholder="Username" required>
</div> </div>

View File

@ -2,7 +2,7 @@
<div id="input-form-wrap"> <div id="input-form-wrap">
<h2>Create New Product</h2> <h2>Create New Product</h2>
<form class="input-form" method="POST" enctype="multipart/form-data"> <form class="input-form" method="POST" action="{{ url_for('main.products.add') }}" enctype="multipart/form-data">
<div class="input-form-row"> <div class="input-form-row">
<input type="text" id="name" name="name" placeholder="Product Name" required> <input type="text" id="name" name="name" placeholder="Product Name" required>
<input type="file" id="image" name="image" accept="image/x" required> <input type="file" id="image" name="image" accept="image/x" required>
@ -31,6 +31,6 @@
</form> </form>
<div id="create-account-wrap"> <div id="create-account-wrap">
<p>Want to view all of your products? <a href="{{url_for('main.products.display_own_products')}}">Click Here</a><p> <p>Want to view all of your products? <a href="{{ url_for('main.products.display_own') }}">Click Here</a><p>
</div> </div>
</div> </div>

View File

@ -4,7 +4,7 @@
{% if product != None %} {% if product != None %}
{% if user.id == product.sellerID %} {% if user.id == product.sellerID %}
<!-- Form --> <!-- Form -->
<form class="product-fs" method="POST" action="{{url_for('main.products.update_product', id=product.id)}}" enctype="multipart/form-data"> <form class="product-fs" method="POST" action="{{ url_for('main.products.update', id=product.id) }}" enctype="multipart/form-data">
<img class="product-image" src="{{ url_for('static', filename='assets/img/products/' + product.image) }}" alt="Brake Disks"/> <img class="product-image" src="{{ url_for('static', filename='assets/img/products/' + product.image) }}" alt="Brake Disks"/>
<div class="product-details"> <div class="product-details">
<div class="input-form-row"> <div class="input-form-row">
@ -83,7 +83,7 @@
<label class="modal__close" for="deleteModal"></label> <label class="modal__close" for="deleteModal"></label>
<h2>Confirm Delete</h2> <h2>Confirm Delete</h2>
<p>Are you sure you want to <b>delete {{product.name}}</b> from your products</p> <p>Are you sure you want to <b>delete {{product.name}}</b> from your products</p>
<form method="POST" action="{{url_for('main.products.delete_product', id=product.id)}}"> <form method="POST" action="{{ url_for('main.products.delete', id=product.id) }}">
<div class="input-form-row"> <div class="input-form-row">
<input type="submit" class="modal-btn error" for="deleteModal" value="Delete" /> <input type="submit" class="modal-btn error" for="deleteModal" value="Delete" />
</div> </div>

View File

@ -2,7 +2,7 @@
<div id="input-form-wrap"> <div id="input-form-wrap">
<h2>Sign Up</h2> <h2>Sign Up</h2>
<form class="input-form" method="POST"> <form class="input-form" action="{{ url_for('main.users.signup') }}" method="POST">
<div class="input-form-row"> <div class="input-form-row">
<input type="text" id="firstname" name="firstname" placeholder="First Name" required> <input type="text" id="firstname" name="firstname" placeholder="First Name" required>
<input type="text" id="lastname" name="lastname" placeholder="Last Name" required> <input type="text" id="lastname" name="lastname" placeholder="Last Name" required>
@ -28,6 +28,6 @@
</form> </form>
<div id="create-account-wrap"> <div id="create-account-wrap">
<p>Already have an account? <a href="{{url_for('main.users.login')}}">Login</a><p> <p>Already have an account? <a href="{{ url_for('main.users.display_login') }}">Login</a><p>
</div> </div>
</div> </div>