示例#1
0
def addbook():
    form = AddBookForm()
    book = Book(uploader_id=current_user.id)
    ts = str(time.time()).split('.')[0]
    if form.validate_on_submit():
        if form.book_img.data:
            f = form.book_img.data
            file_name = ts + '_' + f.filename
            file_path = os.path.join(config['default'].UPLOAD_PATH + '/books/',
                                     file_name)
            f.save(file_path)
            book.image_path = url_for('static',
                                      filename='uploads/books/' + file_name)
        book.name = form.name.data
        book.author = form.author.data or u'无'
        book.description = form.description.data or u'暂无介绍'
        book.category_id = form.category.data
        if form.tag.data and len(form.tag.data.split(';')) > 0:
            for i in form.tag.data.split(';'):
                tag = Tag.findOrInsert(i)
                if tag == None:
                    continue
                book.tags.append(tag)
        book.status_id = form.status.data
        book.upload_user = current_user._get_current_object()
        book.isbn = form.isbn.data or u'暂无'
        book.total_count = int(form.total_count.data or 0)
        book.book_number = form.book_number.data or u'暂无'
        book.publisher = form.publisher.data or u'暂无'

        try:
            if form.book_img.data is None:
                id, img_200_200_path, img_350_350_path = download_images_from_bookname(
                    form.name.data, config['default'].UPLOAD_PATH + '/books/')
                book.description = get_detail_info(id)[0]
                book.image_path = img_350_350_path.split('/app')[-1]
        except Exception as e:
            raise e
        db.session.add(book)
        flash('新增图书成功')
        return redirect(url_for('main.addbook'))
    return custom_render_template('book/add_book.html', form=form)