Esempio n. 1
0
def create_product():
    if 'username' not in session:
        return redirect(request.referrer)

    form = NewProductForm()

    all_users = Database.get_name_of_users()

    form.productOwner.choices = all_users  # All the available names are made to be possible choices in the form

    name = session.get('username')

    form.productOwner.default = name  # The name of the user in clicked edit button's row is made default

    form.process()  # Processes the change in the default

    if request.method == 'POST' and form.is_submitted():
        owner = request.form.get("productOwner")

        product_name = request.form.get("productName")

        # Gets the file name from within the file in form of the posted request
        file = request.files['imageFile']
        filename = secure_filename(file.filename)

        # Saves the file to the designated folder
        file.save(os.path.join(app.config["UPLOAD_PATH"], filename))

        price = request.form.get("productPrice")

        product = Product(product_name, price, filename, owner)

        Database.insert_product(product)

        return redirect('/listProduct')

    return render_template('createProduct.html',
                           title='New Product',
                           form=form)