REFACTOR: Changes all raw url's to url_fors
This commit is contained in:
parent
3dc7a1f861
commit
82f1eed554
@ -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'))
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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'))
|
||||
|
@ -59,12 +59,6 @@
|
||||
</form>
|
||||
</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 %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
@ -4,7 +4,7 @@
|
||||
<p>Showing results for {{products|count}} products</p>
|
||||
<div class="product-container">
|
||||
{% 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-content-container">
|
||||
<img class="product-image-preview" src="{{ url_for('static', filename='assets/img/products/' + product.image) }}" alt="Brake Disks" />
|
||||
|
@ -19,8 +19,8 @@
|
||||
{% if user.role == "Seller" %}
|
||||
<div class="categories">
|
||||
{# 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_own_products')}}" class="category">View My Products</a>
|
||||
<a href="{{ url_for('main.products.display_add') }}" class="category">Create 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>
|
||||
</div>
|
||||
{% elif user.role == "Admin" %}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<div id="input-form-wrap">
|
||||
<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">
|
||||
<input type="text" id="username" name="username" placeholder="Username" required>
|
||||
</div>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<div id="input-form-wrap">
|
||||
<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">
|
||||
<input type="text" id="name" name="name" placeholder="Product Name" required>
|
||||
<input type="file" id="image" name="image" accept="image/x" required>
|
||||
@ -31,6 +31,6 @@
|
||||
</form>
|
||||
|
||||
<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>
|
||||
|
@ -4,7 +4,7 @@
|
||||
{% if product != None %}
|
||||
{% if user.id == product.sellerID %}
|
||||
<!-- 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"/>
|
||||
<div class="product-details">
|
||||
<div class="input-form-row">
|
||||
@ -83,7 +83,7 @@
|
||||
<label class="modal__close" for="deleteModal"></label>
|
||||
<h2>Confirm Delete</h2>
|
||||
<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">
|
||||
<input type="submit" class="modal-btn error" for="deleteModal" value="Delete" />
|
||||
</div>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<div id="input-form-wrap">
|
||||
<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">
|
||||
<input type="text" id="firstname" name="firstname" placeholder="First Name" required>
|
||||
<input type="text" id="lastname" name="lastname" placeholder="Last Name" required>
|
||||
@ -28,6 +28,6 @@
|
||||
</form>
|
||||
|
||||
<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>
|
||||
|
Loading…
Reference in New Issue
Block a user