Beispiel #1
0
def search():
    """
    Request Response Http请求头 POST内容
    """
    # 注意, request是通过http请求触发的才会有正确的值;
    form = SearchForm(request.args)
    books = BookCollection()

    if form.validate():
        q = form.q.data.strip()  # 去掉前后空格
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)

        yushu_book = YushuBook()

        if isbn_or_key == 'isbn':
            yushu_book.serch_by_isbn(q)
        else:
            yushu_book.serch_by_keyword(q, page)

        books.fill(yushu_book, q)
        return json.dumps(books, default=lambda obj: obj.__dict__)
    else:
        flash('搜索关键字格式不合要求,请重新输入')
        # return jsonify(form.errors)

    return render_template('search_result.html', books=books)
Beispiel #2
0
def book_detail(isbn):
    yushu_book = YushuBook()
    yushu_book.serch_by_isbn(isbn)
    book = BookViewModel(yushu_book.first)

    has_in_gifts = False
    has_in_wishes = False

    if current_user.is_authenticated:
        # 如果未登录,current_user将是一个匿名用户对象
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = True
        if Wish.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=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_wishes_model = TradeInfo(trade_wishes)
    trade_gifts_model = TradeInfo(trade_gifts)

    return render_template('book_detail.html', book=book, has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes,
                           wishes=trade_wishes_model,
                           gifts=trade_gifts_model)
Beispiel #3
0
    def can_saveto_list(self, isbn):
        if is_isbn_or_key(isbn) != 'isbn':
            return False
        yushu_book = YushuBook()
        yushu_book.serch_by_isbn(isbn)
        if not yushu_book.first:
            return False

        # 不允许同一用户同时赠送多本相同图书
        gifting = Gift.query.filter_by(uid=self.id, isbn=isbn,
                                       launched=False).first()

        # 一个用户不能同时成为赠送者和索要者
        wishing = Wish.query.filter_by(uid=self.id, isbn=isbn,
                                       launched=False).first()

        # 既不在赠送清单,又不在心愿清单才能添加
        if not gifting and not wishing:
            return True
        else:
            return False
Beispiel #4
0
 def book(self):
     yushu_book = YushuBook()
     yushu_book.serch_by_isbn(self.isbn)
     return yushu_book.first