Beispiel #1
0
def has_reserved_book(user, book_id):
    book = get_book_by_book_id(book_id)
    books = get_book_by_user_trove(user)
    if book in books:
        return True
    else:
        return False
Beispiel #2
0
def look_book_detail_page(request, book_id):
    user = auth.get_user(request)
    book = get_book_by_book_id(book_id)
    if book is None:
        return render(request, 'error.html', {'user': user, 'error_message': '暂无此书籍'})
    else:
        book.price = float('%.2f' % (float(book.discount) * book.origin_price))
        has_reserved = has_reserved_book(user, book.book_id)
        return render(request, 'user/look_book_detail_page.html',
                      {'user': user, 'book': book, 'has_reserved': has_reserved})
Beispiel #3
0
def sold_out_or_putaway(request, book_id):
    redirect_to = request.GET.get('redirect_to')
    if redirect_to is None or redirect_to == '':
        redirect_to = reverse('manager:home')
    book = get_book_by_book_id(book_id)
    if book.is_on_sale:
        book.is_on_sale = False  # 下架
    else:
        book.is_on_sale = True  # 上架
    book.save()
    return redirect(redirect_to)
Beispiel #4
0
def trove_or_cancel_trove_book(request, book_id):
    user = auth.get_user(request)
    redirect_to = request.GET.get('redirect_to')
    if redirect_to is None or redirect_to == '':
        redirect_to = reverse('user:home')
    book = get_book_by_book_id(book_id)
    if book is not None:
        if has_reserved_book(user, book_id):  # 取消收藏
            user.profile.trove_books.remove(book)
        else:  # 收藏
            user.profile.trove_books.add(book)
    return redirect(redirect_to)
Beispiel #5
0
def look_book_detail_page(request, book_id):
    user = auth.get_user(request)
    book = get_book_by_book_id(book_id)
    if book is None:
        return render(request, 'error.html', {
            'user': user,
            'error_message': '暂无此书籍'
        })
    else:
        return render(request, 'manager/look_book_detail_page.html', {
            'user': user,
            'book': book
        })
Beispiel #6
0
def modify_book_page(request, book_id):
    user = auth.get_user(request)
    if request.method == 'POST':
        modify_book_form = ModifyBookForm(request.POST, request.FILES)
        if modify_book_form.is_valid():
            book_name = modify_book_form.cleaned_data.get('name')
            publisher = modify_book_form.cleaned_data.get('publisher')
            author = modify_book_form.cleaned_data.get('author')
            category = modify_book_form.cleaned_data.get('category')
            origin_price = modify_book_form.cleaned_data.get('origin_price')
            discount = modify_book_form.cleaned_data.get('discount')
            stock = modify_book_form.cleaned_data.get('stock')
            cover = modify_book_form.cleaned_data.get('cover')
            catalogue = modify_book_form.cleaned_data.get('catalogue')
            summary = modify_book_form.cleaned_data.get('summary')

            book_dic = {
                'name': book_name,
                'publisher': publisher,
                'author': author,
                'category': category,
                'origin_price': origin_price,
                'discount': discount,
                'stock': stock
            }
            book_detail_dic = {
                'cover': cover,
                'catalogue': catalogue,
                'summary': summary
            }

            result = modify_book(book_id, book_dic, book_detail_dic)
            if result:
                return render(request, 'manager/modify_book_successfully.html',
                              {
                                  'user': user,
                                  'book_id': book_id
                              })
            else:
                return render(request, 'error.html', {
                    'user': user,
                    'error_message': '出错啦'
                })
        else:  # 表单出错
            return render(request, 'manager/modify_book_page.html', {
                'user': user,
                'modify_book_form': modify_book_form
            })
    else:  # 当正常访问时
        book = get_book_by_book_id(book_id)
        if book is None:
            return render(request, 'error.html', {
                'user': user,
                'error_message': '出错啦'
            })
        else:
            modify_book_form = ModifyBookForm({
                'book_id':
                book.book_id,
                'name':
                book.name,
                'publisher':
                book.publisher,
                'author':
                book.author,
                'category':
                book.category,
                'origin_price':
                book.origin_price,
                'discount':
                book.discount,
                'stock':
                book.stock,
                'cover':
                book.bookdetail.cover,
                'catalogue':
                book.bookdetail.catalogue,
                'summary':
                book.bookdetail.summary
            })
            return render(request, 'manager/modify_book_page.html', {
                'user': user,
                'modify_book_form': modify_book_form
            })
Beispiel #7
0
def drop_book_from_shopping_cart(request, book_id):
    user = auth.get_user(request)
    book = get_book_by_book_id(book_id)
    if book is not None and book in get_book_by_user_shopping_cart(user):
        user.profile.shopping_cart.remove(book)
    return redirect(reverse('user:look_shopping_cart_page'))
Beispiel #8
0
def add_book_to_shopping_cart(request, book_id):
    user = auth.get_user(request)
    book = get_book_by_book_id(book_id)
    if book is not None and book not in get_book_by_user_shopping_cart(user):
        user.profile.shopping_cart.add(book)
    return redirect(reverse('user:look_book_detail_page', args=[book_id]))