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)
def search(): form = SearchForm(request.args) books = {} page = 0 total = 0 q = '' if form.validate(): q = form.q.data page = form.page.data isbn_or_key = is_isbn_or_key(q) yushu = YuShuBook() if isbn_or_key == 'key': yushu.search_by_key(q, page) if isbn_or_key == 'isbn': yushu.search_by_isbn(q) yushu_view = BookCollection(yushu) books = yushu_view.books total = yushu_view.total page = page else: flash('搜索的关键字不符合要求,请重新输入关键字') return render_template('search.html', q=q, books=books, total=total, per_page=current_app.config['PER_PAGE'], current_page=page)
def search(): # 注意验证层 form = SearchForm(request.args) book = BookCollection() if form.validate(): q = form.q.data.strip() page = form.page.data print(q, page) isbn_or_key = is_isbn_or_key(q) yushu_book = YuShuBook() if isbn_or_key == "isbn": yushu_book.isbnSearch(q) else: yushu_book.keySearch(q) book.fill(yushu_book, q) # return json.dumps(book,default=lambda obj:obj.__dict__) else: flash('关键字错误,请重新输入关键字') # return jsonify(form.errors) return render_template('search_result.html', books=book)
def search(): # isbn isbn13 13个0-9的数字组成 # q = request.args['q'] # page = request.args['page'] # 不可变字典 # a = request.args.to_dict() # 不可变字典转换为可变字典 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) # dict 序列化 # API # return json.dumps(result),200,{'content-type':'application/json'} # jsonify() 序列化字典 # return json.dumps(books, default=lambda o: o.__dict__) else: flash("搜索的关键字不符合要求,请重新输入关键字") # 消息闪现 # return jsonify(form.errors) return render_template('search_result.html', books=books, form=form)
def can_save_to_list(self, isbn): # 检查isbn格式 if is_isbn_or_key(isbn) != 'isbn': return False # 检查是否有这本书 yushu_book = YuShuBook() 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() return not gifting and not wishing
def can_save_to_list(self, isbn): if is_isbn_or_key(isbn) != 'isbn': return False yushu_book = YuShuBook() 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
def search(): 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 = YuShu() if isbn_or_key == 'isbn': yushu.search_by_isbn(q) else: yushu.search_by_keyword(q, page) books.fill(yushu, q) # return json.dumps(books, default=lambda x: x.__dict__) else: flash('搜索的关键字不符合要求,请重新输入关键字') # return jsonify({'msg': form.errors}) return render_template('search_result.html', books=books)
def can_save_to_list(self, isbn): # 1.判断isbn格式 # 2.判断isbn是否存在 # 3.一个人不能同时赠送多本书(根据launched) # 4.用户既不能又在赠送这本书状态下又索要这本书 if is_isbn_or_key(isbn) != 'isbn': return False yushubook = YuShuBook() yushubook.isbnSearch(isbn) if not yushubook.first: return False gift = Gift.query.filter_by(uid=current_user.id, isbn=isbn, launched=False).first() # 证明这个用户这个书正在赠送 wish = Wish.query.filter_by(uid=current_user.id, isbn=isbn, launched=False).first() # 证明这个用户这个书正在索要 if not gift and not wish: return True return False
def search(): # q = request.args['q'] # page = request.args['page'] form = SearchForm(request.args) books = BookCollection() if form.validate(): #从form中取数的好处,page空 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) if isbn_or_key == 'key': yushu_book.search_by_keyword(q, page) books.fill(yushu_book, q) return json.dumps(books, default=lambda o: o.__dict__), { 'content-type': 'application/json' } else: return jsonify(form.errors)
def search(): form = SearchForm(request.args) books = BookCollection() if form.validate(): q = form.q.data.strip() # 从验证通过的form中取值,保证数据对正确性 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) # return json.dumps(books, default=lambda o: o.__dict__) # 这里将对象进行json序列化。 else: # return jsonify(form.errors) flash("搜索的关键字不符合要求,请重新输入关键字") return render_template('search_result.html', books=books)
def search(): """ q: 普通关键字 isbn page: ?q=金庸&page=1 """ # q = request.args['q'] # page = request.args['page'] # a = request.args.to_dict() # 调用此方法是将不可变的字典转成常见的可变字典。 form = SearchForm(request.args) if form.validate(): q = form.q.data.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, page) return jsonify(result) # return json.dumps(result), 200, {'content-type': 'application/json'} else: return jsonify(form.errors)
def search(): ''' 搜索书籍路由 :param q: keyword or isbn :param 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 render_template('search_result.html', books=books)
def validate_isbn(self, field): is_isbn = is_isbn_or_key(field.data) if is_isbn != "isbn": raise ValidationError("isbn不合规")