diff --git a/controllers/database/database.py b/controllers/database/database.py index 2e17940..523d9b6 100644 --- a/controllers/database/database.py +++ b/controllers/database/database.py @@ -44,7 +44,10 @@ class DatabaseController(ABC): """ obj = of.__new__(of) for attr, value in with_fields.items(): - setattr(obj, attr, value) + try: + setattr(obj, attr, value) + except KeyError: + return of(value) return obj def do(self, query: str, params: list[str]): diff --git a/controllers/database/stats.py b/controllers/database/stats.py index 283385c..fb08d2d 100644 --- a/controllers/database/stats.py +++ b/controllers/database/stats.py @@ -76,6 +76,19 @@ class StatsController(DatabaseController): return day_views + def read_product_views(self, id: int): + """ Returns the total number of views for a product """ + params = [ + id + ] + + # Total Views + query = """ + SELECT count(id) FROM Views WHERE productID = ? + """ + + return self.get_one(query, params, int) + def update(self): print("Doing work") diff --git a/controllers/web/stats.py b/controllers/web/stats.py index 070ce20..f8d22da 100644 --- a/controllers/web/stats.py +++ b/controllers/web/stats.py @@ -5,6 +5,10 @@ from flask import Blueprint from flask import render_template, request from controllers.database.stats import StatsController +from controllers.database.product import ProductController + +from datetime import datetime +import random # Blueprint to append user endpoints to blueprint = Blueprint("stats", __name__, url_prefix='/stats') @@ -27,18 +31,49 @@ def view_product_stats(id: int): prev_days: int = request.args.get('prev_days', 7, int) data = db.read_days(id, prev_days) - product_frequency_data = dict(map( - # lambda k, v: (k, random.randint(0, 100)), - lambda k, v: (k, len(v)), + product_view_frequency_data = dict(map( + lambda k, v: (k, random.randint(0, 100)), + # lambda k, v: (k, len(v)), data.keys(), data.values() )) + print(db.read_product_views(id)) + + db = ProductController() + product = db.read_id(id) + + # Ranking in category + query = """ + SELECT id, count(id) FROM stats WHERE id = ( + SELECT id FROM Products WEHRE id = ( + SELECT categoryID FROM Products WHERE id = ? + ) + ) + """ + + # Stock Level + product.quantityAvailable + + # Age + time_since_posted = datetime.now() - product.postedDate + + data = { + "age": time_since_posted, + "ranking": 0, + "views": { + "total": 0, + "headings": list(reversed(product_view_frequency_data.keys())), + "data": list(reversed(product_view_frequency_data.values())) + }, + "stocklevel": product.quantityAvailable + } + return render_template( "index.html", content="stats.html", - headings=list(reversed(product_frequency_data.keys())), - data=list(reversed(product_frequency_data.values())) + headings=list(reversed(product_view_frequency_data.keys())), + data=list(reversed(product_view_frequency_data.values())) ) diff --git a/venv.bat b/venv.bat new file mode 100644 index 0000000..1e5301b --- /dev/null +++ b/venv.bat @@ -0,0 +1 @@ +./.venv/Scripts/activate