def add_goods(request): id = request.POST.get("id") count = request.POST.get("count") num = request.POST.get("num", 1) is_login = request.session.get("is_login") if is_login: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) print(cart0) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=id)[0] GoodsCar.objects.create(user=user, book=book, num=int(count)) else: cart0 = cart0[0] cart0.num += int(count) * int(num) cart0.save() else: cart = request.session.get("cart") if not cart: cart = Cart() else: cart = request.session.get("cart") cart.add(id, int(count) * int(num)) request.session["cart"] = cart return HttpResponse("")
def cart(request): name = request.session.get('name') is_login = request.session.get("is_login") id = request.POST.get("id") uid = request.session.get('id') if is_login: cart00 = Cart() id1 = GoodsCar.objects.filter(user_id=uid) for i in id1: cart00.add(i.book_id, i.num) content = { 'cartlist': cart00.book_list, 'name': name, 'is_login': is_login, } return render(request, "car.html", content) else: cart = request.session.get("cart", Cart()) content = { 'cartlist': cart.book_list, 'name': name, 'is_login': is_login, } return render(request, "car.html", content)
def indent(request): request.session["backcart"] = Cart() name = request.session.get('name') is_login = request.session.get("is_login") uid = request.session.get('id') address_list = request.GET.get('address_list') print(address_list) cart = Cart() address = Addressee.objects.filter(user_id=uid) print(address.count()) address_list = Addressee.objects.filter(address=address_list) id1 = GoodsCar.objects.filter(user_id=uid) s = 0 for i in id1: cart.add(i.book_id, i.num) book = Books(i.book_id, i.num) s += book.getTolPrice request.session['s'] = s request.session['order'] = cart content = { 's': s, 'cartlist': cart.book_list, 'name': name, 'is_login': is_login, 'address': address, 'address_list': address_list, } return render(request, 'indent.html', content)
def back_to_car(request): this = request.POST.get('book_id') uid = request.session.get("id") book = GoodsCar.objects.filter(book_id=this, user_id=uid)[0] cart = request.session.get("backcart") if not cart: cart = Cart() t = cart.add(book.book_id, book.num) request.session["backcart"] = cart if t == 1: print("no") else: print("Yes") print(cart.book_list, len(cart.book_list)) # for i in cart.book_list: # print(22222222222222, i.name) print(book.book_id) return HttpResponse('1')
def indent_logic(request): name = request.POST.get('name') address = request.POST.get('address') address_id = request.POST.get("address_id") post = request.POST.get('post') phone = request.POST.get('phone') tel = request.POST.get('tel') uid = request.session.get('id') user = User.objects.filter(id=uid)[0] if name: if address: if len(post) == 6: if re.findall(r'\(?0\d{2,3}[)-]?\d{7,8}', tel) or re.findall(r'^(1\d)\d{9}$', phone): if not Addressee.objects.filter(id=address_id): cur_address = Addressee.objects.create(name=name, address=address, telephone=tel, cellphone=phone, post_code=post, user=user) else: cur_address = Addressee.objects.filter(id=address_id)[0] od = request.session.get('order') bc = request.session.get('backcart') for i in bc.book_list: for j in od.book_list: if i.id == j.id: od.removeBook(j.id) request.session['order'] = od cost_sum = 0 for book in od.book_list: cost_sum += book.getTolPrice list = List.objects.create(code=int(time.time()), date=datetime.now(), store="当当网", user=user, adressee=cur_address,price=cost_sum) request.session['orderlist']=list for i in od.book_list: cur_book = Book.objects.filter(id=i.id)[0] BookList.objects.create(book=cur_book, list=list, num=i.count) for cur_book in GoodsCar.objects.all(): cur_book.delete() backbook = request.session.get('backcart') for book in backbook.book_list: cur_book = Book.objects.filter(id=book.id)[0] GoodsCar.objects.create(user=user, book=cur_book, num=book.count) request.session["backcart"] = Cart() return HttpResponse("ok") else: return HttpResponse("wrongphone") else: return HttpResponse("wrongpost") else: return HttpResponse("wrongaddress") else: return HttpResponse("wrongname")
def add_book(request): id = request.GET.get('id') cart = request.session.get('cart') if cart is None: cart = Cart() cart.add_book_toCart(id) request.session['cart'] = cart else: cart.add_book_toCart(id) request.session['cart'] = cart #print(32, cart.cart_item[0].book.id, cart.cart_item[1].book.id, cart.cart_item[0].amount, cart.cart_item[1].amount) return HttpResponse('1')
def login_logic(request): name = request.POST.get('name') a = User.objects.filter(name=name)[0] id = a.id # 先获得用户id和name,以便之后记录用户id和name并存入session password = request.POST.get('password') d = User.objects.filter(name=name, password=password) # 登录逻辑验证用户名密码是否匹配 captcha = request.POST.get('code') code = request.session.get( 'code') # 实现验证码功能 前端输入框中的内容和get_captcha函数中存入session的内容做对比 location = request.session.get('location') # 取出login函数中的参数 实现“从哪来回哪去” autologin = request.POST.get('autologin') # 记住密码的勾选框 if d and captcha.lower() == code.lower( ): # 前端输入框中的内容和get_captcha函数中存入session的内容做对比 request.session["is_login"] = True request.session["name"] = name request.session[ "id"] = id # 记录用户id和name以及是否记住密码 并存入session 其他页面登陆状态和购物时都需要显示用户信息 if location == 'booklist': run1 = HttpResponse("ok_booklist") if autologin: run1.set_cookie("name", name) run1.set_cookie("pwd", password) cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run1 cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run1 elif location == 'bookdetails': run2 = HttpResponse("ok_bookdetails") if autologin: run2.set_cookie("name", name) run2.set_cookie("pwd", password) cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run2 cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run2 elif location == 'indent': run3 = HttpResponse("ok_indent") if autologin: run3.set_cookie("name", name) run3.set_cookie("pwd", password) cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run3 cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run3 else: run4 = HttpResponse("ok_index") if autologin: run4.set_cookie("name", name) run4.set_cookie("pwd", password) cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run4 cart = request.session.get("cart", Cart()) # 从session中读取相应的购物车对象 for a in cart.book_list: uid = request.session.get('id') cart0 = GoodsCar.objects.filter(user_id=uid, book_id=id) if not cart0: user = User.objects.filter(id=uid)[0] book = Book.objects.filter(id=a.id)[0] GoodsCar.objects.create(user=user, book=book, num=a.count) else: cart0 = cart0[0] cart0.num += a.count cart0.save() cart.book_list = [] request.session['cart'] = cart return run4 elif not d: return HttpResponse('用户名或密码错误') else: return HttpResponse('验证码错误') # 登陆失败后向前端页面Ajax传递的内容