#3 Added ability for images to be uploaded to the site

This commit is contained in:
2024-01-19 16:35:23 +00:00
parent 98a17bdc43
commit 3ce5d16651
4 changed files with 11 additions and 8 deletions

View File

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