Example #1
0
def marketplace_shipping_method_edit(shipping_method_id):
    website_settings = MSettings.query.first()
    shipping_method = MShippingMethod.query.filter_by(
        id=shipping_method_id).filter_by(seller=current_user).first_or_404()
    form = SShippingMethodForm(obj=shipping_method)
    currencies = MCurrency.query.all()
    if request.method == 'POST':
        if form.validate_on_submit():
            valid = True
            for currency in currencies:
                input_field = 'price[{}]'.format(currency.id)
                if not request.form.get(input_field):
                    valid = False
                    form.errors[input_field] = ['This Field is Required']
            if not valid:
                form.process(request.form)
                return render_template(
                    'marketplace/seller/shipping_methods/add-edit.html',
                    form=form,
                    website_settings=website_settings,
                    currencies=currencies)

            shipping_method.name = form.name.data
            shipping_method.seller = current_user
            db.session.add(shipping_method)
            db.session.commit()
            db.session.refresh(shipping_method)
            for currency in currencies:
                input_field = 'price[{}]'.format(currency.id)
                shipping_method_price = MShippingMethodPrice.query.filter_by(
                    shipping_method=shipping_method).filter_by(
                        price_currency=currency).first()
                if not shipping_method_price:
                    shipping_method_price = MShippingMethodPrice(
                        shipping_method=shipping_method,
                        price_currency=currency,
                    )
                shipping_method_price.price = request.form.get(input_field)
                db.session.add(shipping_method_price)
                db.session.commit()
            flash(
                'Shipping Method {} successfully Updated'.format(
                    shipping_method.name), 'success')
            return redirect(
                url_for('marketplace.marketplace_shipping_methods'))
    return render_template('marketplace/seller/shipping_methods/add-edit.html',
                           form=form,
                           currencies=currencies,
                           website_settings=website_settings)
Example #2
0
def marketplace_shipping_method_create():
    form = MShippingMethodForm()
    currencies = MCurrency.query.all()
    if request.method == 'POST':
        if form.validate_on_submit():
            valid = True
            for currency in currencies:
                input_field = 'price[{}]'.format(currency.id)
                if not request.form.get(input_field):
                    valid = False
                    form.errors[input_field] = ['This Field is Required']
            if not valid:
                form.process(request.form)
                return render_template('admin/marketplace/shipping_methods/add-edit.html', form=form,
                                       currencies=currencies)
            shipping_method = MShippingMethod(
                name=form.name.data,
                seller=form.seller.data
            )
            db.session.add(shipping_method)
            db.session.commit()
            db.session.refresh(shipping_method)
            for currency in currencies:
                input_field = 'price[{}]'.format(currency.id)
                shipping_method_price = MShippingMethodPrice(
                    shipping_method=shipping_method,
                    seller=form.seller.data,
                    price_currency=currency,
                    price=request.form.get(input_field)
                )
                db.session.add(shipping_method_price)
                db.session.commit()
            flash('Shipping Method {} successfully created'.format(shipping_method.name), 'success')
            return redirect(url_for('admin.marketplace_shipping_methods'))
    return render_template('admin/marketplace/shipping_methods/add-edit.html', form=form, currencies=currencies)