Пример #1
0
def orderproduct(request, id):  #rezervasyon
    url = request.META.get("HTTP_REFERER")  # gelinen url
    category = Category.objects.all()
    setting = Setting.objects.get(pk=1)
    current_user = request.user
    product = Car.objects.get(id=id)
    total = Calculate.objects.all().order_by('-id')[0].day * product.price
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():  # form geçerli ise
            data = Order()
            data.first_name = form.cleaned_data[
                'first_name']  # formdan veriyi getiriyor
            data.last_name = form.cleaned_data[
                'last_name']  # formdan veriyi getiriyor
            data.city = form.cleaned_data['city']  # formdan veriyi getiriyor
            data.phone = form.cleaned_data['phone']  # formdan veriyi getiriyor
            data.date_start = Calculate.objects.all().order_by(
                '-id')[0].date_start
            data.date_end = Calculate.objects.all().order_by('-id')[0].date_end
            data.quatity = Calculate.objects.all().order_by('-id')[0].day
            data.address = form.cleaned_data['address']
            data.country = form.cleaned_data['country']
            data.user_id = current_user.id
            data.car_id = id
            data.total = total
            data.ip = request.META.get('REMOTE_ADDR')
            ordercode = get_random_string(5).upper()
            data.code = ordercode
            data.save()

            orderproduct = OrderProduct()
            orderproduct.user_id = current_user.id
            product.amount = product.amount - 1
            product.save()
            Calculate.objects.all().order_by('-id')[0].delete()
            messages.success(request,
                             "Rezervasyonunuz Yapıldı\nCode %s" % ordercode)
            return HttpResponseRedirect(url)
            # return HttpResponseRedirect("/")
        else:
            messages.warning(request, form.errors)
            return HttpResponseRedirect(url)
    form = OrderForm()
    profile = UserProfile.objects.get(user_id=current_user.id)
    context = {
        'category': category,
        'total': total,
        'form': form,
        'profile': profile,
        'setting': setting
    }
    return render(request, 'Order_Form.html', context)
Пример #2
0
def ordercreate(request):
    totail = 0  # 商品总价
    totail_num = 0
    for i in request.session['order_add']:
        totail += i['num'] * i['price']
        totail_num += i['num']

    data = request.POST
    print(data)
    if data.get('consignee') and data.get('address') and data.get(
            'phone') and data.get('code'):
        try:
            # 1. 添加订单数据信息
            oi = Order()  # 添加订单数据
            oi.total_price = totail
            oi.total_number = totail_num
            oi.addcode = data.get('code')  # 收获邮箱
            oi.addname = data.get('consignee')  # 收获人
            oi.addphone = data.get('consignee')  # 收获手机号
            oi.address = data.get('address')  # 收获地址
            oi.uid = Users.objects.get(
                id=request.session['login_users']['user_id'])  # 订单用户
            oi.status = 1  # 订单状态
            oi.save()  # 保存数据

            # 2, 添加订单详情
            # print(request.session['order_add'])
            for i in request.session['order_add']:
                order_info_add = OrderInfo()
                order_info_add.num = i.get('num')  # 商品数量
                order_info_add.price = i.get('price')  # 商品单价
                order_info_add.orderid = oi
                order_info_add.gid = Goods.objects.get(id=i.get('id'))
                order_info_add.save()
                del request.session['cart'][str(i.get('id'))]
            if request.session.get('order_add'):
                del request.session['order_add']

            return HttpResponse(
                '<script>alert("下单成功, 请支付");location.href="/buy/%s"</script>' %
                oi.id)
        except:
            return HttpResponse(
                '<script>alert("订单信息错误, 请重新添加");location.href="/orderadd/"</script>'
            )

    return HttpResponse(
        '<script>alert("没有填写收获信息,请重新填写");location.href="/orderadd/"</script>')