예제 #1
0
def car(request):

    islogin = request.session.get('is_login')
    # 判断是否登录,已登录从数据库中获取购物车数据渲染页面
    if islogin:
        # 判断购物车表中是否有该用户的购物车,如果有则获取数据

        username = request.session.get('username')
        user_id = TUser.objects.get(username=username).id
        books = ShoppingCart.objects.filter(user=user_id)
        addresss = TAddress.objects.filter(user_id=user_id)
        if books:
            # 从购车表中拿出这个人的购物车的书和数量构建前端购物车car对象
            car = Car()
            for book in books:
                id = book.book_id
                count = book.book_num
                car.add_book(id, count)
            # 计算每本书的总计
            total = 0
            for i in car.book_list:
                total += float(i.price) * int(i.count)
            return render(
                request, 'car.html', {
                    'car': car.book_list,
                    'total': round(total, 2),
                    'islogin': islogin,
                    'username': username,
                    'addresss': addresss
                })
        # 如果没有购物车,则返回空购物车
        else:
            return render(request, 'car.html', {
                'islogin': islogin,
                'username': username,
                'addresss': addresss
            })
    # 未登录状态,从session中获取购物车数据
    else:
        car = request.session.get('car')
        username = request.session.get('username')
        # 如果已有购物车,返回购物车页面、购物车数据、小计、
        if car:
            json.dumps(list(car.book_list), default=mydefault)
            total = 0
            for i in car.book_list:
                total += (float(i.price) * int(i.count))
            return render(
                request, 'car.html', {
                    'car': car.book_list,
                    'total': round(total, 2),
                    'islogin': islogin,
                    'username': username
                })
        # 若没有购物车则返回空购车
        else:
            return render(request, 'car.html', {
                'islogin': islogin,
                'username': username
            })
예제 #2
0
파일: views.py 프로젝트: unyoah/dangdang
def index(request):
    user = request.session.get('is_login')
    car = request.session.get('car')
    if user:
        user = TUser.objects.get(username=user)
        tcar = list(user.tcar_set.filter(user_id=user.id))
        if tcar:
            car = Car()
            for i in tcar:
                car.add_book(i.book_id, i.count)
    count = 0
    if car:
        for i in car:
            count += i.count
    cate_1 = Category.objects.filter(level=1)
    cate_2 = Category.objects.filter(level=2)
    new_book = TBook.objects.all().order_by('pub_time')[:8]
    top_selling1 = TBook.objects.filter(
        pub_time__gte=str(datetime.datetime.now() + datetime.timedelta(
            days=-200))[:10]).order_by('-sales_volume')[:5]
    top_comments = TBook.objects.order_by('comments')
    top_selling2 = TBook.objects.filter(
        pub_time__gte=str(datetime.datetime.now() + datetime.timedelta(
            days=-1000))[:10]).order_by('-sales_volume')[:10]
    content = {
        'cate_1': cate_1,
        'cate_2': cate_2,
        'new_book': new_book,
        'top_selling1': top_selling1,
        'top_comments': top_comments,
        'top_selling2': top_selling2,
        'user': user,
        'count': count
    }
    return render(request, 'index.html', content)
예제 #3
0
def del_car(request):
    id = request.GET.get('id')
    islogin = request.session.get('is_login')
    # 判断是否是登陆状态,若已登录
    if islogin:
        username = request.session.get('username')
        user_id = TUser.objects.get(username=username).id

        # 从购物车表删除该用户的该id书的数据
        ShoppingCart.objects.filter(user=user_id, book=id)[0].delete()
        # 拿出这个用户的购物车,创建页面显示购物车对象car
        books = ShoppingCart.objects.filter(user=user_id)
        car = Car()
        for book in books:
            book_id = book.book_id
            count = book.book_num
            car.add_book(book_id, count)
        # 利用页面显示购物车对象car,计算总价total
        total = 0
        for i in car.book_list:
            total += (float(i.price) * int(i.count))
        return JsonResponse({"total": total})
    # 未登录:则从session中获取购物车>删除指定id的书
    else:
        # 从session中获取购物车
        car = request.session.get('car')
        # 删除购物车中指定书id的数据
        car.remove_book(id)
        request.session['car'] = car
        # 计算总价
        total = 0
        for i in car.book_list:
            total += (float(i.price) * int(i.count))
        return JsonResponse({"total": total})
