Ejemplo n.º 1
0
def search():  # controller 也叫视图函数,本质就是函数,用于控制mvc view视图
    """
        q :普通关键字/isbn查询 用代码区分
        page
        ?q=name&page=num
    """
    # 验证层
    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)
        # 不可变转可变 test3.py = request.args.to_dict()  args是一个immutable dict
        yushu_book = YushuBook()

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

        books.fill(yushu_book, q)
        # return json.dumps(books, default=lambda o: o.__dict__)
    else:
        flash('搜索的关键字不符合要求,请重新输入关键字')
        # return jsonify(form.errors)
        # return json.dumps(result), 200, {'content-type': 'application/json'}
    # 确保总能返回结果
    return render_template('search_result.html', books=books)
Ejemplo n.º 2
0
def search():
    form = SearchForm(request.args)
    books = BookCollection()
    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        is_key = is_isbn_or_key(q)

        yushu_book = YushuBook()
        if is_key == 'isbn':
            yushu_book.search_by_isbn(q)

        else:
            yushu_book.search_by_keyword(q)
        books.fill(yushu_book, q)
    else:
        flash("您输入的关键词有误,请重新输入")
    return render_template('search_result.html', books=books)
Ejemplo n.º 3
0
def search():
    """
        q: 普通关键字或isbn
        page:
        ?q=金庸&page=1
        isbn isbn13 isbn13个0到9的数字组成
        isbn10 10个0到9的数字组成,含有一些' - '
    """
    # Request Response
    # HTTP的请求信息
    # 查询参数 POST参数 remote ip

    # isbn isbn13 isbn13个0到9的数字组成
    # isbn10 10个0到9的数字组成,含有一些' - '

    # q = request.args['q']
    # page = request.args['page']

    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.search_by_isbn(q)
        else:
            yushu_book.search_by_keyword(q, page)

        books.fill(yushu_book, q)

    else:
        flash("搜索的关键字不符合要求,请重新输入关键字")
        # return jsonify(form.errors)
    return render_template('search_result.html', books=books)
Ejemplo n.º 4
0
def search():
    form = SearcgForm(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 = YushuBook()

        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__)
        # print('books', books)
        # return json.dumps(books, default=lambda o:o.__dict__)
        return render_template('index.html', data=books)
    else:
        # return jsonify({'meg': '参数校验错误'})
        return jsonify(form.errors)
Ejemplo n.º 5
0
def search():
    '''
    q:关键字 or ISBN号
    '''
    # q = request.args['q']
    # page = request.args['page']

    form = SearchForm(request.args)
    if form.validate():
        q = form.q.data.strip() #strip函数去除字符串前后的空格
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        if isbn_or_key == 'isbn':
            result = YushuBook.search_by_isbn(q)
        else:
            result = YushuBook.search_by_keyword(q)
        # return json.dumps(result),200,{'content-type':'application/json'}
        return jsonify(result)
    else:
        return jsonify(form.errors)