Exemplo n.º 1
0
def update_product(id):
    if session['role'] == 'seller':
        product = Product.query.get_or_404(id)
        if product.seller != current_user:
            abort(403)
        form = ProductForm()
        if form.validate_on_submit():
            product.name = form.name.data
            product.description = form.description.data
            product.category = form.category.data
            product.price = form.price.data
            product.quantity = form.quantity.data
            if product.photo_name is None:
                product.photo_name = save_picture(form.photo.data)
            elif form.photo.data:
                pathp = app.root_path + '\static\product_pics\{}'.format(
                    product.photo_name)
                os.remove(pathp)
                product.photo_name = save_picture(form.photo.data)
            db.session.commit()
            flash('Product has been updated!', 'check')
            return redirect(url_for('seller_dashboard'))
        elif request.method == 'GET':
            form.name.data = product.name
            form.category.data = product.category
            form.description.data = product.description
            form.price.data = product.price
            form.quantity.data = product.quantity
        return render_template('new_product.html',
                               legend='Update Product',
                               form=form)
    else:
        flash('Only Seller can access this page', 'warning')
        return redirect(url_for('customer_home'))
Exemplo n.º 2
0
def new_product():
    if session['role'] == 'seller':
        form = ProductForm()
        if form.validate_on_submit():
            product = Product(name=form.name.data,
                              description=form.description.data,
                              category=form.category.data,
                              price=form.price.data,
                              quantity=form.quantity.data,
                              photo_name=save_picture(form.photo.data),
                              seller=current_user)
            db.session.add(product)
            db.session.commit()
            flash('Product has been added successfully!', 'check')
            return redirect(url_for('seller_dashboard'))
        return render_template('new_product.html',
                               legend="Add Product",
                               form=form)
    else:
        flash('Only Seller can access this page', 'warning')
        return redirect(url_for('customer_home'))