예제 #4
0
def add_car(request, user):
    id = request.POST.get('id')
    count = request.POST.get('count1')
    car = request.session.get('car')
    if user:
        t_user = TUser.objects.filter(user_name=user)
        if t_user:
            t_user = t_user[0].id
        book = TCar.objects.filter(book_id=id, user_id=t_user)
        if book:  # 还在登录状态(且这本书添加过了)
            book[0].product_number = int(count) + int(book[0].product_number)
            book[0].save()
            return HttpResponse("登录中更改旧书")
        else:  # 一直在登录状态(添加新书)
            TCar.objects.create(book_id=id,
                                user_id=t_user,
                                product_number=count)
            return HttpResponse("登录中添加新书")
    else:
        if car:
            car.add_book(id, count)
            request.session['car'] = car
            return HttpResponse('未登录添加书')
        else:
            car = Car()
            car.add_book(id, count)
            request.session['car'] = car
            return HttpResponse("未登录添加新购物车")
예제 #5
0
파일: views.py 프로젝트: ONG008/test
def indent(request):
    username = request.session.get('username')
    user_id = TUser.objects.get(username=username).id
    books = ShoppingCart.objects.filter(user=user_id)
    addresss = TAddress.objects.filter(user_id=user_id)
    car = Car()
    for book in books:
        id = book.book_id
        count = book.book_num
        car.add_book(id, count)
    total = 0
    for i in car.book_list:
        total += float(i.price) * int(i.count)
    return render(request, 'indent.html', {'car': car.book_list, 'username': username,
                                           'total': round(total, 2), 'addresss': addresss})
예제 #6
0
def add_car(request):

    id = request.GET.get('id')
    count = int(request.GET.get('count'))
    # 判断是否登录,已登录存入数据库
    if request.session.get('is_login'):
        # 判断是已存在这本书,直接修改数量
        username = request.session.get('username')
        user_id = TUser.objects.get(username=username).id
        books = ShoppingCart.objects.filter(user=user_id, book=id)
        if books:
            books[0].book_num = books[0].book_num + count
            books[0].save()
        # 判断否则,创建一行数据
        else:
            username = request.session.get('username')
            user_id = TUser.objects.get(username=username).id
            ShoppingCart.objects.create(
                book_id=id,
                book_num=count,
                user_id=user_id,
            )
        # 购物车里点加减号所需的总价和数量
        # car = Car()
        price = TBook.objects.filter(book_id=id)[0].discount_price
        count = ShoppingCart.objects.get(book_id=id).book_num
        total = price * count
        return JsonResponse({"total": total, 'count': count})
    # 未登录状态,存入session
    else:
        # 如果有购物车
        car = request.session.get('car')
        if car:
            pass
        else:
            car = Car()
        car.add_book(id, count)
        request.session['car'] = car
        total = 0
        for i in car.book_list:
            total += float(i.price) * int(i.count)
            if i.id == id:
                count = i.count
        return JsonResponse({"total": total, 'count': count})
