def eshop_cart_add(req): do_check_mgc(req) check_token(req, req.json.get('token')) cart = ShoppingCart(req) item_id = req.json.getfirst('item_id', fce=nint) count = req.json.getfirst('count', 0, int) if count < 1: req.state = state.HTTP_BAD_REQUEST req.content_type = 'application/json' return json.dumps({'reason': 'count must bigger then zero'}) item = Item(item_id) if not item.get(req) or item.state != STATE_VISIBLE: req.state = state.HTTP_NOT_FOUND req.content_type = 'application/json' return json.dumps({'reason': 'item not found'}) # append or incrase item cart.merge_items(((item_id, {'name': item.name, 'price': item.price, 'count': count }),)) cart.store(req) cart.calculate() req.content_type = 'application/json' return json.dumps({'reason': 'item append to cart', 'cart': cart.dict()})
def eshop_cart_recapitulation(req): do_check_mgc(req) cart = ShoppingCart(req) cart.calculate() # get method returns HTML Form cfg = Object() cfg.addresses_country = req.cfg.addresses_country cfg.addresses_region = req.cfg.addresses_region cfg.eshop_currency = req.cfg.eshop_currency # all defined transportation (for universal use): for key, val in req.cfg.__dict__.items(): if key.startswith('eshop_transportation_'): cfg.__dict__[key[6:]] = val elif key.startswith('eshop_payment_'): cfg.__dict__[key[6:]] = val # GET method only view shopping cart - no store was needed return generate_page(req, "eshop/shopping_recapitulation.html", token=create_token(req), cfg=cfg, cart=cart)
def eshop_cart_pay_and_order(req): do_check_mgc(req) check_token(req, req.form.get('token'), uri='/eshop/cart/recapitulation') cart = ShoppingCart(req) # TODO: payment page if could be (paypal, card, transfer) order = Order.from_cart(cart) if not order: redirect(req, '/eshop') order.client_id = req.login.id if req.login else None retval = order.add(req) if retval == order: cart.clean(req) send_order_status(req, order) return generate_page(req, "eshop/shopping_accept.html", order=order) if retval[0] == EMPTY_ITEMS: redirect(req, '/eshop') if retval[0] == NOT_ENOUGH_ITEMS: cart.set_not_enought(retval[1]) cart.store(req) redirect(req, '/eshop/cart')
def eshop_cart_address_post(req): do_check_mgc(req) check_token(req, req.form.get('token'), uri='/eshop/cart/address') cart = ShoppingCart(req) way = req.form.getfirst('way', '', str) same_as_billing = 'same_as_billing' in req.form billing_address = Address.bind(req.form, 'billing_') if same_as_billing: shipping_address = billing_address.copy() shipping_address['same_as_billing'] = True else: shipping_address = Address.bind(req.form, 'shipping_') shipping_address['same_as_billing'] = False transportation = req.form.getfirst('transportation', '', str) payment = req.form.getfirst('payment', '', str) if req.login: email = req.login.email emailcheck = email else: email = req.form.getfirst('email', '', str) emailcheck = req.form.getfirst('emailcheck', '', str) transportation_price = req.cfg.__dict__.get( 'eshop_transportation_' + transportation, -1) payment_price = req.cfg.__dict__.get( 'eshop_payment_' + payment, -1) if transportation and transportation_price < 0: raise SERVER_RETURN(state.HTTP_BAD_REQUEST) if payment and payment_price < 0: raise SERVER_RETURN(state.HTTP_BAD_REQUEST) if len(billing_address): cart.billing_address = billing_address if len(shipping_address): cart.shipping_address = shipping_address if transportation: cart.transportation = (transportation, transportation_price) if payment: cart.payment = (payment, payment_price) if re_email.match(email): cart.email = email if re_email.match(emailcheck): cart.emailcheck = emailcheck cart.store(req) # store shopping cart if not billing_address: return eshop_cart_address(req, cart, error='no_billing_address') if len(shipping_address) == 1: # only same_as_billing return eshop_cart_address(req, cart, error='no_shipping_address') if not email or email != emailcheck: return eshop_cart_address(req, cart, error='no_email') if not transportation: return eshop_cart_address(req, cart, error='no_transportation') if not payment: return eshop_cart_address(req, cart, error='no_payment') # end of errors block cart.calculate() if way == 'next': redirect(req, '/eshop/cart/recapitulation') elif way == 'prev': redirect(req, '/eshop/cart') return eshop_cart_address(req, cart)
def eshop_cart_wipe(req): """ /eshop/cart/wipe - wipe esho cart - for debug only """ do_check_login(req) cart = ShoppingCart(req) cart.clean(req) redirect(req, '/eshop/cart')
def eshop_cart(req): do_check_mgc(req) cart = ShoppingCart(req) if req.method == 'PATCH': check_token(req, req.json.get('token'), uri='/eshop/cart') cart.merge_items(req.json.get('items', [])) req.content_type = 'application/json' cart.store(req) # store shopping cart cart.calculate() return json.dumps({'cart': cart.dict()}) cart.calculate() if req.is_xhr: check_origin(req) req.content_type = 'application/json' return json.dumps({'cart': cart.dict()}) # GET method only view shopping cart - no store was needed return generate_page(req, "eshop/shopping_cart.html", token=create_token(req), cfg_currency=req.cfg.eshop_currency, cart=cart)