def __matching(self, gift): schema = BookSchema() count = 0 for wish_count in self.__wish_count_list: if gift.isbn == wish_count['isbn']: count = wish_count['count'] break r = { 'id': gift.id, 'book': schema.dump(gift.book).data, 'wishes_count': count } return r
def search_by_isbn(self, isbn): schema = BookSchema() book = Book.query.filter(Book.isbn == isbn).first() if book: dumps = schema.dump(book).data else: url = self.isbn_url.format(isbn) data = HTTP.get(url) if not data: self.total = 0 return dumps = schema.dump(data).data with db.auto_commit(): book = Book(**dumps) db.session.add(book) self.__fill_single(dumps)
def book_detail(isbn): has_in_gifts = False has_in_wishes = False # 取书籍详情数据 yushu_book = YuShuBook() yushu_book.search_by_isbn(isbn) uid = getattr(g, REQUEST_USER_ID, None) if uid: if Gift.query.filter_by(user_id=uid, isbn=isbn, launched=False, is_deleted=False).first(): has_in_gifts = True if Wish.query.filter_by(user_id=uid, isbn=isbn, launched=False, is_deleted=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_gifts_model = TradeInfo(trade_gifts) trade_wishes_model = TradeInfo(trade_wishes) book_schema = BookSchema() book_data = book_schema.dump(yushu_book.first).data # type: dict has = {'has_in_gifts': has_in_gifts, 'has_in_wishes': has_in_wishes} trade = TradeModelSchema() gifts = {'gifts': trade.dump(trade_gifts_model).data} wishes = {'wishes': trade.dump(trade_wishes_model).data} book_data.update(**has, **wishes, **gifts) schema = BookDetailViewSchema() data = schema.dump(book_data).data return SuccessResponse(data=data)()
def save_data(self, data: dict, many: bool = False): if not many: schema = BookSchema(strict=True) book = schema.load(data).data with db.auto_commit(): db.session.add(book) else: schema = BookSchema(strict=True, many=True) books = schema.load(data['books']).data isbns = [book.isbn for book in books] distinct_books = self.distinct_books(isbns, books) with db.auto_commit(): db.session.add_all(distinct_books) redis.set(self.format_key, data)
def search_by_keyword(self, keyword, start, count=10): schema = BookSchema(many=True) url = self.keyword_url.format(keyword, count, YuShuBook.cal_start(start)) data = HTTP.get(url) dumps = schema.dump(data['books']).data self.__fill_collection(data['total'], dumps)
def index(): recent_gifts = Gift.recent() schema = BookSchema(only=('title', 'summary', 'author', 'isbn', 'image')) recent = [schema.dump(gift.book).data for gift in recent_gifts] return SuccessResponse(data=recent)()