diff --git a/controllers/web/product.py b/controllers/web/product.py index 9e872bb..64f5d94 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -1,10 +1,21 @@ from flask import Blueprint from flask import render_template, session, flash, request, redirect + +from models.products.product import Product from controllers.database.product import ProductController from controllers.database.category import CategoryController from controllers.database.user import UserController +from datetime import datetime +from werkzeug import secure_filename + +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} + +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + blueprint = Blueprint("products", __name__, url_prefix="/products") # Global context to enable the categories to be accessed @@ -54,7 +65,7 @@ def id(id: int): # Launches the page to add a new product to the site @blueprint.route('/add') -def add_product(): +def display_add_product(): user_id = session.get('user_id') # User must be logged in to view this page @@ -69,3 +80,47 @@ def add_product(): return redirect('/') return render_template('index.html', content='new_product.html') + + +# Processes a request to add a new product to the site +@blueprint.post('/add') +def add_product(): + user_id = session.get('user_id') + + # User must be logged in to view this page + if user_id == None: + flash("Please Login to view this page") + return redirect('/login', code=302) + + db = UserController() + user = db.read_id(user_id) + if user == None or user.role != "Seller": + flash("You must be logged in as a Seller to perform this action") + return redirect('/', code=302) + + file = request.files.get('image') + + # Ensure that the correct file type is uploaded + 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 + + product = Product( + request.form.get('name'), + filename + request.form.get('description'), + request.form.get('cost'), + request.form.get('category'), + user.id, + datetime.now(), + request.form.get('quantity') + ) + db = ProductController() + db.create(product) + + return render_template('index.html', content='new_product.html') diff --git a/docker-compose.yml b/docker-compose.yml index 98d9fda..1946300 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ services: - APPSECRET=test - ENVIRON=test # - ENVIRON=prod + - FILESTORE=static/assets/img/products tty: true ports: - "5000:5000" diff --git a/templates/new_product.html b/templates/new_product.html index 882d66d..ea2fa2e 100644 --- a/templates/new_product.html +++ b/templates/new_product.html @@ -1,18 +1,20 @@

Sign Up

-
+ - + - + + + +
-

Already have an account? Login

+

Want to view all of your products? Click Here