def get_book(): page_number = int(request.args.get('page', 1)) per_page = int(request.args.get('per_page', 6)) schema = BookSchema(many=True) books = Book.query.paginate(page_number, per_page, False) items = schema.dump(books.items) return jsonify(items=items), 200
def get(self, id): book = db.session.query(BookModel).filter_by(id=id).first() if book is None: abort(404) res = BookSchema().dump(book).data return res
def post(self): args = self.reqparse.parse_args() book = BookModel(args.title, args.author, args.item_caption, args.image_url, args.item_url, args.image) db.session.add(book) db.session.commit() res = BookSchema().dump(book).data return res, 201
def insert_book(book_json): book = BookSchema().load(book_json) return book_service.insert_book(book)
from models.book import BookSchema book_schema = BookSchema() books_schema = BookSchema(many=True) def book_schema_dump_return(func): def a(*args, **kwargs): book = func(*args, **kwargs) if type(book) == list: return books_schema.dumps(book) return book_schema.dump(book) return a
def get(self, title): books = BookModel.query.filter_by(title=title).all() #hoge = codecs.open(books, 'w', 'utf-8') #jsonData = BookSchema(many=True).dump(books, hoge, ensure_ascii=False).data jsonData = BookSchema(many=True).dump(books).data return jsonify({'items': jsonData})
def get(self): results = BookModel.query.all() jsonData = BookSchema(many=True).dump(results).data return jsonify({'items': jsonData})
def upload_book(): if 'book' not in request.files: return jsonify({"Error": "Please choose an electronic book"}), 400 book = request.files['book'] user_id = request.form.get('user_id') title = request.form.get('title') author = request.form.get('author', ) page_number = request.form.get('page_number') if not title: return jsonify({ "Error": [{ "Type": "I/O", "message_error": "title is not inserted" }] }), 400 if not author: return jsonify({ "Error": [{ "Type": "I/O", "message_error": "author is not inserted" }] }), 400 if not page_number: return jsonify({ "Error": [{ "Type": "I/O", "message_error": "page_number is not inserted" }] }), 400 if not user_id: return jsonify({ "Error": [{ "Type": "I/O", "message_error": "user_id is not inserted" }] }), 400 user = User.query.filter_by(id=user_id).first() if not user: return jsonify({ "Error": [{ "Type": "business", "message_error": "user_id not found" }] }), 400 if book: filename = secure_filename(book.filename) url_file = current_app.config['UPLOAD_FILE'] + filename duplicate = Book.query.filter_by(book_url=url_file).first() if duplicate: return jsonify({ "Error": [{ "Type": "business", "message_error": "This book uploaded before" }] }), 400 book.save(url_file) book = Book(title=title, page_number=page_number, author=author, book_url=url_file, thumbnaiUrl=filename, user_id=user_id) db.session.add(book) db.session.commit() return jsonify(BookSchema().dump(book)), 200