#5 Completed product ranking in category

This commit is contained in:
Luke Else 2024-02-12 17:21:13 +00:00
parent 83829af06f
commit cafaf94a00
2 changed files with 22 additions and 19 deletions

View File

@ -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/<int:id>')
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/<int:id>')
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)

View File

@ -19,7 +19,7 @@
</div>
<div>
<p><b>Age:</b> {{data['age']}}</p>
<p><b>Ranking:</b> {{data['ranking']}}</p>
<p><b>Ranking:</b> #{{data['ranking']}} in {{data['category']}}</p>
<p><b>Stock Level:</b> {{data['stocklevel']}}</p>
<p><b>Total Views:</b> {{data['views']['total']}}</p>
</div>