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

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

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
### SQLite 3 Database ### ### SQLite 3 Database ###
data/ data/
static/assets/img/products/
### CICD Registration files ### ### CICD Registration files ###
cicd/runner-data cicd/runner-data

View File

@ -8,7 +8,10 @@ from controllers.database.category import CategoryController
from controllers.database.user import UserController from controllers.database.user import UserController
from datetime import datetime 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'} ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
@ -101,18 +104,17 @@ def add_product():
file = request.files.get('image') file = request.files.get('image')
# Ensure that the correct file type is uploaded # 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") flash("Invalid File Uploaded")
return redirect("/add") return redirect("/add")
# Create the product object and push to database # Create the product object and push to database
filename = secure_filename(file.filename) filename = str(uuid.uuid4()) + pathlib.Path(file.filename).suffix
file.save(os.path.join('static/assets/img/products/', secure_filename)) file.save(os.path.join('static/assets/img/products/', filename))
file.save
product = Product( product = Product(
request.form.get('name'), request.form.get('name'),
filename filename,
request.form.get('description'), request.form.get('description'),
request.form.get('cost'), request.form.get('cost'),
request.form.get('category'), request.form.get('category'),

View File

@ -7,7 +7,7 @@
<div class="product-title">{{product.name}}</div> <div class="product-title">{{product.name}}</div>
<div class="product-information"> <div class="product-information">
<div class="product-image"> <div class="product-image">
<img src="{{url_for('static', filename=product.image)}}" alt="Brake Disks"/> <img src="{{url_for('static', filename='assets/img/products/' + product.image)}}" alt="Brake Disks"/>
</div> </div>
<div class="product-details"> <div class="product-details">
<div class="product-price">£{{product.cost}}</div> <div class="product-price">£{{product.cost}}</div>

View File

@ -6,7 +6,7 @@
<input type="file" id="image" name="image" accept="image/x" required> <input type="file" id="image" name="image" accept="image/x" required>
<select name="category" id="category"> <select name="category" id="category">
{% for category in categories %} {% for category in categories %}
<option value="{{category.name}}">{{category.name}}</option> <option value="{{category.id}}">{{category.name}}</option>
{% endfor %} {% endfor %}
</select> </select>
<input type="number" id="cost" name="cost" placeholder=10.99 min=0 step=any required> <input type="number" id="cost" name="cost" placeholder=10.99 min=0 step=any required>