From 3ce5d166510c19a7f7f9edd7e42806b331d1e932 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 19 Jan 2024 16:35:23 +0000 Subject: [PATCH] #3 Added ability for images to be uploaded to the site --- .gitignore | 1 + controllers/web/product.py | 14 ++++++++------ templates/content.html | 2 +- templates/new_product.html | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index af5171e..85263a3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ ### SQLite 3 Database ### data/ +static/assets/img/products/ ### CICD Registration files ### cicd/runner-data diff --git a/controllers/web/product.py b/controllers/web/product.py index 64f5d94..c68ea69 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -8,7 +8,10 @@ from controllers.database.category import CategoryController from controllers.database.user import UserController from datetime import datetime -from werkzeug import secure_filename +from werkzeug.utils import secure_filename +import os +import uuid +import pathlib ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} @@ -101,18 +104,17 @@ def add_product(): file = request.files.get('image') # Ensure that the correct file type is uploaded - if file == None OR NOT allowed_file(file.filename): + if file == None or not allowed_file(file.filename): flash("Invalid File Uploaded") return redirect("/add") # Create the product object and push to database - filename = secure_filename(file.filename) - file.save(os.path.join('static/assets/img/products/', secure_filename)) - file.save + filename = str(uuid.uuid4()) + pathlib.Path(file.filename).suffix + file.save(os.path.join('static/assets/img/products/', filename)) product = Product( request.form.get('name'), - filename + filename, request.form.get('description'), request.form.get('cost'), request.form.get('category'), diff --git a/templates/content.html b/templates/content.html index 1b98811..680098e 100644 --- a/templates/content.html +++ b/templates/content.html @@ -7,7 +7,7 @@
{{product.name}}
- Brake Disks + Brake Disks
£{{product.cost}}
diff --git a/templates/new_product.html b/templates/new_product.html index ea2fa2e..59d8184 100644 --- a/templates/new_product.html +++ b/templates/new_product.html @@ -6,7 +6,7 @@