Ejemplo n.º 1
0
def save_to_wish(isbn):
    uid = getattr(g, REQUEST_USER_ID)
    user = User.query.filter_by(id=uid).first()
    if is_isbn_or_key(isbn):
        return ErrorResponse(BOOK_ISBN_ERROR).make()
    yushu_book = YuShuBook()
    yushu_book.search_by_isbn(isbn)
    if not yushu_book.first:
        return ErrorResponse(BOOK_ISBN_ERROR).make()
    if user.can_save_to_list(str(isbn)):
        with db.auto_commit():
            wish = Wish()
            wish.isbn = isbn
            wish.user_id = user.id
            db.session.add(wish)
    else:
        return ErrorResponse(SAVE_WISH_ERROR).make()
    return SuccessResponse()()
Ejemplo n.º 2
0
def save_to_gifts(isbn):
    if is_isbn_or_key(isbn):
        return ErrorResponse(BOOK_ISBN_ERROR).make()
    yushu_book = YuShuBook()
    yushu_book.search_by_isbn(isbn)
    if not yushu_book.first:
        return ErrorResponse(BOOK_ISBN_ERROR).make()
    uid = getattr(g, REQUEST_USER_ID)
    user = User.query.get(uid)
    if user.can_save_to_list(isbn):
        with db.auto_commit():
            gift = Gift()
            gift.isbn = isbn
            gift.user_id = user.id
            user.beans += Decimal(0.5).quantize(Decimal('0.00'))
            db.session.add(gift)
    else:
        return ErrorResponse(SAVE_BOOK_ERROR).make()
    return SuccessResponse()()
Ejemplo n.º 3
0
def search(keyword):
    dict = request.args.to_dict()
    dict.update({'keyword': keyword})
    schema = BookSearchValSchema(strict=True).load(dict)
    data = schema.data
    keyword = data['keyword']
    page = data['page']
    per_page = data['per_page']

    yushu_book = YuShuBook()
    if is_isbn_or_key(keyword):
        yushu_book.search_by_keyword(keyword, page, per_page)
    else:
        yushu_book.search_by_isbn(keyword)
    pagination = {
        'total': yushu_book.total,
        'current_page': page,
        'per_page': per_page
    }
    return SuccessResponse(data=yushu_book.books, pagination=pagination)()
Ejemplo n.º 4
0
def book_detail(isbn):
    has_in_gifts = False
    has_in_wishes = False

    # 取书籍详情数据
    yushu_book = YuShuBook()
    yushu_book.search_by_isbn(isbn)

    uid = getattr(g, REQUEST_USER_ID, None)
    if uid:
        if Gift.query.filter_by(user_id=uid,
                                isbn=isbn,
                                launched=False,
                                is_deleted=False).first():
            has_in_gifts = True
        if Wish.query.filter_by(user_id=uid,
                                isbn=isbn,
                                launched=False,
                                is_deleted=False).first():
            has_in_wishes = True

    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False).all()

    trade_gifts_model = TradeInfo(trade_gifts)
    trade_wishes_model = TradeInfo(trade_wishes)

    book_schema = BookSchema()
    book_data = book_schema.dump(yushu_book.first).data  # type: dict
    has = {'has_in_gifts': has_in_gifts, 'has_in_wishes': has_in_wishes}
    trade = TradeModelSchema()
    gifts = {'gifts': trade.dump(trade_gifts_model).data}
    wishes = {'wishes': trade.dump(trade_wishes_model).data}
    book_data.update(**has, **wishes, **gifts)
    schema = BookDetailViewSchema()
    data = schema.dump(book_data).data
    return SuccessResponse(data=data)()
Ejemplo n.º 5
0
def search():
    '''
    :parameter:
        keyword 普通关键字 or isbn
        page分页
        1. ?传参 ?keyword=金庸&page=1
    :return: response
    '''
    form = SearchForm(request.args)
    if form.validate():
        keyword = form.keyword.data.strip()
        page = form.page.data
        if is_isbn(keyword):
            result = YuShuBook.search_by_isbn(keyword)
        else:
            result = YuShuBook.search_by_keyword(keyword, page)
        return jsonify(result)
    else:
        return jsonify(form.errors)
Ejemplo n.º 6
0
 def book(self):
     yushu_boook = YuShuBook()
     yushu_boook.search_by_isbn(self.isbn)
     return yushu_boook.first