From e27ed02028d6f5ef89d2ad62522a6b4b699fda79 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 30 Jan 2024 21:49:17 +0000 Subject: [PATCH] Added ability for stats to be tracked on products --- controllers/database/stats.py | 103 ++++++++++++++++++++++++++++++++++ controllers/web/product.py | 14 +++-- models/stats.py | 21 +++++++ scripts/create_tables.sql | 13 ++++- 4 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 controllers/database/stats.py create mode 100644 models/stats.py diff --git a/controllers/database/stats.py b/controllers/database/stats.py new file mode 100644 index 0000000..cff0db3 --- /dev/null +++ b/controllers/database/stats.py @@ -0,0 +1,103 @@ +from .database import DatabaseController +from models.stats import Stats + + +class StatsController(DatabaseController): + FIELDS = ['id', 'userID', 'productID', 'viewDate'] + + def __init__(self): + super().__init__() + + def create(self, view: Stats): + params = [ + view.userID, + view.productID, + view.viewDate + ] + + self._conn.execute( + """ + INSERT INTO Views + (userID, productID, viewDate) + VALUES (?, ?, ?)""", + params + ) + self._conn.commit() + + def read(self) -> list[Stats] | None: + + cursor = self._conn.execute( + "SELECT * FROM Views", + ) + rows = cursor.fetchmany() + + if rows is None: + return None + + views = list() + + # Create an object for each row + for view in rows: + params = dict(zip(self.FIELDS, view)) + obj = self.new_instance(Stats, params) + views.append(obj) + + return views + + def read_product(self, product_id: int = 0) -> list[Stats] | None: + params = [ + product_id + ] + + cursor = self._conn.execute( + """SELECT * FROM Stats + WHERE productID = ? + """, + params + ) + rows = cursor.fetchall() + + if len(rows) == 0: + return None + + views = list() + + # Create an object for each row + for view in rows: + params = dict(zip(self.FIELDS, view)) + obj = self.new_instance(Stats, params) + views.append(obj) + + return views + + def read_user(self, user_id: int) -> list[Stats] | None: + params = [ + user_id + ] + + cursor = self._conn.execute( + """SELECT * FROM Views + WHERE userID = ? + """, + params + ) + rows = cursor.fetchall() + + if len(rows) == 0: + return None + + views = list() + + # Create an object for each row + for view in rows: + params = dict(zip(self.FIELDS, view)) + obj = self.new_instance(Stats, params) + views.append(obj) + + return views + + def update(self): + print("Doing work") + + def delete(self): + print("Doing work") diff --git a/controllers/web/product.py b/controllers/web/product.py index 2a4a135..08a0feb 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -6,15 +6,15 @@ from flask import render_template, session, flash, request, redirect, Blueprint from models.products.product import Product +from models.stats import Stats from controllers.database.product import ProductController from controllers.database.category import CategoryController -from controllers.database.user import UserController +from controllers.database.stats import StatsController from datetime import datetime -from utils.file_utils import allowed_file, save_image, remove_file +from utils.file_utils import save_image, remove_file from utils.user_utils import is_role -import pathlib import os blueprint = Blueprint("products", __name__, url_prefix="/products") @@ -74,6 +74,12 @@ def id(id: int): flash(f"No Product available with id {id}", "warning") return redirect("/") + # Record a view on the product + db = StatsController() + user_id = session.get('user_id') + print(user_id) + db.create(Stats(product.id, user_id)) + return render_template( 'index.html', content='product.html', @@ -84,8 +90,6 @@ def id(id: int): @blueprint.route('/add') def display_add_product(): """ Launches the page to add a new product to the site """ - user_id = session.get('user_id') - # User needs to be logged in as a seller to view this page if not is_role("Seller"): flash("You must be logged in as a seller to view this page!", "error") diff --git a/models/stats.py b/models/stats.py new file mode 100644 index 0000000..291d258 --- /dev/null +++ b/models/stats.py @@ -0,0 +1,21 @@ +from datetime import datetime + +from models.users.user import User +from models.products.product import Product + + +class Stats: + + def __init__(self): + """ Constructor for a Stat object """ + self.id = 0 + self.userID = 0 + self.productID = 0 + self.viewDate = datetime.now() + + def __init__(self, product_id: int, user_id: int): + """ Construct a view with the user and product class """ + self.id = 0 + self.userID = user_id + self.productID = product_id + self.viewDate = datetime.now() diff --git a/scripts/create_tables.sql b/scripts/create_tables.sql index 5c587f7..6aa5ac3 100644 --- a/scripts/create_tables.sql +++ b/scripts/create_tables.sql @@ -53,4 +53,15 @@ CREATE TABLE IF NOT EXISTS Orders ( orderDate DATE NOT NULL ); - +CREATE TABLE IF NOT EXISTS Views ( + id INTEGER PRIMARY KEY, + userID INTEGER + REFERENCES Users (id) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + productID INTEGER NOT NULL + REFERENCES Users (id) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + viewDate DATE NOT NULL +)