From 897d4ab9aa68fa02aa6b917b2234420366115f97 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 22 Jan 2024 10:49:08 +0000 Subject: [PATCH] #1 Allow searches to be made from the main page --- controllers/web/product.py | 36 +++++++----------------------------- static/css/alerts.css | 5 +++-- templates/content.html | 2 +- templates/header.html | 4 ++-- utils/__init__.py | 0 utils/file_utils.py | 11 +++++++++++ 6 files changed, 24 insertions(+), 34 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/file_utils.py diff --git a/controllers/web/product.py b/controllers/web/product.py index c35db7e..6811f46 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -3,9 +3,7 @@ categories and image processing. """ -from flask import Blueprint - -from flask import render_template, session, flash, request, redirect +from flask import render_template, session, flash, request, redirect, Blueprint from models.products.product import Product from controllers.database.product import ProductController @@ -13,23 +11,12 @@ from controllers.database.category import CategoryController from controllers.database.user import UserController from datetime import datetime -from werkzeug.utils import secure_filename +from utils.file_utils import allowed_file + import os import uuid import pathlib -ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} - - -def allowed_file(filename): - """ Ensures only filenames ending with the correct extension are allowed. - Note: This does not verify that the content inside of the file - matches the type specified - """ - return '.' in filename and \ - filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS - - blueprint = Blueprint("products", __name__, url_prefix="/products") @@ -44,17 +31,9 @@ def category_list(): @blueprint.route('/') def index(): """ The front product page """ - database = ProductController() - products = database.read_all() - - # No Products visible - if products is None: - flash("No Products available") - - return render_template('index.html', - content="content.html", - products=products - ) + # Returning an empty category acts the same + # as a generic home page + return category("") @blueprint.route('/') @@ -65,14 +44,13 @@ def category(category: str): # Check to see if there is a custome search term search_term = request.args.get("search", type=str) if search_term is not 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 is None: - flash(f"No Products available in {category}") + flash(f"No Products available. Try expanding your search criteria.") return render_template('index.html', content="content.html", diff --git a/static/css/alerts.css b/static/css/alerts.css index 548acc7..a547bf3 100644 --- a/static/css/alerts.css +++ b/static/css/alerts.css @@ -27,14 +27,15 @@ .alertText { display: table; margin: 0 auto; + padding: 0 5px 0 0; text-align: center; - font-size: 16px; + font-size: 20px; } .alertClose { float: right; padding-top: 5px; - font-size: 10px; + font-size: 15px; } .clear { diff --git a/templates/content.html b/templates/content.html index 74786fe..234e95c 100644 --- a/templates/content.html +++ b/templates/content.html @@ -1,7 +1,7 @@
- {% if products is not None %} + {% if products != None %} {% for product in products %}
{{product.name}}
diff --git a/templates/header.html b/templates/header.html index a3e6852..850d9dd 100644 --- a/templates/header.html +++ b/templates/header.html @@ -5,7 +5,7 @@ - {% if user is not None: %} + {% if user != None: %}
Welcome, {{ user.username }} {% else %} Login/Signup @@ -13,7 +13,7 @@ - {% if user is not None and user.role == "Seller" %} + {% if user != None and user.role == "Seller" %}
{# List all available seller tools #} Create Products diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/file_utils.py b/utils/file_utils.py new file mode 100644 index 0000000..a457dd1 --- /dev/null +++ b/utils/file_utils.py @@ -0,0 +1,11 @@ +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} + + +def allowed_file(filename): + """ Ensures only filenames ending with the correct extension are allowed. + Note: This does not verify that the content inside of the file + matches the type specified + """ + return '.' in filename and \ + filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS +