diff --git a/controllers/web/stats.py b/controllers/web/stats.py index 91ab33d..8e9a3eb 100644 --- a/controllers/web/stats.py +++ b/controllers/web/stats.py @@ -3,9 +3,11 @@ """ from flask import Blueprint -from flask import render_template, request +from flask import render_template, request, flash, session, redirect from controllers.database.stats import StatsController from controllers.database.product import ProductController +from controllers.database.category import CategoryController +from controllers.web.product import is_role from datetime import datetime import random @@ -26,6 +28,19 @@ def index(): @blueprint.route('/products/') def view_product_stats(id: int): """ Page to view statistics for a given product """ + # 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("/") + + db = ProductController() + product = db.read_id(id) + + # Check user owns this product + if product is None or product.sellerID is not session.get("user_id"): + flash("This product does not belong to you!", "error") + return redirect("/products/ownproducts") + db = StatsController() prev_days: int = request.args.get('prev_days', 7, int) @@ -45,18 +60,14 @@ def view_product_stats(id: int): # Ranking in category ranking = db.read_ranking(id) - db = ProductController() - product = db.read_id(id) - - # Stock Level - product.quantityAvailable - - # Age - time_since_posted = datetime.now() - product.postedDate + # Category name + db = CategoryController() + category = db.read(product.category).name data = { - "age": time_since_posted, + "age": datetime.now() - product.postedDate, "ranking": ranking, + "category": category, "views": { "total": total_views, "headings": list(reversed(product_view_frequency_data.keys())), @@ -70,11 +81,3 @@ def view_product_stats(id: int): content="stats.html", data=data ) - - -@blueprint.route('/users/') -def view_user_stats(id: int): - """ Page to view statistics for a given user """ - db = StatsController() - data = db.read_user(id) - return render_template("index.html", content="stats.html", data=data) diff --git a/templates/stats.html b/templates/stats.html index 91206e8..590c5ec 100644 --- a/templates/stats.html +++ b/templates/stats.html @@ -19,7 +19,7 @@

Age: {{data['age']}}

-

Ranking: {{data['ranking']}}

+

Ranking: #{{data['ranking']}} in {{data['category']}}

Stock Level: {{data['stocklevel']}}

Total Views: {{data['views']['total']}}