def undo_remove_product(product_number, quantity): current_order = Order.load(current_user) if not current_order: current_order = Order() if not current_user.is_anonymous: current_user.current_order = current_order line = current_order.add_line( product_number=product_number, quantity=quantity ) current_order.save() flash('Returned {} of "{}" to cart.'.format(line.quantity, line.label)) return redirect(request.args.get('origin') or url_for('shop.cart'))
def cart(): current_order = Order.load(current_user) form = ShoppingCartForm(obj=current_order) try: if current_order.status == Order.PENDING_REVIEW: form.checkout.label.text = 'Review Order' except AttributeError: pass if form.validate_on_submit(): for line in form.lines.entries: current_order.change_line_quantity( line.product_number.data, line.quantity.data ) current_order.save() if form.checkout.data: return redirect(url_for('shop.shipping')) if form.save.data: flash('Changes saved.') return redirect(url_for('shop.cart')) else: print(form.errors) return render_template( 'shop/cart.html', current_order=current_order, form=form )
def billing(): # TODO: Handle returning customers. form = BillingForm() form.address.set_selects() order = Order.load(current_user) customer = order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): if form.same_as_shipping.data: customer.billing_address = customer.shipping_address else: customer.shipping_address = form.address.get_or_create_address() session['stripe_token'] = form.stripeToken.data db.session.commit() return redirect(url_for('shop.review')) try: form.address.populate_from_address(customer.billing_address) except AttributeError: form.address.country.data = "USA" return render_template( 'shop/billing.html', form=form, order=order, shipping=customer.shipping_address )
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 add_order(): form = OrderForm() if form.validate_on_submit(): try: products = Product.query.filter( Product.id.in_([product.id for product in form.products.data])) order = Order(client=form.client.data, products=products.all(), total_price=products.with_entities( func.sum(Product.price))) db.session.add(order) db.session.query(Product).filter( Product.id.in_(products.with_entities(Product.id))).update( dict(in_stock=False), synchronize_session='fetch') db.session.commit() return redirect('/add-order') except: db.session.rollback() return render_template('add_order.html', form=form)
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)
def add_to_cart(product_number): form = AddProductForm(prefix=product_number) if form.validate_on_submit: qty = form.quantity.data pn = product_number current_order = Order.load(current_user) if not current_order: current_order = Order() if not current_user.is_anonymous: current_user.current_order = current_order line = current_order.add_line(product_number=pn, quantity=qty) current_order.save() flash( 'Added {0} of "{1}" to shopping cart.'.format(line.quantity, line.label) ) return redirect(request.args.get('origin') or url_for('shop.cart'))
def get_stats(): total_by_manufacturer = Product.get_total_price_by_every_manufacturer() total_by_category = Product.get_total_price_by_every_category() total_by_month = Order.get_total_price_by_every_month() return render_template('stats.html', total_by_manufacturer=total_by_manufacturer, total_by_category=total_by_category, total_by_month=total_by_month)
def remove_product(product_number): """Remove the line containing given product from the cart.""" current_order = Order.load(current_user) origin = request.args.get('origin') line = current_order.get_line(product_number) flash( 'Removed {} of "{}" from cart. [<a href="{}">UNDO</a>]'.format( line.quantity, line.label, url_for( 'shop.undo_remove_product', product_number=line.product_number, quantity=line.quantity, origin=origin ) ) ) current_order.delete_line(line) return redirect(origin or url_for('shop.cart'))