def add_new(): # 使用form重写添加书籍函数,使用form可以直接验证数据字段是否提交 form = BookAddForm(request.form).validate_for_api() name = form.name.data author_id = form.author_id.data category_id = form.category_id.data isbn = form.isbn.data content = form.content.data # image_url 字段通过request获取 file = request.files['image_url'] if file and allowed_file(file.filename): file_name = rename_for_upload(file.filename) upload_path = os.path.join('static/uploads/', file_name) file.save(upload_path) book = Book() book.name = name book.author_id = author_id book.category_id = category_id book.isbn = isbn book.content = content book.image_url = upload_path book.save() return CreateSuccess() else: return ParameterError()
def add_new_book(): name = request.form.get('name') author_id = request.form.get('author_id') category_id = request.form.get('category_id') isbn = request.form.get('isbn') content = request.form.get('content') file = request.files['image_url'] if file and allowed_file(file.filename): file_name = rename_for_upload(file.filename) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径(一定要相对路径) upload_path = os.path.join('static/uploads/', file_name) file.save(upload_path) book = Book() book.name = name book.author_id = author_id book.category_id = category_id book.isbn = isbn book.content = content book.image_url = upload_path try: book.save() return CreateSuccess() except: return ParameterError() else: return ParameterError()
def addBook(): """""" response_result = ResponseResult() book_collection_name = request.args.get("currentBookCollectionName") book_content = request.args.get("bookContent") book_collection = BookCollection().query.filter_by(name=book_collection_name).first() book_collection_id = book_collection.id book = Book() book.book_collection_id = book_collection_id book.content = book_content db.session.add(book) db.session.commit() return json.dumps(response_result, default=lambda o: o.__dict__)
def submitBook(): """""" response_result = ResponseResult() book_id = request.args.get("editBookId") book_content = request.args.get("editContent") print(book_id) print(book_content) if (book_id != "-1"): try: edit_book = Book().query.filter_by(id=book_id).first() edit_book.content = book_content db.session.commit() response_result.message = "提交成功" except Exception as e: print(e) response_result.ret = "fail" response_result.message = "提交失败" else: response_result.ret = "fail" response_result.message = "还没有选中笔记呢!" return json.dumps(response_result, default=lambda o: o.__dict__)