Exemple #1
0
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()
Exemple #2
0
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()