2024-01-22 08:21:40 +00:00
|
|
|
"""
|
|
|
|
Product related endpoints. Included contexts for principles such as
|
|
|
|
categories and image processing.
|
|
|
|
"""
|
|
|
|
|
2024-01-22 10:49:08 +00:00
|
|
|
from flask import render_template, session, flash, request, redirect, Blueprint
|
2024-01-19 13:31:06 +00:00
|
|
|
|
|
|
|
from models.products.product import Product
|
2024-01-05 18:20:56 +00:00
|
|
|
from controllers.database.product import ProductController
|
2024-01-06 01:14:20 +00:00
|
|
|
from controllers.database.category import CategoryController
|
2024-01-19 12:37:51 +00:00
|
|
|
from controllers.database.user import UserController
|
2024-01-05 18:20:56 +00:00
|
|
|
|
2024-01-19 13:31:06 +00:00
|
|
|
from datetime import datetime
|
2024-01-25 00:05:34 +00:00
|
|
|
from utils.file_utils import allowed_file, save_image, remove_file
|
2024-01-22 17:35:49 +00:00
|
|
|
from utils.user_utils import is_role
|
2024-01-22 10:49:08 +00:00
|
|
|
|
2024-01-19 16:35:23 +00:00
|
|
|
import pathlib
|
2024-01-25 00:05:34 +00:00
|
|
|
import os
|
2024-01-19 13:31:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-06 01:14:20 +00:00
|
|
|
@blueprint.context_processor
|
|
|
|
def category_list():
|
2024-01-22 08:21:40 +00:00
|
|
|
""" Places a list of all categories in the products context """
|
2024-01-06 01:14:20 +00:00
|
|
|
database = CategoryController()
|
|
|
|
categories = database.read_all()
|
|
|
|
return dict(categories=categories)
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.route('/')
|
|
|
|
def index():
|
2024-01-22 08:21:40 +00:00
|
|
|
""" The front product page """
|
2024-01-22 10:49:08 +00:00
|
|
|
# Returning an empty category acts the same
|
|
|
|
# as a generic home page
|
|
|
|
return category("")
|
2024-01-05 21:40:32 +00:00
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 21:40:32 +00:00
|
|
|
@blueprint.route('/<string:category>')
|
|
|
|
def category(category: str):
|
2024-01-22 08:21:40 +00:00
|
|
|
""" Loads a given categories page """
|
2024-01-06 01:14:20 +00:00
|
|
|
database = ProductController()
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-15 21:47:18 +00:00
|
|
|
# Check to see if there is a custome search term
|
|
|
|
search_term = request.args.get("search", type=str)
|
2024-01-21 22:22:29 +00:00
|
|
|
if search_term is not None:
|
2024-01-15 21:47:18 +00:00
|
|
|
products = database.read_all(category, search_term)
|
|
|
|
else:
|
|
|
|
products = database.read_all(category)
|
2024-01-06 01:14:20 +00:00
|
|
|
|
|
|
|
# No Products visible
|
2024-01-21 22:22:29 +00:00
|
|
|
if products is None:
|
2024-01-22 10:49:08 +00:00
|
|
|
flash(f"No Products available. Try expanding your search criteria.")
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-22 17:53:56 +00:00
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content="content.html",
|
|
|
|
products=products,
|
|
|
|
category=category
|
|
|
|
)
|
2024-01-05 21:40:32 +00:00
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-05 21:40:32 +00:00
|
|
|
@blueprint.route('/<int:id>')
|
|
|
|
def id(id: int):
|
2024-01-22 08:21:40 +00:00
|
|
|
""" Loads a given product based on ID """
|
2024-01-23 17:53:00 +00:00
|
|
|
db = ProductController()
|
|
|
|
product = db.read_id(id)
|
|
|
|
|
|
|
|
# Check that a valid product was returned
|
|
|
|
if product is None:
|
2024-01-24 18:07:41 +00:00
|
|
|
flash(f"No Product available with id {id}")
|
2024-01-23 17:53:00 +00:00
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
print(product.name)
|
2024-01-24 18:07:41 +00:00
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content='product.html',
|
|
|
|
product=product
|
|
|
|
)
|
2024-01-05 21:40:32 +00:00
|
|
|
|
2024-01-19 12:37:51 +00:00
|
|
|
|
|
|
|
@blueprint.route('/add')
|
2024-01-19 13:31:06 +00:00
|
|
|
def display_add_product():
|
2024-01-22 08:21:40 +00:00
|
|
|
""" Launches the page to add a new product to the site """
|
2024-01-19 12:37:51 +00:00
|
|
|
user_id = session.get('user_id')
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-22 17:35:49 +00:00
|
|
|
# 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!")
|
2024-01-23 13:30:18 +00:00
|
|
|
return redirect("/")
|
2024-01-19 12:37:51 +00:00
|
|
|
|
|
|
|
return render_template('index.html', content='new_product.html')
|
2024-01-19 13:31:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.post('/add')
|
|
|
|
def add_product():
|
2024-01-22 08:21:40 +00:00
|
|
|
""" Server site processing to handle a request to add a
|
|
|
|
new product to the site
|
|
|
|
"""
|
2024-01-19 13:31:06 +00:00
|
|
|
user_id = session.get('user_id')
|
2024-01-21 22:06:06 +00:00
|
|
|
|
2024-01-22 17:35:49 +00:00
|
|
|
# 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!")
|
2024-01-23 13:30:18 +00:00
|
|
|
return redirect("/")
|
2024-01-19 13:31:06 +00:00
|
|
|
|
|
|
|
file = request.files.get('image')
|
2024-01-25 00:05:34 +00:00
|
|
|
image_filename = save_image(file)
|
2024-01-19 13:31:06 +00:00
|
|
|
|
|
|
|
product = Product(
|
|
|
|
request.form.get('name'),
|
2024-01-25 00:05:34 +00:00
|
|
|
image_filename if image_filename is not None else "",
|
2024-01-19 13:31:06 +00:00
|
|
|
request.form.get('description'),
|
|
|
|
request.form.get('cost'),
|
|
|
|
request.form.get('category'),
|
2024-01-22 17:53:56 +00:00
|
|
|
user_id,
|
2024-01-19 13:31:06 +00:00
|
|
|
datetime.now(),
|
|
|
|
request.form.get('quantity')
|
|
|
|
)
|
2024-01-24 18:07:41 +00:00
|
|
|
|
2024-01-19 13:31:06 +00:00
|
|
|
db = ProductController()
|
|
|
|
db.create(product)
|
|
|
|
|
2024-01-23 09:48:59 +00:00
|
|
|
return redirect('/products/ownproducts')
|
2024-01-22 17:35:49 +00:00
|
|
|
|
|
|
|
|
2024-01-24 18:07:41 +00:00
|
|
|
@blueprint.post('/update/<int:id>')
|
|
|
|
def update_product(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')
|
|
|
|
|
|
|
|
# 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!")
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
product = db.read_id(id)
|
|
|
|
|
|
|
|
if product.sellerID != user_id:
|
|
|
|
flash("This product does not belong to you!")
|
|
|
|
return redirect("/ownproducts")
|
|
|
|
|
2024-01-25 00:05:34 +00:00
|
|
|
# Save new image file
|
|
|
|
file = request.files.get('image')
|
|
|
|
new_image = save_image(file)
|
|
|
|
|
|
|
|
if new_image is not None:
|
|
|
|
remove_file(os.path.join(os.environ.get('FILESTORE'), product.image))
|
|
|
|
product.image = new_image
|
|
|
|
|
2024-01-24 18:07:41 +00:00
|
|
|
# Update product details
|
|
|
|
product.name = request.form.get('name')
|
|
|
|
product.description = request.form.get('description')
|
|
|
|
product.category = request.form.get('category')
|
|
|
|
product.cost = request.form.get('cost')
|
|
|
|
product.quantityAvailable = request.form.get('quantity')
|
|
|
|
|
|
|
|
db.update(product)
|
|
|
|
flash("Product successfully updated")
|
2024-01-25 00:05:34 +00:00
|
|
|
return redirect(f"/products/{product.id}")
|
2024-01-24 18:07:41 +00:00
|
|
|
|
|
|
|
|
2024-01-22 17:35:49 +00:00
|
|
|
@blueprint.route('/ownproducts')
|
|
|
|
def display_own_products():
|
2024-01-23 09:48:59 +00:00
|
|
|
""" Display products owned by the currently logged in seller """
|
2024-01-22 17:53:56 +00:00
|
|
|
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!")
|
2024-01-23 13:30:18 +00:00
|
|
|
return redirect("/")
|
2024-01-22 17:53:56 +00:00
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
products = db.read_user(user_id)
|
|
|
|
|
|
|
|
if products is None:
|
|
|
|
flash("You don't currently have any products for sale.")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content='content.html',
|
|
|
|
products=products
|
|
|
|
)
|