示例#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)
示例#2
0
    def add_book():
        """Add a book to the library"""

        new_book = Book()

        if request.method == 'POST':
            data = request.get_json(force=True)
            new_book.title = data.get('book_title')
            new_book.publisher = data.get('publisher')
            new_book.publication_year = data.get('publication_year')
            new_book.edition = data.get('edition')
            new_book.category = data.get('category')
            new_book.subcategory = data.get('subcategory')
            new_book.description = data.get('description')

            new_book.save()

            return jsonify(new_book.serialize()), 201
示例#3
0
    def book_update_delete(book_id):
        """Update or delete a specific book with a given id"""

        book = Book().get_by_id(book_id)

        if not book:
            return jsonify({'message':
                            'The requested book was not found'}), 404

        if request.method == 'DELETE':
            book.delete()
            return jsonify({'message': 'Successfully deleted'}), 204

        elif request.method == 'PUT':
            data = request.get_json(force=True)
            book.title = data.get('book_title')
            book.publisher = data.get('publisher')
            book.publication_year = data.get('publication_year')
            book.edition = data.get('publisher')
            book.category = data.get('category')
            book.subcategory = data.get('subcategory')
            book.description = data.get('description')

            return jsonify(book.serialize()), 200