WMGZON/controllers/web/product.py

72 lines
2.2 KiB
Python

from flask import Blueprint
from flask import render_template, session, flash, request, redirect
from controllers.database.product import ProductController
from controllers.database.category import CategoryController
from controllers.database.user import UserController
blueprint = Blueprint("products", __name__, url_prefix="/products")
# Global context to enable the categories to be accessed
# from any view
@blueprint.context_processor
def category_list():
database = CategoryController()
categories = database.read_all()
return dict(categories=categories)
# Loads the front product page
@blueprint.route('/')
def index():
database = ProductController()
products = database.read_all()
# No Products visible
if products == None:
flash("No Products available")
return render_template('index.html', content="content.html", products = products)
# Loads a given product category page
@blueprint.route('/<string:category>')
def category(category: str):
database = ProductController()
# Check to see if there is a custome search term
search_term = request.args.get("search", type=str)
if search_term != None:
print(f"Search Term {search_term}")
products = database.read_all(category, search_term)
else:
products = database.read_all(category)
# No Products visible
if products == None:
flash(f"No Products available in {category}")
return render_template('index.html', content="content.html", products = products, category = category)
# Loads a given product based on ID
@blueprint.route('/<int:id>')
def id(id: int):
return "ID: " + str(id)
# Launches the page to add a new product to the site
@blueprint.route('/add')
def add_product():
user_id = session.get('user_id')
# User must be logged in to view this page
if user_id == None:
flash("Please Login to view this page")
return redirect('/login')
db = UserController()
user = db.read_id(user_id)
if user == None or user.role != "Seller":
flash("You must be logged in as a Seller to view this page")
return redirect('/')
return render_template('index.html', content='new_product.html')