コード例 #1
0
ファイル: book.py プロジェクト: HeQingQiang/Ying
def book_detail(isbn):
    has_in_gifts = False
    has_in_wishes = False

    # 取书籍详情数据
    book = Book()
    book.search_by_isbn(isbn)
    book_result = BookViewModel(book.first)

    if current_user.is_authenticated:
        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_result,
                           wishes=[trade_wishes_model],
                           gifts=[trade_gifts_model],
                           has_in_wishes=has_in_wishes,
                           has_in_gifts=has_in_gifts)
コード例 #2
0
ファイル: book.py プロジェクト: shepherd-dc/flask-advanced
def book_detail(isbn):
    has_in_gifts = False
    has_in_wishes = False

    # 获取书籍详情数据
    book = Book()
    book.search_by_isbn(isbn)
    book_details = BookViewModel(book.first)
    # return jsonify(book_detail.__dict__)
    # return json.dumps(book_detail, default=lambda o: o.__dict__)

    if current_user.is_authenticated:
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = True
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = 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)

    return render_template('book_detail.html',
                           book=book_details,
                           gifts=trade_gifts_model,
                           wishes=trade_wishes_model,
                           has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes)
コード例 #3
0
def book_detail(isbn):
    has_in_gifts = False
    has_in_wishes = False

    # 书籍详情
    yushu_book = Book()
    yushu_book.search_by_isbn(isbn)
    book = BookViewModel(yushu_book.first)

    if current_user.is_authenticated:
        if Gift.query.filter_by(isbn=isbn, uid=current_user.id,
                                launched=False).first():
            has_in_gifts = True
        elif Wish.query.filter_by(isbn=isbn,
                                  uid=current_user.id,
                                  launched=False).first():
            has_in_wishes = True

    # wishes
    trade_wished = Wish.query.filter_by(isbn=isbn, launched=False).all()
    trade_wished_model = TradeInfo(trade_wished)
    # gifts
    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_gifts_model = TradeInfo(trade_gifts)

    return render_template('book_detail.html',
                           book=book,
                           wishes=trade_wished_model,
                           gifts=trade_gifts_model,
                           has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes)
コード例 #4
0
ファイル: user.py プロジェクト: shepherd-dc/flask-advanced
    def can_save_to_list(self, isbn):
        if is_isbn_or_key(isbn) != 'isbn':
            return False
        book = Book()
        book.search_by_isbn(isbn)
        if not 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
コード例 #5
0
 def can_save_to_list(self, isbn):
     """
     1、isbn不符合规则
     2、网站搜索不到该isbn的书
     3、用户赠送列表中已经存在
     4、用户索要列表中已经存在
     """
     if is_isbn_or_key(isbn) != 'isbn':
         return False
     yushu_book = Book()
     yushu_book.search_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
コード例 #6
0
ファイル: book.py プロジェクト: HeQingQiang/Ying
def search():
    """
    :param q: 普通关键字 isbn
    :param page: 分页参数
    :return:
    """
    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)
        book = Book()

        if isbn_or_key == 'isbn':
            book.search_by_isbn(q)
        else:
            book.search_by_keyword(q, page)

        # 返回序列化结果
        books.fill(book, q)
        # return jsonify(books.__dict__)
        # return json.dumps(books, default=lambda o: o.__dict__)
    else:
        # return jsonify(form.errors)
        flash('搜索的关键字不符合要求,请重新输入关键字。')
    return render_template('search_result.html', books=books, form=form)
コード例 #7
0
ファイル: book.py プロジェクト: shepherd-dc/flask-advanced
def search():
    """
    /book/search?q=金庸&page=1
    """
    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)

        book_data = Book()
        if isbn_or_key == 'isbn':
            book_data.search_by_isbn(q)
        else:
            book_data.search_by_keyword(q, page)

        books.fill(book_data, q)
        # return json.dumps(books, default=lambda o: o.__dict__)
    else:
        flash('搜索关键字有误,请重新输入')
        # return jsonify(form.errors)

    return render_template('search_result.html', books=books)
コード例 #8
0
def hell():
    '''
    isbn
    http://127.0.0.1:5001/book/search?q=9787501524044&page=1
    http://127.0.0.1:5001/book/search?q=%E8%81%8A%E6%96%8B&page=1
    :param name:
    :return:
    '''
    search_form = SearchForm(request.args)
    check_form = search_form.validate()
    if not check_form:
        return jsonify(search_form.errors)
    q = search_form.q.data
    result = Book.search_by_keyword(q, search_form.page.data)
    return jsonify(result)
コード例 #9
0
def search():
    """
        q : 普通关键字 ISBN关键字
        page : 页码
        /search?q=&page=
    """
    # flask的request代理模式
    # flask视图函数中的request才是request对象,否则可能成为本地代理格式
    # q = request.args['q']
    # page = request.args['page']
    # to_dict()不可变字典转变成为可变字典
    # a = request.args.to_dict()

    # 验证层:参数校验
    form = SearchForm(request.args)
    books = BooksViewModel()

    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        yushu_book = Book()
        if isbn_or_key == 'isbn':
            yushu_book.search_by_isbn(q)
            # res = Book.search_by_isbn(q)
            # res = BookViewModel.package_single(res, q)
        else:
            yushu_book.search_by_keyword(q, page)
            # res = Book.search_by_keyword(q, page)
            # res = BookViewModel.package_collection(res, q)
        books.fill(yushu_book, q)

        # 代码解释权反转:default调用内置函数使json序列化
        # 类似的:dumps sorted filter
        # return json.dumps(books, default=lambda obj: obj.__dict__, ensure_ascii=False)

        # jsonify代替response封装
        # return jsonify(books)

    else:
        # return jsonify(form.errors)
        flash('搜索的关键字不符合要求,请重新输入关键字')

    return render_template('search_result.html', books=books)
コード例 #10
0
ファイル: gift.py プロジェクト: shepherd-dc/flask-advanced
 def book(self):
     book = Book()
     book.search_by_isbn(self.isbn)
     return book.first
コード例 #11
0
ファイル: gift.py プロジェクト: mattwuu/fisher
 def book(self):
     book_model = Book()
     book_model.search_by_isbn(self.isbn)
     return book_model.first