From 0951bcc23e9a0fe6c3d8449f765854b4b109d882 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 24 Jan 2024 18:07:41 +0000 Subject: [PATCH] #4 #6 Base creation of a product viewing page with the ability to edit as a seller --- controllers/database/product.py | 28 ++++++++++++++-- controllers/web/product.py | 41 ++++++++++++++++++++++-- static/css/loginform.css | 21 +++++++++--- static/css/products.css | 20 ++++++++++++ templates/content.html | 2 +- templates/index.html | 1 - templates/login.html | 6 ++-- templates/new_product.html | 25 ++++++++++----- templates/product.html | 57 +++++++++++++++++++++++++++++++++ templates/signup.html | 20 ++++++++---- 10 files changed, 193 insertions(+), 28 deletions(-) create mode 100644 templates/product.html diff --git a/controllers/database/product.py b/controllers/database/product.py index c232aa9..d6c35a4 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -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") diff --git a/controllers/web/product.py b/controllers/web/product.py index 7212088..5b0b9e3 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -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/') +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 """ diff --git a/static/css/loginform.css b/static/css/loginform.css index 7ce757d..cec1f1d 100644 --- a/static/css/loginform.css +++ b/static/css/loginform.css @@ -2,7 +2,8 @@ h2 { font-weight:300; text-align:center; } -#login-form-wrap { + +#input-form-wrap { background-color: rgba(255, 255, 255, .15); backdrop-filter: blur(200px); width: 35%; @@ -11,7 +12,8 @@ h2 { border-radius: 4px; box-shadow: 0px 30px 50px 0px rgba(0, 0, 0, 0.2); } -.login-form { + +.input-form { padding: 1em 2em; display: flex; flex-direction: column; @@ -19,7 +21,13 @@ h2 { gap: 1em; } -.login-form input, .login-form select, .login-form option { +.input-form-row { + display: flex; + flex-direction: row; + gap: 1em 1em; +} + +.input-form input, .input-form select, .input-form option, .input-form textarea { width: 100%; padding: 0 0 0 10px; margin: 0; @@ -48,7 +56,12 @@ h2 { } } -.login-form input[type="submit"] { +.input-form textarea { + min-height: 120px; + max-width: 100%; +} + +.input-form input[type="submit"] { border: none; display:block; background-color: rgba(255, 255, 255, .10); diff --git a/static/css/products.css b/static/css/products.css index b15b85f..ebfce71 100644 --- a/static/css/products.css +++ b/static/css/products.css @@ -42,12 +42,32 @@ justify-content: space-between; flex-wrap: wrap; flex: 4 0 1rem; + max-width: 40%; padding: .5rem 1rem 2rem 2rem; background: var(--bg-secondary); border-radius: .5rem .5rem; transition: all 0.2s; } +.product-fs { + height: 80%; + width: 80%; + font-size: 150%; + display: flex; + flex-direction: column; + justify-content: space-between; + flex-wrap: wrap; + padding: .5rem 1rem 2rem 2rem; + background: var(--bg-secondary); + border-radius: .5rem .5rem; + transition: all 0.2s; +} + +.product-title { + font-size: 130%; + font-weight: bold; +} + .product-information { display: flex; flex-direction: row; diff --git a/templates/content.html b/templates/content.html index 234e95c..d96f5ef 100644 --- a/templates/content.html +++ b/templates/content.html @@ -16,6 +16,6 @@
- {% endfor %} + {% endfor %} {% endif %} diff --git a/templates/index.html b/templates/index.html index 94b51cf..6c8d582 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,6 @@ - diff --git a/templates/login.html b/templates/login.html index 30e3137..438b5be 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,6 +1,8 @@ -
+ + +

Login

-