def shipping(): form = ShippingForm() form.address.set_selects(filter_noship=True) order = Order.load(current_user) customer = order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): if not customer: customer = Customer() db.session.add(customer) if customer.current_order is not order: customer.current_order = order customer.shipping_address = form.address.get_or_create_address() if not form.comments.data: form.comments.data = None if form.comments.data != order.shipping_comments: order.shipping_comments = form.comments.data db.session.commit() return redirect(url_for('shop.billing')) try: form.address.populate_from_address(customer.shipping_address) form.comments.data = order.shipping_comments except AttributeError: form.address.country.data = 'USA' return render_template('shop/shipping.html', form=form, order=order)
def checkout(): guest = request.args.get('guest') or False current_order = Order.load(current_user) form = CheckoutForm() form.billing_address.set_selects() form.shipping_address.set_selects(filter_noship=True) customer = current_order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): print('Nonce: {}'.format(form.nonce.data)) if not customer: customer = Customer() if customer.current_order is not current_order: customer.current_order = current_order if not current_user.is_anonymous and not current_user.customer_data: current_user.customer_data = customer if not form.billing_address.equals_address(customer.billing_address): customer.billing_address = ( form.billing_address.get_or_create_address() ) if not form.shipping_address.equals_address(customer.shipping_address): if form.billing_address.form == form.shipping_address.form: customer.shipping_address = customer.billing_address else: customer.shipping_address = ( form.shipping_address.get_or_create_address() ) if form.shipping_comments.data: current_order.shipping_comments = form.shipping_notes.data elif current_order.shipping_comments: current_order.shipping_comments = None current_order.status = Order.PENDING_REVIEW db.session.add_all([current_order, customer]) db.session.commit() if current_user.is_anonymous: customer.save_id_to_session() flash('All fields valid.') return redirect(url_for('shop.review_order')) # Since as of writing this wtforms has a bug in which `None` is coerced # to a string in select fields, I'm using whether or not `form.submit` has # been pressed to know if the default countries need to be set or not. -N if not form.review_order.data: try: form.billing_address.populate_from_address( customer.billing_address ) form.shipping_address.populate_from_address( customer.shipping_address ) except AttributeError: form.billing_address.country.data = 'USA' form.shipping_address.country.data = 'USA' if current_order.shipping_comments: form.shipping_comments.data = current_order.shipping_notes return render_template('shop/checkout.html', current_order=current_order, guest=guest, form=form)