def registration_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) form = RegistrationForm(request.POST or None) categories = Category.objects.all() if form.is_valid(): new_user = form.save(commit=False) first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['password'] email = form.cleaned_data['email'] username = form.cleaned_data['username'] password = form.cleaned_data['password'] new_user.first_name = first_name new_user.last_name = last_name new_user.email = email new_user.username = username new_user.set_password(password) new_user.save() aunth_user = authenticate(username=username, password=password) if aunth_user: login(request, aunth_user) return HttpResponseRedirect(reverse('main_page')) context = {'form': form, 'categories': categories} return render(request, 'registration.html', context)
def login_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) form = LoginForm(request.POST or None) #categories = Category.objects.all() if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] # raise ValueError('A very specific bad thing happened.') aunth_user = authenticate(username=username, password=password) if aunth_user: login(request, aunth_user) return HttpResponseRedirect(reverse('main_page')) context = { 'form': form, # 'categories': categories } return render(request, 'login.html', context)
def make_order_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) form = OrderForm(request.POST or None) categories = Category.objects.all() if form.is_valid(): name = form.cleaned_data['name'] last_name = form.cleaned_data['last_name'] phone = form.cleaned_data['phone'] buying_type = form.cleaned_data['buying_type'] address = form.cleaned_data['address'] comments = form.cleaned_data['comments'] new_order = Order.objects.create(user=request.user, items=cart, total=cart.total_cart, first_name=name, last_name=last_name, phone=phone, address=address, buying_type=buying_type, comments=comments) del request.session['cart_id'] del request.session['total'] return HttpResponseRedirect(reverse('thank_you')) return render(request, 'order.html', {'categories': categories})
def cart_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) categories = Category.objects.all() context = {'cart': cart, 'categories': categories} return render(request, 'cart1.html', context)
def order_create_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) categories = Category.objects.all() form = OrderForm(request.POST or None) context = {'form': form, 'cart': cart, 'categories': categories} return render(request, 'order.html', context)
def addcart(request): shoes_id = request.GET.get('shoes_id') num = int(request.GET.get('num')) # 前端传过来的提交数据 print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') print(num) token = request.COOKIES.get('token') resopnse_data = { 'message': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登录 } if token: shoes = Sport_f1_shoes.objects.get(pk=int(shoes_id)) user = User.objects.get(token=token) carts = Cart.objects.filter(user=user).filter(shoes=shoes) if carts.exists(): # 修改数量 cart = carts.first() cart.num += num cart.save() resopnse_data['num'] = cart.num else: # 添加一条新记录 cart = Cart() cart.user = user cart.shoes = shoes # cart.num = 1 cart.num = num cart.save() resopnse_data['num'] = cart.num return JsonResponse(resopnse_data) else: resopnse_data['message'] = '未登录,请登录后操作' resopnse_data['status'] = 0 return JsonResponse(resopnse_data)
def product_view(request, product_slug): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) product = Product.objects.get(slug=product_slug) #categories = Category.objects.all() context = { #'categories': categories, 'product': product, 'cart': cart } return render(request, 'product1.html', context)
def category_view(request, category_slug): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) category = Category.objects.get(slug=category_slug) products_in_cat = Product.objects.filter(category=category) # categories = Category.objects.all() context = { 'category': category, 'products_in_cat': products_in_cat #'categories': categories } return render(request, 'category1.html', context)
def cart_order_complete(request, total_price, username, phonenumber, address, uid, delivery_message): delivery_lis = [] cart_ref = db.collection("users").document(uid).collection('cart') cart_alldoc = cart_ref.stream() for doc in cart_alldoc: cart = Cart.from_dict(doc.to_dict()) doc_ref = db.collection("delivery") doc_ref_random = db.collection("delivery").document() doc_ref_random.set( Delivery(cart.brand, cart.name, cart.sizedic, total_price, username, phonenumber, address, firestore.SERVER_TIMESTAMP, "배송전", "", "", uid, delivery_message, cart.downloadurl, cart.documentId).to_dict()) user_doc = db.collection("users").document(uid).collection( "delivery").document(doc_ref_random.id) user_doc.set( Delivery(cart.brand, cart.name, cart.sizedic, total_price, username, phonenumber, address, firestore.SERVER_TIMESTAMP, "배송전", "", "", uid, delivery_message, cart.downloadurl, cart.documentId).to_dict()) delievery_par = Delivery.from_dict( Delivery(cart.brand, cart.name, cart.sizedic, cart.price, username, phonenumber, address, firestore.SERVER_TIMESTAMP, "배송전", "", "", uid, delivery_message, cart.downloadurl, cart.documentId).to_dict()) # delivery=Delivery.from_dict(delivery_docs.to_dict()) delivery_lis.append(delievery_par) #print(delivery_lis) #for문끝 def delete_collection(coll_ref, batch_size): deldocs = coll_ref.limit(batch_size).stream() deleted = 0 for doc in deldocs: print(f'Deleting doc {doc.id} => {doc.to_dict()}') doc.reference.delete() deleted = deleted + 1 if deleted >= batch_size: return delete_collection(coll_ref, batch_size) delete_collection(cart_ref, 3) return render(request, 'getdata.html', {'delivery_lis': delivery_lis})
def main_page(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) categories = Category.objects.all() products = Product.objects.all() paginator = Paginator(products, 6) page = request.GET.get('page') products = paginator.get_page(page) context = { 'categories': categories, 'products': products, 'cart': cart, } return render(request, 'main_page.html', context)
def add_item_cart(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) product_slug = request.GET.get('product_slug') product = Product.objects.get(slug=product_slug) cart.add_cart_item_to_cart(product_slug) cart_total_price = 0.00 for item in cart.items.all(): cart_total_price += float(item.total_item) cart.total_cart = cart_total_price cart.save() return JsonResponse({ 'total_cart': cart.items.count(), 'cart_total_price': cart.total_cart })
def make_discount(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) coupon = request.GET.get('coupon') cart.make_discount(coupon) return JsonResponse({'cart_total_price': cart.total_cart})
def addgoods(request): goods_id = request.GET.get('goods_id') goods = Goods.objects.get(pk=goods_id) # 根据商品ID 获取到对应的商品信息 token = request.session.get('token') responese_data = { 'message': '增加成功', 'status': 1, } if token: user = User.objects.get(token=token) # 筛选得到用户集合并取出第一个用户 carts = Cart.objects.filter(user=user).filter( goods=goods) # 通过商品和用户筛选出对应购物车中的商品 if carts: cart = carts.first() cart.num += 1 cart.save() print(cart) responese_data['num'] = cart.num else: cart = Cart() cart.num = 1 cart.goods = goods cart.user = user cart.save() responese_data['num'] = cart.num return JsonResponse(responese_data) else: responese_data['message'] = '未登陆' responese_data['status'] = 0 return JsonResponse(responese_data)
def change_item_count(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) count = request.GET.get('count') item_id = request.GET.get('item_id') cart.change_count(count, item_id) cart_item = CartItem.objects.get(id=int(item_id)) return JsonResponse({ 'total_cart': cart.items.count(), 'total_item': cart_item.total_item, 'cart_total_price': cart.total_cart })
def addtocart(request): username = request.session.get('username') data = {'status': '200', 'msg': '操作成功'} if not username: data['status'] = '302' data['msg'] = '用户不存在' return JsonResponse(data) user = User.objects.filter(u_name=username).first() goodsid = request.GET.get('goodsid') goods = Goods.objects.filter(pk=goodsid).first() cart_item = Cart.objects.filter(c_belong=False).filter(c_user=user).filter( c_goods=goods).first() if not cart_item: cart_item = Cart() else: cart_item.c_num = cart_item.c_num + 1 cart_item.c_goods = goods cart_item.c_user = user cart_item.save() data['c_num'] = cart_item.c_num return JsonResponse(data)