def pay_ali(request):
    '''username = request.session['login_user']
    total_price = float(request.POST['money'])
    commodity_id_list_str = request.POST['commodity_id_list_str']
    commodity_id_list = commodity_id_list_str[1:].split(',')
    order_list = []
    cart = client_mongodb_options.find_cart(db, username)
    c_list = cart['c_list']
    for item_id in commodity_id_list:
        for commodity in c_list:
            if commodity['commodity_id'] == item_id:
                order_list.append(commodity)
    o_id = uuid.uuid1()
    m_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    state = 0   # 0:未付款,1:已付款
    client_mongodb_options.add_order(db, o_id, username, m_time, total_price, order_list, state)
    pay_url = p_alipay.alipay.create_partner_trade_by_buyer(o_id, "充值测试", "hello zhong", total_price)# 订单号, 商品名, 商品描述, 价钱
    return render(request, 'client/pay.html', {'pay_url': pay_url})'''

    username = request.session['login_user']
    total_price = float(request.POST['money'])
    commodity_id_list_str = request.POST['commodity_id_list_str']
    amount_list_str = request.POST['amount_list_str']
    address = request.POST['address']
    commodity_id_list = commodity_id_list_str[1:].split(',')
    amount_list = amount_list_str[1:].split(',')
    order_list = []
    cart = client_mongodb_options.find_cart(db, username)
    c_list = cart['c_list']
    i = 0
    for item_id in commodity_id_list:
        for commodity in c_list:
            if commodity['commodity_id'] == item_id:
                item_num = amount_list[i]
                commodity['num'] = item_num
                order_list.append(commodity)
                i = i + 1
    o_id = uuid.uuid1()
    m_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    state = '未付款'  # 0:未付款,1:已付款
    client_mongodb_options.add_order(db, o_id, username, m_time, total_price, order_list, state ,address)
    if request.POST['trade_type'] == "0":
        # 担保支付
        pay_url = p_alipay.alipay.create_partner_trade_by_buyer(o_id, "充值测试", "hello zhong",
                                                                total_price)  # 订单号, 商品名, 商品描述, 价钱
    elif request.POST['trade_type'] == "1":
        # 即时到账
        pay_url = p_alipay.alipay.create_direct_pay_by_user(o_id, "充值测试", "hello zhong",
                                                            total_price)  # 订单号, 商品名, 商品描述, 价钱

    # 清空购物车
    c_list = []
    c_num = 0
    client_mongodb_options.update_commodity_to_cart(db, username, c_list, c_num)

    return render(request, 'client/pay.html', {'pay_url': pay_url})
def add_cart(request):
    flag = False
    if "login_user" in request.session:
        username = request.session['login_user']
        flag = True
    if flag:
        classes_list = mongodb_options.find_classes(db)
        brands_list = mongodb_options.find_brands(db)
        brands_list_design = []
        for brand in brands_list:
            if brand['isdesign'] == u'是':
                brands_list_design.append(brand)
        commodity_id = request.POST['commodity_id']
        color = request.POST['color']
        size = request.POST['size']
        commodity = mongodb_options.find_commodity_by_cid(db, commodity_id)
        commodity_dict = {'commodity_id': commodity_id, 'color': color, 'size': size, 'unit_price': commodity['c_price']}

        cart = client_mongodb_options.find_cart(db, username)
        if cart:
            c_list = cart['c_list']
            c_list.append(commodity_dict)
            c_num = len(c_list)
            client_mongodb_options.update_commodity_to_cart(db, username, c_list, c_num)
        else:
            c_list = []
            c_list.append(commodity_dict)
            c_num = len(c_list)
            client_mongodb_options.insert_commodity_to_cart(db, username, c_list, c_num)

        return render(request, 'client/client_cart_info.html',
                      {'username': username, 'flag': flag, 'c_num': c_num, 'commodity_id': commodity_id, 'classes_list': classes_list, 'brands_list': brands_list, 'brands_list_design': brands_list_design})
    else:
        classes_list = mongodb_options.find_classes(db)
        brands_list = mongodb_options.find_brands(db)
        brands_list_design = []
        for brand in brands_list:
            if brand['isdesign'] == u'是':
                brands_list_design.append(brand)
        return render(request, 'client/client_index.html',
                      {'message': '<script type="text/javascript">alert("您还没登录,无法查看!");</script>', 'flag': flag, 'classes_list': classes_list, 'brands_list': brands_list,
                       'brands_list_design': brands_list_design})