Exemplo n.º 1
0
def update_product(product_id):
    product = Product.query.get_or_404(product_id)
    form = ProductForm()
    valid_input = True
    if product.company != current_user:
        abort(Constants.FORBIDDEN_PAGE_ERROR_PAGE)
    if form.validate_on_submit():
        if form.validate_name(form.name):
            product.name = form.name.data
        else:
            valid_input = False
            flash("Username already being used. Please try again", "danger")
        if form.company.data and form.company.data != product.company.name:
            product.company = Company.query.filter_by(
                name=form.company.data).first()
        if form.price.data and form.price.data != product.price:
            product.price = form.price.data
        if form.description.data and form.description.data != product.description:
            product.description = form.description.data
        if form.image_file.data and form.image_file.data != product.image_file:
            product.image_file = form.image_file.data
        if valid_input:
            database.session.commit()
            flash("Your product has been updated!", "success")
        return redirect(url_for("products.product", product_id=product.id))
    elif request.method == "GET":
        form.name.data = product.name
        if product.company:
            form.company.data = product.company.name
        form.price.data = product.price
        form.description.data = product.description
    return render_template("new_product.html",
                           legend="Update Product",
                           form=form)
Exemplo n.º 2
0
def add_product():
    form = ProductForm()
    if form.validate_on_submit():
        product = Product(name=form.name.data,
                          origin=form.origin.data,
                          organic=form.organic.data,
                          fair_trade=form.fair_trade.data,
                          crop=form.crop.data,
                          comment=form.comment.data,
                          colour=form.colour.data,
                          product_family_id=form.product_family_id.data.id)
        db.session.add(product)
        db.session.commit()
        flash('Le produit a été créé.', 'success')
        return redirect(url_for('products.add_product'))
    page = request.args.get('page', 1, type=int)
    pagination = Product.query.order_by(
        Product.product_family_id.asc(),
        Product.name.asc()).paginate(page,
                                     current_app.config['PRODUCTS_PER_PAGE'],
                                     False)
    products = pagination.items
    return render_template('products/add_product.html',
                           title='Ajouter un Produit',
                           form=form,
                           products=products,
                           pagination=pagination)
Exemplo n.º 3
0
    def post(self, id):
        product = Products.query.get(id)
        form = ProductForm()

        if form.validate_on_submit():
            # Checks to see if the user has already started a cart.
            if 'cart' in session:
                # If the product is not in the cart, then add it.
                if not any(product.name in d.keys() for d in session['cart']):
                    session['cart'].append({
                        product.name: {
                            'q': form.quantity.data,
                            'p': product.price,
                            't': form.quantity.data * product.price
                        }
                    })

                # If the product is already in the cart, update the quantity
                elif any(product.name in p.keys() for p in session['cart']):
                    print(session['cart'])
                    for p in session['cart']:
                        if product.name in p.keys():
                            p[product.name]['q'] = form.quantity.data
                            p[product.
                              name]['t'] = form.quantity.data * product.price
                    # d.update((k, form.quantity.data) for k, v in d.items() if k == product.name)

            else:
                # In this block, the user has not started a cart, so we start it for them and add the product.
                session['cart'] = [{
                    product.name: {
                        'q': form.quantity.data,
                        'p': product.price,
                        't': form.quantity.data * product.price
                    }
                }]

            flash("Successfully added to cart.")
            return redirect("/cart")

        return make_response(
            render_template("products/product_details.html",
                            product=product,
                            title=Products.query.get(id).name,
                            form=form,
                            fix=fix), 200, headers)
Exemplo n.º 4
0
 def get(self, id):
     product = Products.query.get(id)
     form = ProductForm()
     return make_response(
         render_template("products/product_details.html",
                         product=product,
                         title=Products.query.get(id).name,
                         form=form,
                         fix=fix), 200, headers)
Exemplo n.º 5
0
def add_product():
    form = ProductForm()
    if request.method == 'POST':
        product = Products(product_name=form.product.data, quantity= form.quantity.data)
        db.session.add(product)
        db.session.commit()
        flash("Your Product has been inserted")
        return redirect(url_for('products.products'))
    return render_template('add_product.html', form=form, title='Add Product')
Exemplo n.º 6
0
def edit_product(product_id):
    form = ProductForm(request.form)
    my_product = Products.query.filter_by(product_id=product_id).first()
    if my_product and request.method=="POST":
        my_product.product_name = form.product.data
        my_product.quantity = form.quantity.data
        db.session.commit()
        flash("Record Updated Successfully","success")
        return redirect(url_for('products.products'))
    return render_template('edit_product.html',form=form ,product_id=my_product.product_id, title="Edit Product")
Exemplo n.º 7
0
def new_product():
    form = ProductForm()
    form.company.choices = [("Other", "Other")] + [
        (company.name, company.name) for company in Company.query.all()
    ]
    if form.validate_on_submit():
        if not form.validate_name(form.name):
            flash("Product name already being used. Please try again",
                  "danger")
        else:
            company = Company.query.filter_by(name=form.company.data).first()
            product = Product(name=form.name.data,
                              company=company,
                              price=form.price.data,
                              description=form.description.data,
                              image_file=form.image_file.data)
            database.session.add(product)
            database.session.commit()
            flash("Your product has been posted!", "success")
            product = Product.query.filter_by(
                name=form.name.data).first_or_404()
            return redirect(url_for("products.product", product_id=product.id))
    return render_template("new_product.html", legend="New Product", form=form)