From d6f7b3e549db0b4868b3fc7bb22ebdad799b9471 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 14 Feb 2024 16:46:30 +0000 Subject: [PATCH] #10 Added Product section to admin page --- controllers/web/admin.py | 2 ++ controllers/web/product.py | 16 ++++++--- controllers/web/stats.py | 2 +- static/css/alerts.css | 1 + templates/admin.html | 73 +++++++++++++++++++++++++++++++++++++- templates/header.html | 2 +- templates/product.html | 2 +- 7 files changed, 90 insertions(+), 8 deletions(-) diff --git a/controllers/web/admin.py b/controllers/web/admin.py index b2097e7..8a41765 100644 --- a/controllers/web/admin.py +++ b/controllers/web/admin.py @@ -46,6 +46,8 @@ def products(): db = ProductController() products = db.read_all() + print(len(products)) + return render_template( "index.html", content="admin.html", diff --git a/controllers/web/product.py b/controllers/web/product.py index 097262c..144bc15 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -50,6 +50,13 @@ def get_filter(): return FILTERS['Relevance'] +def redirect_on_complete(): + """ Redirects to an appropriate location depending on the role """ + if is_role("Admin"): + return redirect(url_for('main.admin.products')) + return redirect(url_for('main.seller.display_own')) + + @product_blueprint.context_processor @seller_blueprint.context_processor def category_list(): @@ -168,9 +175,10 @@ def update(id: int): db = ProductController() product = db.read_id(id) - if product.sellerID != user_id: + # Only admins and the owner can change the product + if product.sellerID != user_id and not is_role("Admin"): flash("This product does not belong to you!", "error") - return redirect(url_for('main.seller.own')) + return redirect(url_for('main.seller.display_own')) # Save new image file file = request.files.get('image') @@ -201,13 +209,13 @@ def delete(id: int): db = ProductController() product = db.read_id(id) - if product.sellerID != user_id: + if product.sellerID != user_id and not is_role("Admin"): flash("This product does not belong to you!", "error") return redirect(url_for('main.seller.display_own')) db.delete(id) flash("Product Removed!", "success") - return redirect(url_for('main.seller.display_own')) + return redirect_on_complete() @seller_blueprint.route('/ownproducts') diff --git a/controllers/web/stats.py b/controllers/web/stats.py index 96d04bf..2d49f82 100644 --- a/controllers/web/stats.py +++ b/controllers/web/stats.py @@ -31,7 +31,7 @@ def view_product_stats(id: int): # Check user is seller if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") - return redirect(url_for('main.index')) + return redirect(url_for('main.products.product', id=id)) db = ProductController() product = db.read_id(id) diff --git a/static/css/alerts.css b/static/css/alerts.css index 3677ff7..376567a 100644 --- a/static/css/alerts.css +++ b/static/css/alerts.css @@ -74,6 +74,7 @@ .modal { opacity: 0; + z-index: 1; visibility: hidden; position: fixed; top: 0; diff --git a/templates/admin.html b/templates/admin.html index 154bce0..d4cbc3f 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -2,7 +2,8 @@ -{% if users != None %} + +{% if users is defined and users != None %}

Showing {{users|count}} users

@@ -63,6 +64,76 @@
+ + + +{% elif products is defined and products != None %} +

Showing {{products|count}} products

+
+ + + + + + + + + + + + + + {% for product in products %} + + + + + + + + + + + + + + {% endfor %} + +
#NameSellerIDPriceCategoryQuantityActions
{{product.id}}{{product.name}}{{product.sellerID}}{{product.cost}}{{product.category}}{{product.quantityAvailable}} +
+ +
+

Edit Product

+
+

{{product.id}}

+
+
+
+ +
+
+
+ + {% else %}

Sorry... We have nothing to show here!

{% endif %} diff --git a/templates/header.html b/templates/header.html index 8906f32..bd778ec 100644 --- a/templates/header.html +++ b/templates/header.html @@ -27,7 +27,7 @@
{# List all available Admin tools #} Manage Users - Manage Products + Manage Products
{% endif %} {% endif %} diff --git a/templates/product.html b/templates/product.html index b4969cf..6b193cf 100644 --- a/templates/product.html +++ b/templates/product.html @@ -2,7 +2,7 @@ {% if product != None %} - {% if user.id == product.sellerID %} + {% if user.id == product.sellerID or user.role == "Admin" %}
Brake Disks