Exemple #1
0
def add_product():
    form = ProductsForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()

        product = Products()
        product.title = form.title.data
        product.description = form.description.data
        product.location = form.location.data
        product.price = form.price.data
        product.is_private = form.is_private.data
        product.category = form.category.data

        #save image
        f = form.photo.data
        if f:
            filename = secure_filename(f.filename)
            f.save(os.path.join(app.config['UPLOAD_PATH'], filename))
            product.photo = filename

        #тут через раз падает с багом lazy
        current_user.products.append(product)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/my_products')
    return render_template('product.html',
                           title='Добавление товара',
                           form=form)
Exemple #2
0
def add_product():
    form = ProductsForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        products = Products()
        products.title = form.title.data
        products.quantity = form.quantity.data
        products.price = form.price.data
        products.description = form.description.data
        products.category = form.category.data
        if form.photo.data:
            fname, fext = os.path.splitext(secure_filename(form.photo.data.filename))
            filepath = os.path.join(app.root_path, 'static', "user_photos", fname + fext)
            while os.path.exists(filepath):
                fname += "_1"
                filepath = os.path.join(app.root_path, 'static', "user_photos", fname + fext)
            form.photo.data.save(filepath)
            products.photo_path = os.path.join('..', 'static', "user_photos", fname + fext)
        current_user.products.append(products)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/')
    return render_template('products.html', title='Добавление товара', form=form)