2024-01-05 18:20:56 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
|
2024-01-19 12:37:51 +00:00
|
|
|
from flask import render_template, session, flash, request, redirect
|
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
|
|
|
|
from werkzeug import secure_filename
|
|
|
|
|
|
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
|
|
|
|
|
def allowed_file(filename):
|
|
|
|
return '.' in filename and \
|
|
|
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
2024-01-05 18:20:56 +00:00
|
|
|
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
|
|
|
|
2024-01-07 13:47:09 +00:00
|
|
|
# Global context to enable the categories to be accessed
|
|
|
|
# from any view
|
2024-01-06 01:14:20 +00:00
|
|
|
@blueprint.context_processor
|
|
|
|
def category_list():
|
|
|
|
database = CategoryController()
|
|
|
|
categories = database.read_all()
|
|
|
|
return dict(categories=categories)
|
|
|
|
|
2024-01-05 21:40:32 +00:00
|
|
|
# Loads the front product page
|
2024-01-05 18:20:56 +00:00
|
|
|
@blueprint.route('/')
|
|
|
|
def index():
|
|
|
|
database = ProductController()
|
|
|
|
products = database.read_all()
|
|
|
|
|
|
|
|
# No Products visible
|
|
|
|
if products == None:
|
|
|
|
flash("No Products available")
|
|
|
|
|
2024-01-19 11:57:53 +00:00
|
|
|
return render_template('index.html', content="content.html", products = products)
|
2024-01-05 21:40:32 +00:00
|
|
|
|
|
|
|
# Loads a given product category page
|
|
|
|
@blueprint.route('/<string:category>')
|
|
|
|
def category(category: str):
|
2024-01-06 01:14:20 +00:00
|
|
|
database = ProductController()
|
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)
|
|
|
|
if search_term != None:
|
|
|
|
print(f"Search Term {search_term}")
|
|
|
|
products = database.read_all(category, search_term)
|
|
|
|
else:
|
|
|
|
products = database.read_all(category)
|
2024-01-06 01:14:20 +00:00
|
|
|
|
|
|
|
# No Products visible
|
|
|
|
if products == None:
|
2024-01-15 22:26:10 +00:00
|
|
|
flash(f"No Products available in {category}")
|
2024-01-06 01:14:20 +00:00
|
|
|
|
2024-01-19 11:57:53 +00:00
|
|
|
return render_template('index.html', content="content.html", products = products, category = category)
|
2024-01-05 21:40:32 +00:00
|
|
|
|
|
|
|
# Loads a given product based on ID
|
|
|
|
@blueprint.route('/<int:id>')
|
|
|
|
def id(id: int):
|
|
|
|
return "ID: " + str(id)
|
|
|
|
|
2024-01-19 12:37:51 +00:00
|
|
|
|
|
|
|
# Launches the page to add a new product to the site
|
|
|
|
@blueprint.route('/add')
|
2024-01-19 13:31:06 +00:00
|
|
|
def display_add_product():
|
2024-01-19 12:37:51 +00:00
|
|
|
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')
|
2024-01-19 13:31:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Processes a request to add a new product to the site
|
|
|
|
@blueprint.post('/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', code=302)
|
|
|
|
|
|
|
|
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 perform this action")
|
|
|
|
return redirect('/', code=302)
|
|
|
|
|
|
|
|
file = request.files.get('image')
|
|
|
|
|
|
|
|
# Ensure that the correct file type is uploaded
|
|
|
|
if file == None OR NOT allowed_file(file.filename):
|
|
|
|
flash("Invalid File Uploaded")
|
|
|
|
return redirect("/add")
|
|
|
|
|
|
|
|
# Create the product object and push to database
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
file.save(os.path.join('static/assets/img/products/', secure_filename))
|
|
|
|
file.save
|
|
|
|
|
|
|
|
product = Product(
|
|
|
|
request.form.get('name'),
|
|
|
|
filename
|
|
|
|
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)
|
|
|
|
|
|
|
|
return render_template('index.html', content='new_product.html')
|