From 7faca50b738ec03bd2f4d3879f43b812a5dd7e79 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 12 Feb 2024 18:27:04 +0000 Subject: [PATCH] #7 Added basic set of product orderings. --- controllers/database/product.py | 10 +++++++--- controllers/web/product.py | 31 +++++++++++++++++++++++++++++-- templates/Car Parts.html | 14 +++++++------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/controllers/database/product.py b/controllers/database/product.py index ce0ac52..0a65697 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -30,11 +30,13 @@ class ProductController(DatabaseController): self.do(query, params) - def read(self, name: str = "") -> list[Product] | None: + def read(self, name: str = "", filter: str = "") -> list[Product] | None: params = [ "%" + name + "%" ] + query = "SELECT * FROM Products WHERE name like ?" + query += filter return self.get_many(query, params) @@ -46,8 +48,8 @@ class ProductController(DatabaseController): return self.get_one(query, params) - def read_all(self, category: str = "", - search_term: str = "") -> list[Product] | None: + def read_all(self, category: str = "", search_term: str = "", + filter: str = "") -> list[Product] | None: params = [ "%" + category + "%", "%" + search_term + "%" @@ -59,6 +61,8 @@ class ProductController(DatabaseController): AND Products.name LIKE ? """ + query += filter + return self.get_many(query, params) def read_user(self, user_id: int) -> list[Product] | None: diff --git a/controllers/web/product.py b/controllers/web/product.py index 503beb1..58e3100 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -19,6 +19,33 @@ import os blueprint = Blueprint("products", __name__, url_prefix="/products") +# 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' +} + + +@blueprint.context_processor +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'] + @blueprint.context_processor def category_list(): @@ -44,9 +71,9 @@ 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: - products = database.read_all(category, search_term) + products = database.read_all(category, search_term, get_filter()) else: - products = database.read_all(category) + products = database.read_all(category, "", get_filter()) # No Products visible if products is None: diff --git a/templates/Car Parts.html b/templates/Car Parts.html index 26bf504..4571cb9 100644 --- a/templates/Car Parts.html +++ b/templates/Car Parts.html @@ -10,13 +10,13 @@ - - - + {% if filters != None %} + + {% endif %}