示例#1
0
def add_book_bulk(batch, root, book_file, cover_file, metadata):
    if 'title' not in metadata or not metadata['title'].strip():
        raise ValueError("Title must not be blank: " + book_file)

    ids = {'isbn': metadata.get('id_isbn'),
           'calibre': metadata.get('id_calibre'),
           'amazon': metadata.get('id_amazon'),
           'google': metadata.get('id_google')}

    for id_type, identifer in ids.iteritems():
        if identifer:
            if get_book_by_identifier(id_type, identifer):
                raise RuntimeError("There is already a book with identifier [" + id_type + ": " + identifer + "]!")

    book = Book()
    book.batch = batch
    book.title = metadata['title']
    book.title_sort = metadata['title_sort'] if 'title_sort' in metadata else metadata['title']
    book.description = metadata['description'] if 'description' in metadata else None
    book.series_seq = int(metadata['series_index']) if 'series_index' in metadata else None
    book.public = True
    book.user = get_user('devin')
    book.created_at = datetime.now()

    book.id_isbn = ids['isbn']
    book.id_amazon = ids['amazon']
    book.id_google = ids['google']
    book.id_calibre = ids['calibre']

    taxonomies = {}

    if 'publisher' in metadata:
        taxonomies['publisher'] = [metadata['publisher']]
    if 'series' in metadata:
        taxonomies['series'] = [metadata['series']]

    if 'creator' in metadata and 'creator_sort' in metadata:
        if isinstance(metadata['creator'], list):
            taxonomies['author'] = []
            for i, c in enumerate(metadata['creator']):
                taxonomies['author'].append((c, metadata['creator_sort'][i]))
        else:
            taxonomies['author'] = [ (metadata['creator'], metadata['creator_sort'])]
    elif 'creator' in metadata:
        if isinstance(metadata['creator'], list):
            taxonomies['author'] = []
            for i, c in enumerate(metadata['creator']):
                taxonomies['author'].append((c, c))
        else:
            taxonomies['author'] = [ (metadata['creator'], metadata['creator'])]

    if 'subject' in metadata:
        tags = []
        if not isinstance(metadata['subject'], list):
            metadata['subject'] = [metadata['subject']]

        for subject in metadata['subject']:
            tags.append(subject.lower())

        if len(tags) > 0:
            taxonomies['tag'] = tags

    book.update_taxonomies(taxonomies)

    cover_dest_path = cover_upload_set.config.destination
    new_cover_file = re.sub('[^0-9a-zA-Z\._\-]+', '', cover_file.replace(" ", "_"))

    if os.path.exists(os.path.join(cover_dest_path, new_cover_file)):
        new_cover_file = cover_upload_set.resolve_conflict(cover_dest_path, new_cover_file)
        
    shutil.copy(os.path.join(root, cover_file), cover_upload_set.path(new_cover_file))
    
    book_dest_path = book_upload_set.config.destination
    new_book_file = re.sub('[^0-9a-zA-Z\._\-]+', '', book_file.replace(" ", "_"))

    if os.path.exists(os.path.join(book_dest_path, new_book_file)):
        new_book_file = book_upload_set.resolve_conflict(book_dest_path, new_book_file)
        
    shutil.copy(os.path.join(root, book_file), book_upload_set.path(new_book_file))

    book.cover = new_cover_file
    book.filename = new_book_file

    db.session.add(book)

    if 'rating' in metadata and metadata['rating'] > 0:
        book.user.get_book_meta(book.id).rating = metadata['rating']

    db.session.commit()

    if app.config['WRITE_META_ON_SAVE']:
       book.write_meta()

    book.update_word_count()

    return True
示例#2
0
def add_book(form, files):
    if 'title' not in form or not form['title'].strip():
        raise ValueError("Title must not be blank")

    book = Book()
    book.title = form['title']
    book.title_sort = form['title_sort']
    book.description = form['description']
    book.series_seq = int(form['series_seq']) if form['series_seq'] else None
    book.public = True if form['privacy'] == 'public' else False
    book.user = current_user
    book.created_at = datetime.now()
    book.owned = True if form['owned'] else False

    book.id_isbn = form['id_isbn']
    book.id_calibre = form['id_calibre']

    book.update_taxonomies({
        'author':  [(form['author'], form['author_sort'])],
        'publisher': [form['publisher']],
        'series': [form['series']],
        'tag': form['tags'].split(','),
    })

    if 'meta-cover' in form and form['meta-cover']:
        book.move_cover_from_tmp(form['meta-cover'])
    elif 'cover' in files:
        book.attempt_to_update_cover(files['cover'])

    if 'file' in form and form['file']:
        book.move_file_from_staging(form['file'])

    db.session.add(book)
    db.session.commit()

    if app.config['WRITE_META_ON_SAVE']:
        book.write_meta()

    book.update_word_count()

    return True