Added ability for stats to be tracked on products

This commit is contained in:
Luke Else 2024-01-30 21:49:17 +00:00
parent dfb33e195c
commit e27ed02028
4 changed files with 145 additions and 6 deletions

View File

@ -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")

View File

@ -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")

21
models/stats.py Normal file
View File

@ -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()

View File

@ -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
)