2024-01-28 11:59:04 +00:00
|
|
|
"""
|
|
|
|
Product related endpoints. Included contexts for principles such as
|
|
|
|
categories and image processing.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 23:21:20 +00:00
|
|
|
from flask import (
|
|
|
|
render_template, session, flash, request, redirect, Blueprint, url_for
|
|
|
|
)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
from models.products.product import Product
|
2024-01-30 21:49:17 +00:00
|
|
|
from models.stats import Stats
|
2024-01-28 11:59:04 +00:00
|
|
|
from controllers.database.product import ProductController
|
|
|
|
from controllers.database.category import CategoryController
|
2024-01-30 21:49:17 +00:00
|
|
|
from controllers.database.stats import StatsController
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
from datetime import datetime
|
2024-01-30 21:49:17 +00:00
|
|
|
from utils.file_utils import save_image, remove_file
|
2024-01-28 11:59:04 +00:00
|
|
|
from utils.user_utils import is_role
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
product_blueprint = Blueprint("products", __name__, url_prefix="/products")
|
|
|
|
seller_blueprint = Blueprint("seller", __name__, url_prefix="/products")
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-12 18:27:04 +00:00
|
|
|
# List of available filters for the user to select
|
|
|
|
FILTERS = {
|
|
|
|
# ANY INFOMRATION PUT INTO THE VALUES HERE WILL BE INTERPRETED AS SQL!!!
|
|
|
|
'Relevance': 'ORDER BY quantityAvailable DESC',
|
|
|
|
'Price: High -> Low': 'ORDER BY cost DESC',
|
|
|
|
'Price: Low -> High': 'ORDER BY cost'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@product_blueprint.context_processor
|
2024-02-12 18:27:04 +00:00
|
|
|
def filter_list():
|
|
|
|
""" Places a list of all the available filters in the
|
|
|
|
products context
|
|
|
|
"""
|
|
|
|
return dict(filters=FILTERS)
|
|
|
|
|
|
|
|
|
|
|
|
def get_filter():
|
|
|
|
""" Return any filters that are currently active on the page """
|
|
|
|
filter = request.args.get('filter')
|
|
|
|
if filter is None:
|
|
|
|
filter = 'Relevance'
|
|
|
|
|
|
|
|
if filter in FILTERS:
|
|
|
|
return FILTERS[filter]
|
|
|
|
return FILTERS['Relevance']
|
|
|
|
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-14 16:46:30 +00:00
|
|
|
def redirect_on_complete():
|
|
|
|
""" Redirects to an appropriate location depending on the role """
|
|
|
|
if is_role("Admin"):
|
|
|
|
return redirect(url_for('main.admin.products'))
|
|
|
|
return redirect(url_for('main.seller.display_own'))
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@product_blueprint.context_processor
|
|
|
|
@seller_blueprint.context_processor
|
2024-01-28 11:59:04 +00:00
|
|
|
def category_list():
|
|
|
|
""" Places a list of all categories in the products context """
|
|
|
|
database = CategoryController()
|
|
|
|
categories = database.read_all()
|
|
|
|
return dict(categories=categories)
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.before_request
|
|
|
|
def check_role():
|
|
|
|
# User must be logged in as seller to view page
|
|
|
|
if not is_role("Seller") and not is_role("Admin"):
|
|
|
|
flash("You must be logged in as a seller to view this page!", "error")
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
|
|
|
|
|
|
|
|
@product_blueprint.route('/')
|
2024-01-28 11:59:04 +00:00
|
|
|
def index():
|
|
|
|
""" The front product page """
|
|
|
|
# Returning an empty category acts the same
|
|
|
|
# as a generic home page
|
|
|
|
return category("")
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@product_blueprint.route('/<string:category>')
|
2024-01-28 11:59:04 +00:00
|
|
|
def category(category: str):
|
|
|
|
""" Loads a given categories page """
|
|
|
|
database = ProductController()
|
|
|
|
|
|
|
|
# Check to see if there is a custome search term
|
|
|
|
search_term = request.args.get("search", type=str)
|
|
|
|
if search_term is not None:
|
2024-02-12 18:27:04 +00:00
|
|
|
products = database.read_all(category, search_term, get_filter())
|
2024-01-28 11:59:04 +00:00
|
|
|
else:
|
2024-02-12 18:27:04 +00:00
|
|
|
products = database.read_all(category, "", get_filter())
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
# No Products visible
|
|
|
|
if products is None:
|
|
|
|
flash(
|
|
|
|
f"No Products available. Try expanding your search criteria.",
|
|
|
|
"warning"
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content="content.html",
|
|
|
|
products=products,
|
|
|
|
category=category
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@product_blueprint.route('/<int:id>')
|
2024-02-13 23:15:32 +00:00
|
|
|
def product(id: int):
|
2024-01-28 11:59:04 +00:00
|
|
|
""" Loads a given product based on ID """
|
|
|
|
db = ProductController()
|
|
|
|
product = db.read_id(id)
|
|
|
|
|
|
|
|
# Check that a valid product was returned
|
|
|
|
if product is None:
|
|
|
|
flash(f"No Product available with id {id}", "warning")
|
2024-02-13 23:15:32 +00:00
|
|
|
return redirect(url_for('main.index'))
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-01-30 21:49:17 +00:00
|
|
|
# Record a view on the product
|
|
|
|
db = StatsController()
|
2024-02-09 22:10:25 +00:00
|
|
|
|
2024-01-30 21:49:17 +00:00
|
|
|
user_id = session.get('user_id')
|
|
|
|
db.create(Stats(product.id, user_id))
|
|
|
|
|
2024-01-28 11:59:04 +00:00
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content='product.html',
|
|
|
|
product=product
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.route('/add')
|
2024-02-13 23:15:32 +00:00
|
|
|
def display_add():
|
2024-01-28 11:59:04 +00:00
|
|
|
""" Launches the page to add a new product to the site """
|
|
|
|
return render_template('index.html', content='new_product.html')
|
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.post('/add')
|
2024-02-13 23:15:32 +00:00
|
|
|
def add():
|
2024-01-28 11:59:04 +00:00
|
|
|
""" Server site processing to handle a request to add a
|
|
|
|
new product to the site
|
|
|
|
"""
|
|
|
|
user_id = session.get('user_id')
|
|
|
|
|
|
|
|
file = request.files.get('image')
|
|
|
|
image_filename = save_image(file)
|
|
|
|
|
|
|
|
product = Product(
|
|
|
|
request.form.get('name'),
|
|
|
|
image_filename if image_filename is not None else "",
|
|
|
|
request.form.get('description'),
|
|
|
|
request.form.get('cost'),
|
|
|
|
request.form.get('category'),
|
|
|
|
user_id,
|
|
|
|
datetime.now(),
|
|
|
|
request.form.get('quantity')
|
|
|
|
)
|
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
db.create(product)
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
return redirect(url_for('main.seller.display_own'))
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.post('/update/<int:id>')
|
2024-02-13 23:15:32 +00:00
|
|
|
def update(id: int):
|
2024-01-28 11:59:04 +00:00
|
|
|
""" 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')
|
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
product = db.read_id(id)
|
|
|
|
|
2024-02-14 16:46:30 +00:00
|
|
|
# Only admins and the owner can change the product
|
|
|
|
if product.sellerID != user_id and not is_role("Admin"):
|
2024-01-28 11:59:04 +00:00
|
|
|
flash("This product does not belong to you!", "error")
|
2024-02-14 16:46:30 +00:00
|
|
|
return redirect(url_for('main.seller.display_own'))
|
2024-01-28 11:59:04 +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
|
|
|
|
|
|
|
|
# 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", 'notice')
|
2024-02-13 23:15:32 +00:00
|
|
|
return redirect(url_for('main.products.product', id=product.id))
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.post('/delete/<int:id>')
|
2024-02-13 23:15:32 +00:00
|
|
|
def delete(id: int):
|
2024-02-08 19:58:49 +00:00
|
|
|
""" 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')
|
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
product = db.read_id(id)
|
|
|
|
|
2024-02-14 16:46:30 +00:00
|
|
|
if product.sellerID != user_id and not is_role("Admin"):
|
2024-02-08 19:58:49 +00:00
|
|
|
flash("This product does not belong to you!", "error")
|
2024-02-14 14:56:58 +00:00
|
|
|
return redirect(url_for('main.seller.display_own'))
|
2024-02-08 19:58:49 +00:00
|
|
|
|
|
|
|
db.delete(id)
|
|
|
|
flash("Product Removed!", "success")
|
2024-02-14 16:46:30 +00:00
|
|
|
return redirect_on_complete()
|
2024-02-08 19:58:49 +00:00
|
|
|
|
|
|
|
|
2024-02-14 14:56:58 +00:00
|
|
|
@seller_blueprint.route('/ownproducts')
|
2024-02-13 23:15:32 +00:00
|
|
|
def display_own():
|
2024-01-28 11:59:04 +00:00
|
|
|
""" Display products owned by the currently logged in seller """
|
|
|
|
user_id = session.get('user_id')
|
|
|
|
|
|
|
|
db = ProductController()
|
|
|
|
products = db.read_user(user_id)
|
|
|
|
|
|
|
|
if products is None:
|
|
|
|
flash("You don't currently have any products for sale.", "info")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content='content.html',
|
|
|
|
products=products
|
|
|
|
)
|