Exemple #1
0
def address(request, **kwargs):
    """
    地址管理
    """
    UserBasic = kwargs.pop("Mem")
    
    if request.method == "POST":
        if 'add_address' in request.POST:
            address = user_address(user = UserBasic)
            UserAddress = AddressForm(request.POST, instance=address) 
            print UserAddress
            if UserAddress.is_valid():
                UserAddress.save()
        elif 'delete_address' in request.POST:
            print '222'
        elif 'change_address' in request.POST:
            print '333'
    else:
        UserAddress = AddressForm   
        include_template = 'store/add_address.html'
    template_name = 'store/order.html'
    ctx= {
        'UserBasic': UserBasic,
        'include_template': include_template,
        'UserAddress': UserAddress,
        'sum_price':request.session.get('sum_price',0)
    }
    return render_to_response(template_name,RequestContext(request, ctx))
Exemple #2
0
def order(request, **kwargs):
    """
    生成订单
    """
    UserBasic = kwargs.pop("Mem")
    StuffList = request.GET.get("stuff_list",None)
    sum_price = 0
    print StuffList
    error = ""
    if StuffList:
        request.session['StuffList'] = StuffList
    
    template_name = "store/order.html"
    
    if request.method == "POST":
        if 'add_address' in request.POST:
            address = user_address(user = UserBasic)
            UserAddressForm = AddressForm(request.POST, instance=address) 
            print UserAddressForm
            if UserAddressForm.is_valid():
                UserAddressForm.save()
        elif 'order' in request.POST:
            
            password_2nd = request.POST.get("password_2nd")
            result = verify_password_2nd(password_2nd,request.user)

            if result:
                lock = threading.Lock()
                StuffList = request.session.get('StuffList')
                print StuffList
                address_id = request.POST.get("address_id")
                print address_id

                UserMaxMem = user_max_mem.objects.get(user_mid = user_mid_mem.objects.get(user = UserBasic))
                lock.acquire()
                try:
                    StuffList = StuffList.split("_")
                    success = False
                    x = 0
                    address = user_address.objects.get(id = address_id)
                    MyOrder = my_order(max = UserMaxMem,state = "wait",address = address)
                    MyOrder.save()
                    while x < len(StuffList)-1:
                        print x,StuffList
                        Stuff = stuff.objects.get(id = StuffList[x])
                        num = int(StuffList[x+1])
                        if Stuff.total_num >= num:
                            Stuff.total_num -=  num
                            price_total = Stuff.price_single * num
                            sum_price += price_total
                            UserBasic.store_cash -= Decimal(price_total)
                            UserBasic.save()
                            Stuff.save()
                            OrderStuff = order_stuff(order = MyOrder,stuff = Stuff,amount =num, price_total = price_total)
                            OrderStuff.save()
                            x += 2
                            success = True
                        else:
                            error += Stuff.name+"没有足够的数量,所以没有购买成功"
                    if success:
                        extra_context = UserBasic.number+"有新的订单"
                        admin = User.objects.filter(is_superuser = True)
                        notification.send(admin,"order_notice",extra_context,True, request.user)
                        url = reverse('stuff_order_records')+"?error="+error
                        return HttpResponseRedirect(url)
                finally:
                    lock.release()
            else:
                error = "二级密码错误"
    else:
        x = 0
        try:
            StuffList = StuffList.split("_")
            while x < len(StuffList)-1:
                print x,StuffList
                Stuff = stuff.objects.get(id = StuffList[x])
                num = int(StuffList[x+1])
                price_total = Stuff.price_single * num
                sum_price += price_total
                x += 2
            request.session['sum_price'] = sum_price
        except:
            print sum_price
        UserAddressForm = AddressForm
    UserAddress = user_address.objects.filter(user = UserBasic).order_by("-id")
    if not UserAddress:
        display = "block"
    else:
        display = "none"
    include_template = 'store/address.html'
    print error,'test'
    ctx = {
        'include_template':include_template,
        'UserAddress': UserAddress,
        'UserBasic': UserBasic,
        'display':display,
        'UserAddressForm':AddressForm,
        'sum_price':request.session.get('sum_price',0),
        "error":error
    }
    return render_to_response(template_name,RequestContext(request,ctx))