REFACTOR: Changes all raw url's to url_fors

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

View File

@@ -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('/<int:id>')
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/<int:id>')
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/<int:id>')
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)