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(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)