def book_detail(isbn): has_in_gifts = False has_in_wishes = False # 曲书记的详情数据 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_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, wishes=trade_wishes_model, gifts=trade_gifts_model, has_in_wishes=has_in_wishes, has_in_gifts=has_in_gifts)
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(): """ q: keyword or isbn page """ 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) else: return jsonify(form.errors)
def search(q, page): """ q:用户传递的参数 page:用户传递的页面 """ # 判断用户传入的参数是否为isbn isbn_or_key = is_isbn_key(q) if isbn_or_key == 'isbn': result = YuShuBook.search_by_isbn(q)
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) 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) result = BookViewModel.pacage_single(result, q) else: result = YuShuBook.search_by_keyword(q, page) result = BookViewModel.pacage_collection(result, q) return jsonify(result) else: return jsonify(form.errors)
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(isbn=isbn, uid=self.id, launched=False).first() wishing = Wish.query.filter_by(isbn=isbn, uid=self.id, launched=False).first() if not gifting and not wishing: return True else: return False
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 book_detail(isbn): has_in_gifts = False has_in_wishes = False #取书籍详情数据展示 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_book.first) #取赠送者信息和索要者信息 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 book = BookViewModel(yushu_book.first) # if has_in_gifts: # 取赠送者信息和索要者信息 trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False).all() trade_gifts = Gift.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) #return render_template("book_detail.html",book=book,wishes=[],gifts=[]) # @web.route('/test') # def test(): # r={'age': 18, # 'name': 'nnnn' # # } # flash('hello my') # return render_template('test.html',data = r)
def search(): """ :param q: 普通关键字 isbn :param page: start count 缩减而来 :return: """ form = SearchForm(request.args) if form.validate(): q = form.q.data.strip() page = form.page.data key_or_isbn = is_key_or_isbn(q) if key_or_isbn == 'isbn': result = YuShuBook.search_by_isbn(q) else: result = YuShuBook.search_by_keyword(q, page) return jsonify(result) else: return jsonify(form.errors)
def search(): ''' q:普通关键字 isbn page: ''' # q至少有一个字符,长度限制 form = SearchForm(request.args) if form.validate(): q = str(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) else: return jsonify(form.errors)
def search(): form = SearchForm(request.args) yushu_book = YuShuBook() print(form) 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': res = yushu_book.search_by_isbn(q) else: res = yushu_book.search_by_keyword(q, page) else: res = form.errors print("搜索关键字不符合要求") return res
def can_save_to_list(self,isbn): if is_isbn_or_key(isbn) != 'isbn': # 判断是不是isbn return False yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) if not yushu_book.first: # 判断 isbn 是否存在 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 wishing and not gifting
def search(): # isbn_url = 'https://api.douban.com/v2/book/isbn/{}' # keyword_url = 'https://api.douban.com/v2/book/search?q={}&count={}&start={}' # isbn_url = 'http://t.yushu.im/v2/book/isbn/{}' # keyword_url = 'http://t.yushu.im/v2/book/search?q={}&count={}&start={}' 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': res = YuShuBook.search_by_isbn(q) else: res = YuShuBook.search_by_keyword(q) return jsonify(res) else: return jsonify(form.errors)
def can_save_to_list(self, isbn): if is_isbn_or_key(isbn) != 'isbn': #判断isbn格式是否正确 return False yushu_book = YuShuBook() yushu_book.search_by_isbn(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_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): # 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 can_save_to_db(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 can_save_to_list(self, isbn): if is_isbn_or_key(isbn) != 'isbn': #查看isbn号是否符合规范 return False yushu_book = YuShuBook() yushu_book.search_by_isbn(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_save_to_list(self, isbn): if is_isbn_or_key(isbn) != '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) result = {} 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': data = YuShuBook.search_by_isbn(q) result = BookViewModel.package_single(data, q) else: data = YuShuBook.search_by_keyword(q, page) result = BookViewModel.package_collection(data, q) # return jsonify(result) else: # return jsonify(form.errors) flash('搜索的关键字不符合要求,请重新输入关键字') return render_template('search_result.html', books=result)
def search(): """ q:普通关键字 or isbn(一组数字)--如何鉴别 page:strat count ?q=金庸&page=1 :return: """ form = SearchFrom(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) else: return jsonify(form.errors)
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 save_to_gifts(isbn): yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) # gifting = Gift.query.filter_by(uid=current_user.id, isbn=isbn, status=1, # launched=False).first() # wishing = Wish.query.filter_by(uid=current_user.id, isbn=isbn, status=1, # launched=False).first() if current_user.can_save_to_list(isbn): # 既不在赠送清单,也不在心愿清单才能添加 with db.auto_commit(): gift = Gift() gift.uid = current_user.id gift.isbn = isbn # gift.book_id = yushu_book.data.id db.session.add(gift) current_user.beans += current_app.config['BEANS_UPLOAD_ONE_BOOK'] else: flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加') return redirect(url_for('web.book_detail', isbn=isbn))
def can_save_to_list(self, isbn): #isbn是否合规 if is_isbn_or_key(isbn) != 'isbn': return False #是否能找到isbn这本书 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 book_detail(isbn): # 去书籍详情数据 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_book.first) has_in_wishes, has_in_gifts = __confirm_book_state(isbn) trade_gifts = TradeInfo( Gift.query.filter_by(isbn=isbn, launched=False).all()) trade_wishes = TradeInfo( Wish.query.filter_by(isbn=isbn, launched=False).all()) return render_template('book_detail.html', book=book, wishes=trade_wishes, gifts=trade_gifts, has_in_wishes=has_in_wishes, has_in_gifts=has_in_gifts)
def book_detail(isbn): # 如何拿到这本书的字典?现在有isbn,到api上找到这本书,返回一个 """ 1右上角显示,根据current_user来判断 2如果这个用户的wish,gift都没有,中间是显示 '赠送此书,加入心愿单',如果用户的wish有,那么只显示"已经加入心愿清单",如果用户的gift有,那么只显示"只赠送".判断这本书是不是在wish里, 3显示几个人想要,拿到这本书的wish然后可以查到多少人,这是多对多关系的一个功能 4是显示向他们请求此书,还是向他们赠送此书?根据一个标志来确定,这个跟current_user有关,如果wish中有它,没有wish也没gifts,那么显示"向他们请求",否则"向他们赠送"。向他们请求,显示这本书的所有gift的用户信息。 模板中的对象:wishes.total,wishes.trades[gift{user_name,time,id}],has_in_wishes :param isbn: :return: """ has_in_gifts = False has_in_wishes = False # 取书籍详情数据 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_book.first) # MVC MVT 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() # 如何把查询到的这本书的所有wish对象列表,转换成模板需要的? wishes = Wishesinfo(trade_wishes) return render_template('book_detail.html', book=book, wishes=wishes, gifts=[], wish=[], has_in_wishes=has_in_wishes, has_in_gifts=has_in_gifts)
def book_detail(isbn): has_in_gifts = False has_in_wishes = False # 获取原始数据 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_book.first_element) if current_user.is_authenticated: if Gift.query.filter_by(isbn=isbn, launched=False, uid=current_user.id).first(): has_in_gifts = True if Wish.query.filter_by(isbn=isbn, launched=False, uid=current_user.id).first(): has_in_wishes = True trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False, uid=current_user.id).all() trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False, uid=current_user.id).all() trade_gifts_model = Trade(trade_gifts) trade_wishes_model = Trade(trade_wishes) return render_template('book_detail.html', book=book, gifts=trade_gifts_model, wishes=trade_wishes_model , has_in_gifts=has_in_gifts, has_in_wishes=has_in_wishes)
def book_detail(isbn): # 取出每本书的详情 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) book = BookViewModel(yushu_book.first) # 三种情况的判断 has_in_gifts = current_user.is_authenticated and current_user.has_in_gifts(isbn) has_in_wishes = current_user.is_authenticated and current_user.has_in_wishs(isbn) # 赠书人列表和索要人列表 trade_gifts = Gift.query.filter_by(isbn=isbn).all() trade_wishs = Wish.query.filter_by(isbn=isbn).all() trade_wishs_model = TradeInfo(trade_wishs) trade_gifts_model = TradeInfo(trade_gifts) return render_template("book_detail.html", book=book, wishes=trade_wishs_model, gifts=trade_gifts_model, has_in_wishes=has_in_wishes, has_in_gifts=has_in_gifts)
def can_save_to_list(self, isbn): # 检查isbn是否符合规范 if is_isbn_or_key(isbn) != 'isbn': return False yushu_book = YuShuBook() # 查询API中是否存在这个isbn 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(): """ q :普通关键字 (keyword) isbn page """ # 第一种方式 验证方法q,page # q = request.args['q'] # page = request.args['page'] form = SearchForm(request.args) if form.validate(): q = form.q.data.strip() # strip()除去q前后存在空格 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) else: return jsonify(form.errors)
def detail(isbn): has_in_gifts = False has_in_wishs = False if current_user.is_authenticated: if Gift.query.filter_by(isbn=isbn, uid=current_user.id, launched=False).first(): has_in_gifts = True if Wish.query.filter_by(isbn=isbn, uid=current_user.id, launched=False).first(): has_in_wishs = True yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) bookdetail = BookViewModel(yushu_book.firstbook) trade_gift = Gift.query.filter_by(isbn=isbn, launched=False).all() trade_wish = Wish.query.filter_by(isbn=isbn, launched=False).all() trade_wish_gift = TradeInfo(trade_gift) trade_wish_wish = TradeInfo(trade_wish) return render_template("book_detail.html", book=bookdetail, gifts=trade_wish_gift, wishs=trade_wish_wish, has_in_gifts=has_in_gifts, has_in_wishs=has_in_wishs)
def search(): """ :param q: 关键字还是isbn :param page: 分页相关 :return: """ # Request:HTTP 请求相关信息,查询参数,POST参数,remote_ip # Response: # q ,page = request.args['q'],request.args['page'] # q和page合法校验。q:至少有一个字符,长度限制,page :正整数,一个最大值 # 验证层,参数校验 form = SearchForm(request.args) books = BookCollection() if form.validate(): q = form.q.data.strip() page = form.page.data # 没有传递page,会获取到默认值1 isbn_or_key = is_isbn_or_key(q) yushu_book = YuShuBook() if isbn_or_key == 'isbn': # 第一次的亲密接触api:http://127.0.0.1:81/book/search/9787501524044/1 # 第一次的亲密接触api:http://127.0.0.1:81/book/search?q=9787501524044&page=1 # 如果没有导入,快捷导入:alt+enter yushu_book.search_by_isbn(q) # ret = YuShuBook.search_by_isbn(q) # result = BookViewModel.package_single(ret,q) else: yushu_book.search_by_keyword(q, page) # ret = YuShuBook.search_by_keyword(q,page) # result = BookViewModel.package_collection(ret,q) books.fill(yushu_book, q) # 主要是这种返回的思路,带上响应头 # return json.dumps(result,ensure_ascii=False),200,{'content-type':'application/json'}) # API:数据用json格式返回给客户端 return json.dumps(books, default=lambda o: o.__dict__) # 把不能序列化的类,转换为可以序列化的 # return jsonify(books) return jsonify(form.errors)
def can_save_to_list(self, isbn): """ 判断可以将书籍加入心愿清单 1.如果isbn编号不符合规则,不允许添加 2.如果isbn编号对应的书籍不存在,不允许添加 3.同一个用户,不能同时赠送同一本书籍 4.一个用户对于一本书不能既是赠书者,又是索要者 5.3和4合并成一条,就是一本书必须即不在心愿清单又不在赠书列表里才可以添加 :param isbn: :return: """ if not is_isbn_or_key(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 wishing and not gifting
def search(): """ 搜索书籍路由 """ form = SearchForm(request.args) books = BookCollection() if form.validate(): q = form.q.data.strip() isbn_or_key = is_isbn_or_key(q) yushu_book = YuShuBook() if isbn_or_key == 'isbn': yushu_book.search_by_isbn(q) else: page = form.page.data yushu_book.search_by_key(q, page) books.fill(yushu_book, q) else: flash("搜索的关键字不符合要求,请重新输入关键字") return render_template("search_result.html", books=books)
def book(self): yushu_book = YuShuBook() yushu_book.search_by_isbn(self.isbn) return yushu_book.first