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)
def search(): """ q :普通关键字 isbn page: 页码 """ form = SearchForm(request.args) # result = { # 'code': 0, # 'message': '正确返回数据' # } books = BookCollection() if form.validate(): q = form.q.data.strip() page = form.page.data yushu = YuShu() isbn_or_key = is_isbn_or_key(q) if isbn_or_key == 'isbn': yushu.search_by_isbn(q) else: yushu.search_by_keyword(q, page) books.fill(yushu, q) # # 先把对象转换成json字符串 再用loads方法转换成dict # data_string = json.dumps(books, default=lambda o: o.__dict__) # result['data'] = json.loads(data_string) # return jsonify(result) else: # result['code'] = 1 # result['message'] = form.errors # return jsonify(result) flash('搜索关键词不符合要求,请重新输入关键词') return render_template('search_result.html', books=books, form=form)
def can_save_to_list(self, isbn): if is_isbn_or_key(isbn) != 'isbn': return False yushu_book = YuShu() 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 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
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) yushu_book = YuShuBook() if isbn_or_key == "bin": 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: return jsonify(form.errors)