#3 Create product form made, data is processed on the backend. Image data not currently saved
This commit is contained in:
parent
b821050869
commit
98a17bdc43
@ -1,10 +1,21 @@
|
|||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
|
|
||||||
from flask import render_template, session, flash, request, redirect
|
from flask import render_template, session, flash, request, redirect
|
||||||
|
|
||||||
|
from models.products.product import Product
|
||||||
from controllers.database.product import ProductController
|
from controllers.database.product import ProductController
|
||||||
from controllers.database.category import CategoryController
|
from controllers.database.category import CategoryController
|
||||||
from controllers.database.user import UserController
|
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")
|
blueprint = Blueprint("products", __name__, url_prefix="/products")
|
||||||
|
|
||||||
# Global context to enable the categories to be accessed
|
# 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
|
# Launches the page to add a new product to the site
|
||||||
@blueprint.route('/add')
|
@blueprint.route('/add')
|
||||||
def add_product():
|
def display_add_product():
|
||||||
user_id = session.get('user_id')
|
user_id = session.get('user_id')
|
||||||
|
|
||||||
# User must be logged in to view this page
|
# User must be logged in to view this page
|
||||||
@ -69,3 +80,47 @@ def add_product():
|
|||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
return render_template('index.html', content='new_product.html')
|
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')
|
||||||
|
@ -8,6 +8,7 @@ services:
|
|||||||
- APPSECRET=test
|
- APPSECRET=test
|
||||||
- ENVIRON=test
|
- ENVIRON=test
|
||||||
# - ENVIRON=prod
|
# - ENVIRON=prod
|
||||||
|
- FILESTORE=static/assets/img/products
|
||||||
tty: true
|
tty: true
|
||||||
ports:
|
ports:
|
||||||
- "5000:5000"
|
- "5000:5000"
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
<div id="login-form-wrap">
|
<div id="login-form-wrap">
|
||||||
<h2>Sign Up</h2>
|
<h2>Sign Up</h2>
|
||||||
<form class="login-form" method="POST">
|
<form class="login-form" method="POST" enctype="multipart/form-data">
|
||||||
<input type="text" id="name" name="name" placeholder="Product Name" required>
|
<input type="text" id="name" name="name" placeholder="Product Name" required>
|
||||||
<input type="textarea" id="description" name="description" placeholder="Product Description" required>
|
<input type="textarea" id="description" name="description" placeholder="Product Description" required>
|
||||||
<input type="file" id="image" name="image" 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.name}}">{{category.name}}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
<input type="submit" id="Sign Up" value="Sign Up">
|
<input type="number" id="cost" name="cost" placeholder=10.99 min=0 step=any required>
|
||||||
|
<input type="number" id="quantity" name="quantity" placeholder=0 min=0 required>
|
||||||
|
<input type="submit" id="Create Product" value="Create Product">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div id="create-account-wrap">
|
<div id="create-account-wrap">
|
||||||
<p>Already have an account? <a href="login">Login</a><p>
|
<p>Want to view all of your products? <a href="">Click Here</a><p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user