This commit is contained in:
@ -11,12 +11,11 @@ from controllers.database.category import CategoryController
|
||||
from controllers.database.user import UserController
|
||||
|
||||
from datetime import datetime
|
||||
from utils.file_utils import allowed_file
|
||||
from utils.file_utils import allowed_file, save_image, remove_file
|
||||
from utils.user_utils import is_role
|
||||
|
||||
import os
|
||||
import uuid
|
||||
import pathlib
|
||||
import os
|
||||
|
||||
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
||||
|
||||
@ -106,19 +105,11 @@ def add_product():
|
||||
return redirect("/")
|
||||
|
||||
file = request.files.get('image')
|
||||
|
||||
# Ensure that the correct file type is uploaded
|
||||
if file is None or not allowed_file(file.filename):
|
||||
flash("Invalid File Uploaded")
|
||||
return redirect("/add")
|
||||
|
||||
# Create the product object and push to database
|
||||
filename = str(uuid.uuid4()) + pathlib.Path(file.filename).suffix
|
||||
file.save(os.path.join('static/assets/img/products/', filename))
|
||||
image_filename = save_image(file)
|
||||
|
||||
product = Product(
|
||||
request.form.get('name'),
|
||||
filename,
|
||||
image_filename if image_filename is not None else "",
|
||||
request.form.get('description'),
|
||||
request.form.get('cost'),
|
||||
request.form.get('category'),
|
||||
@ -151,17 +142,24 @@ def update_product(id: int):
|
||||
flash("This product does not belong to you!")
|
||||
return redirect("/ownproducts")
|
||||
|
||||
# Save new image file
|
||||
file = request.files.get('image')
|
||||
new_image = save_image(file)
|
||||
|
||||
if new_image is not None:
|
||||
remove_file(os.path.join(os.environ.get('FILESTORE'), product.image))
|
||||
product.image = new_image
|
||||
|
||||
# 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}")
|
||||
return redirect(f"/products/{product.id}")
|
||||
|
||||
|
||||
@blueprint.route('/ownproducts')
|
||||
|
Reference in New Issue
Block a user