#1 Allow searches to be made from the main page

This commit is contained in:
Luke Else 2024-01-22 10:49:08 +00:00
parent 216d71c15d
commit 897d4ab9aa
6 changed files with 24 additions and 34 deletions

View File

@ -3,9 +3,7 @@
categories and image processing. categories and image processing.
""" """
from flask import Blueprint from flask import render_template, session, flash, request, redirect, Blueprint
from flask import render_template, session, flash, request, redirect
from models.products.product import Product from models.products.product import Product
from controllers.database.product import ProductController from controllers.database.product import ProductController
@ -13,23 +11,12 @@ from controllers.database.category import CategoryController
from controllers.database.user import UserController from controllers.database.user import UserController
from datetime import datetime from datetime import datetime
from werkzeug.utils import secure_filename from utils.file_utils import allowed_file
import os import os
import uuid import uuid
import pathlib 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") blueprint = Blueprint("products", __name__, url_prefix="/products")
@ -44,17 +31,9 @@ def category_list():
@blueprint.route('/') @blueprint.route('/')
def index(): def index():
""" The front product page """ """ The front product page """
database = ProductController() # Returning an empty category acts the same
products = database.read_all() # as a generic home page
return category("")
# No Products visible
if products is None:
flash("No Products available")
return render_template('index.html',
content="content.html",
products=products
)
@blueprint.route('/<string:category>') @blueprint.route('/<string:category>')
@ -65,14 +44,13 @@ def category(category: str):
# Check to see if there is a custome search term # Check to see if there is a custome search term
search_term = request.args.get("search", type=str) search_term = request.args.get("search", type=str)
if search_term is not None: if search_term is not None:
print(f"Search Term {search_term}")
products = database.read_all(category, search_term) products = database.read_all(category, search_term)
else: else:
products = database.read_all(category) products = database.read_all(category)
# No Products visible # No Products visible
if products is None: 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', return render_template('index.html',
content="content.html", content="content.html",

View File

@ -27,14 +27,15 @@
.alertText { .alertText {
display: table; display: table;
margin: 0 auto; margin: 0 auto;
padding: 0 5px 0 0;
text-align: center; text-align: center;
font-size: 16px; font-size: 20px;
} }
.alertClose { .alertClose {
float: right; float: right;
padding-top: 5px; padding-top: 5px;
font-size: 10px; font-size: 15px;
} }
.clear { .clear {

View File

@ -1,7 +1,7 @@
<link rel="stylesheet" href="{{ url_for('static', filename='css/products.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='css/products.css') }}">
<div class="product-container"> <div class="product-container">
{% if products is not None %} {% if products != None %}
{% for product in products %} {% for product in products %}
<a href="/products/{{product.id}}" class="product product-link"> <a href="/products/{{product.id}}" class="product product-link">
<div class="product-title">{{product.name}}</div> <div class="product-title">{{product.name}}</div>

View File

@ -5,7 +5,7 @@
<input type="text" name="search" placeholder="Find your favourite products" class="search-bar"> <input type="text" name="search" placeholder="Find your favourite products" class="search-bar">
<input type="submit" class="search-button"> <input type="submit" class="search-button">
</form> </form>
{% if user is not None: %} {% if user != None: %}
<a href="/logout">Welcome, {{ user.username }}</a> <a href="/logout">Welcome, {{ user.username }}</a>
{% else %} {% else %}
<a href="/login">Login/Signup</a> <a href="/login">Login/Signup</a>
@ -13,7 +13,7 @@
</nav> </nav>
<centre> <centre>
{% if user is not None and user.role == "Seller" %} {% if user != None and user.role == "Seller" %}
<div class="categories"> <div class="categories">
{# List all available seller tools #} {# List all available seller tools #}
<a href="/products/add" class="category">Create Products</a> <a href="/products/add" class="category">Create Products</a>

0
utils/__init__.py Normal file
View File

11
utils/file_utils.py Normal file
View File

@ -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