This commit is contained in:
@ -4,7 +4,7 @@ from models.products.product import Product
|
||||
|
||||
class ProductController(DatabaseController):
|
||||
FIELDS = ['id', 'name', 'image', 'description', 'cost',
|
||||
'category', 'sellerID', 'postedDate', 'quantityAvailable']
|
||||
'sellerID', 'category', 'postedDate', 'quantityAvailable']
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@ -131,8 +131,30 @@ class ProductController(DatabaseController):
|
||||
|
||||
return products
|
||||
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
def update(self, product: Product):
|
||||
params = [
|
||||
product.name,
|
||||
product.description,
|
||||
product.image,
|
||||
product.cost,
|
||||
product.quantityAvailable,
|
||||
product.category,
|
||||
product.id
|
||||
]
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""UPDATE Products
|
||||
SET name = ?,
|
||||
description = ?,
|
||||
image = ?,
|
||||
cost = ?,
|
||||
quantityAvailable = ?,
|
||||
categoryID = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
params
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
||||
|
@ -69,12 +69,15 @@ def id(id: int):
|
||||
|
||||
# Check that a valid product was returned
|
||||
if product is None:
|
||||
flash("No Product available here")
|
||||
flash(f"No Product available with id {id}")
|
||||
return redirect("/")
|
||||
|
||||
print(product.name)
|
||||
return render_template('index.html', content='content.html', products = [product])
|
||||
return "ID: " + str(id)
|
||||
return render_template(
|
||||
'index.html',
|
||||
content='product.html',
|
||||
product=product
|
||||
)
|
||||
|
||||
|
||||
@blueprint.route('/add')
|
||||
@ -123,12 +126,44 @@ def add_product():
|
||||
datetime.now(),
|
||||
request.form.get('quantity')
|
||||
)
|
||||
|
||||
db = ProductController()
|
||||
db.create(product)
|
||||
|
||||
return redirect('/products/ownproducts')
|
||||
|
||||
|
||||
@blueprint.post('/update/<int:id>')
|
||||
def update_product(id: int):
|
||||
""" Processes a request to update a product in place on the site """
|
||||
# Ensure that the product belongs to the current user
|
||||
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!")
|
||||
return redirect("/")
|
||||
|
||||
db = ProductController()
|
||||
product = db.read_id(id)
|
||||
|
||||
if product.sellerID != user_id:
|
||||
flash("This product does not belong to you!")
|
||||
return redirect("/ownproducts")
|
||||
|
||||
# Update product details
|
||||
product.name = request.form.get('name')
|
||||
product.description = request.form.get('description')
|
||||
product.category = request.form.get('category')
|
||||
product.image = request.form.get('image')
|
||||
product.cost = request.form.get('cost')
|
||||
product.quantityAvailable = request.form.get('quantity')
|
||||
|
||||
db.update(product)
|
||||
flash("Product successfully updated")
|
||||
return redirect(f"/{product.id}")
|
||||
|
||||
|
||||
@blueprint.route('/ownproducts')
|
||||
def display_own_products():
|
||||
""" Display products owned by the currently logged in seller """
|
||||
|
Reference in New Issue
Block a user