예제 #7
0
파일: views.py 프로젝트: unyoah/dangdang
def book_list(request):
    user = request.session.get('is_login')
    car = request.session.get('car')
    if user:
        user = TUser.objects.get(username=user)
        tcar = list(user.tcar_set.filter(user_id=user.id))
        if tcar:
            car = Car()
            for i in tcar:
                car.add_book(i.book_id, i.count)
    count = 0
    if car:
        for i in car:
            count += i.count
    cate_1 = Category.objects.filter(level=1)
    cate_2 = Category.objects.filter(level=2)
    level = request.GET.get('level', 1)
    num = request.GET.get('num', 1)
    cate_id = request.GET.get('cate_id', 1)

    cate = Category.objects.filter(id=0)

    if level == '1':
        book = TBook.objects.filter(category__parent_id=cate_id)
        cate = cate | Category.objects.filter(id=cate_id)
    else:
        book = TBook.objects.filter(category_id=cate_id)
        cc = Category.objects.filter(id=cate_id)
        cate = cate | Category.objects.filter(id=cc[0].parent_id) | cc
    paginator = Paginator(book, per_page=2)
    page = paginator.page(num)
    content = {
        'cate_1': cate_1,
        'cate_2': cate_2,
        'title': cate,
        'page': page,
        'paginator': paginator,
        'level': level,
        'num': num,
        'user': user,
        'count': count
    }
    return render(request, 'booklist.html', content)
예제 #8
0
파일: views.py 프로젝트: unyoah/dangdang
def book_detail(request):
    user = request.session.get('is_login')
    car = request.session.get('car')
    if user:
        user = TUser.objects.get(username=user)
        tcar = list(user.tcar_set.filter(user_id=user.id))
        if tcar:
            car = Car()
            for i in tcar:
                car.add_book(i.book_id, i.count)
    count = 0
    if car:
        for i in car:
            count += i.count
    book_id = request.GET.get('book_id')
    book = TBook.objects.get(id=book_id)
    cate1 = Category.objects.get(id=book.category.parent_id)
    content = {'book': book, 'cate': cate1, 'user': user, 'count': count}
    return render(request, 'Book details.html', content)
예제 #9
0
파일: views.py 프로젝트: ONG008/test
def address(request):
    consignee_name = request.POST.get('consignee_name')
    detail_address = request.POST.get('detail_address')
    post_code = request.POST.get('post_code')
    cellphone = request.POST.get('cellphone')
    telephone = request.POST.get('telephone')
    username = request.session.get('username')
    user_id = TUser.objects.filter(username=username)[0].id
    # 创建订单对象
    books = ShoppingCart.objects.filter(user=user_id)
    car = Car()
    for book in books:
        id = book.book_id
        count = book.book_num
        number = 0
        number += count
        car.add_book(id, count)
    total = 0
    for i in car.book_list:
        total += float(i.price) * float(i.count)
    # 判断收货地址表是否已有该用户的收货地址,获取
    if TAddress.objects.filter(user_id=user_id, name=consignee_name, detail_address=detail_address):
        address_id = TAddress.objects.filter(user_id=user_id, name=consignee_name, detail_address=detail_address)[0].id
        create_time = datetime.now()
        order_id = int(str(time.strftime('%m%d%H%M%S', time.localtime(time.time()))))
        TOrder.objects.create(
            order_id=order_id,
            all_price=total,
            create_time=create_time,
            address_id=address_id,
            user_id=user_id,
        )
        for book in car.book_list:
            print(order_id, type(order_id))
            OrderItem.objects.create(
                order_id=order_id,
                count=int(book.count),
                book_id=book.id,
            )
        return JsonResponse({'order_id': order_id, 'username': username, 'total': total,
                             'number': number})
    # 没有收货地址,添加收货地址
    else:
        TAddress.objects.create(
            name=consignee_name,
            detail_address=detail_address,
            post_code=post_code,
            cellphone=cellphone,
            telephone=telephone,
            user_id=user_id,
        )
        address_id = TAddress.objects.filter(user_id=user_id, name=consignee_name, detail_address=detail_address)[0].id
        create_time = datetime.now()
        order_id = int(str(time.strftime('%m%d%H%M%S', time.localtime(time.time()))))
        TOrder.objects.create(
            order_id=order_id,
            all_price=total,
            create_time=create_time,
            address_id=address_id,
            user_id=user_id,
        )
        for book in car.book_list:
            OrderItem.objects.create(
                order_id=order_id,
                count=int(book.count),
                book_id=int(book.id),
            )
        return JsonResponse({'order_id': order_id, 'username': username, 'total': total,
                             'number': number,})