示例#1
0
def rm_from_cart():
    cart_name = _post_or_get("cart_name")
    item_id = _post_or_get("item_id")

    if None in (cart_name, item_id):
        return jsonify({"status": "error"})

    if cart_name not in session:
        session[cart_name] = {}

    # remove item from the session cart
    if item_id in session[cart_name]:
        del session[cart_name][item_id]

    # remove the item from the order as well
    order_obj = None
    if "current_order" not in session:
        cart_dic = session[cart.CartType.SHOPPING] if cart.CartType.SHOPPING in session else {}
        order_obj = cart.to_order(cart_dic)
    else:
        order_obj = orders.Order.unserialize(session["current_order"])
    cart.dic_to_order(session[cart_name], order_obj)
    session["current_order"] = order_obj.serialize(json_friendly=True)

    return jsonify({"status": "ok", "cart": session[cart_name]})
示例#2
0
def _session_to_order():
    """
    Converts a JSON key/val in session into an order (if it exists)
    """
    order_obj = None
    if "current_order" not in session:
        cart_dic = session[cart.CartType.SHOPPING] if cart.CartType.SHOPPING in session else {}
        order_obj = cart.to_order(cart_dic)
    else:
        cart_dic = session[cart.CartType.SHOPPING] if cart.CartType.SHOPPING in session else {}
        order_obj = orders.Order.unserialize(session["current_order"])
        cart.dic_to_order(cart_dic, order_obj)

    # append user_id to order
    if current_user is not None and not current_user.is_anonymous():
        order_obj.user_id = current_user._id

    return order_obj