1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-24 00:50:30 +00:00

Merge author rename

This commit is contained in:
Ozzieisaacs 2021-11-13 17:57:01 +04:00
parent 4fd4cf4355
commit 8e5bb02a28
6 changed files with 90 additions and 56 deletions

View File

@ -39,7 +39,6 @@ except ImportError:
from sqlalchemy.pool import StaticPool from sqlalchemy.pool import StaticPool
from sqlalchemy.sql.expression import and_, true, false, text, func, or_ from sqlalchemy.sql.expression import and_, true, false, text, func, or_
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import joinedload
from flask_login import current_user from flask_login import current_user
from flask_babel import gettext as _ from flask_babel import gettext as _
from flask import flash from flask import flash
@ -759,10 +758,13 @@ class CalibreDB():
entries = query.order_by(*order).offset(off).limit(pagesize).all() entries = query.order_by(*order).offset(off).limit(pagesize).all()
except Exception as ex: except Exception as ex:
log.debug_or_exception(ex) log.debug_or_exception(ex)
# display authors in right order
entries = self.order_authors(entries, True)
return entries, randm, pagination return entries, randm, pagination
# Orders all Authors in the list according to authors sort # Orders all Authors in the list according to authors sort
def order_authors(self, entry): def order_authors(self, entries, list_return=False):
for entry in entries:
sort_authors = entry.author_sort.split('&') sort_authors = entry.author_sort.split('&')
authors_ordered = list() authors_ordered = list()
error = False error = False
@ -778,6 +780,9 @@ class CalibreDB():
authors_ordered.append(r) authors_ordered.append(r)
if not error: if not error:
entry.authors = authors_ordered entry.authors = authors_ordered
if list_return:
return entries
else:
return authors_ordered return authors_ordered
def get_typeahead(self, database, query, replace=('', ''), tag_filter=true()): def get_typeahead(self, database, query, replace=('', ''), tag_filter=true()):
@ -839,10 +844,10 @@ class CalibreDB():
)) ))
# read search results from calibre-database and return it (function is used for feed and simple search # read search results from calibre-database and return it (function is used for feed and simple search
def get_search_results(self, term, offset=None, order=None, limit=None, *join): def get_search_results(self, term, offset=None, order=None, limit=None, config_read_column=False, *join):
order = order[0] if order else [Books.sort] order = order[0] if order else [Books.sort]
pagination = None pagination = None
result = self.search_query(term, *join).order_by(*order).all() result = self.search_query(term, config_read_column, *join).order_by(*order).all()
result_count = len(result) result_count = len(result)
if offset != None and limit != None: if offset != None and limit != None:
offset = int(offset) offset = int(offset)
@ -853,6 +858,9 @@ class CalibreDB():
limit_all = result_count limit_all = result_count
ub.store_combo_ids(result) ub.store_combo_ids(result)
# ToDo: doesn't work as more than one table returned
# entries = self.order_authors(result[offset:limit_all], True)
return result[offset:limit_all], result_count, pagination return result[offset:limit_all], result_count, pagination
# Creates for all stored languages a translated speaking name in the array for the UI # Creates for all stored languages a translated speaking name in the array for the UI

View File

@ -171,7 +171,7 @@ def add_objects(db_book_object, db_object, db_session, db_type, add_elements):
def create_objects_for_addition(db_element, add_element, db_type): def create_objects_for_addition(db_element, add_element, db_type):
if db_type == 'custom': if db_type == 'custom':
if db_element.value != add_element: if db_element.value != add_element:
db_element.value = add_element # ToDo: Before new_element, but this is not plausible db_element.value = add_element
elif db_type == 'languages': elif db_type == 'languages':
if db_element.lang_code != add_element: if db_element.lang_code != add_element:
db_element.lang_code = add_element db_element.lang_code = add_element
@ -182,7 +182,7 @@ def create_objects_for_addition(db_element, add_element, db_type):
elif db_type == 'author': elif db_type == 'author':
if db_element.name != add_element: if db_element.name != add_element:
db_element.name = add_element db_element.name = add_element
db_element.sort = add_element.replace('|', ',') db_element.sort = helper.get_sorted_author(add_element.replace('|', ','))
elif db_type == 'publisher': elif db_type == 'publisher':
if db_element.name != add_element: if db_element.name != add_element:
db_element.name = add_element db_element.name = add_element
@ -376,7 +376,7 @@ def render_edit_book(book_id):
for lang in book.languages: for lang in book.languages:
lang.language_name = isoLanguages.get_language_name(get_locale(), lang.lang_code) lang.language_name = isoLanguages.get_language_name(get_locale(), lang.lang_code)
book.authors = calibre_db.order_authors(book) book.authors = calibre_db.order_authors([book])
author_names = [] author_names = []
for authr in book.authors: for authr in book.authors:
@ -1286,7 +1286,7 @@ def table_xchange_author_title():
modif_date = False modif_date = False
book = calibre_db.get_book(val) book = calibre_db.get_book(val)
authors = book.title authors = book.title
book.authors = calibre_db.order_authors(book) book.authors = calibre_db.order_authors([book])
author_names = [] author_names = []
for authr in book.authors: for authr in book.authors:
author_names.append(authr.name.replace('|', ',')) author_names.append(authr.name.replace('|', ','))

View File

@ -334,6 +334,32 @@ def delete_book_file(book, calibrepath, book_format=None):
id=book.id, id=book.id,
path=book.path) path=book.path)
def clean_author_database(renamed_author, calibrepath, local_book=None):
valid_filename_authors = [get_valid_filename(r) for r in renamed_author]
for r in renamed_author:
if local_book:
all_books = [local_book]
else:
all_books = calibre_db.session.query(db.Books) \
.filter(db.Books.authors.any(db.Authors.name == r)).all()
for book in all_books:
book_author_path = book.path.split('/')[0]
if book_author_path in valid_filename_authors or local_book:
new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
all_new_authordir = get_valid_filename(new_author.name)
all_titledir = book.path.split('/')[1]
all_new_path = os.path.join(calibrepath, all_new_authordir, all_titledir)
all_new_name = get_valid_filename(book.title) + ' - ' + all_new_authordir
# change location in database to new author/title path
book.path = os.path.join(all_new_authordir, all_titledir).replace('\\', '/')
for file_format in book.data:
shutil.move(os.path.normcase(
os.path.join(all_new_path, file_format.name + '.' + file_format.format.lower())),
os.path.normcase(os.path.join(all_new_path, all_new_name + '.' + file_format.format.lower())))
file_format.name = all_new_name
# was muss gemacht werden: # was muss gemacht werden:
# Die Autorennamen müssen separiert werden und von dupletten bereinigt werden. # Die Autorennamen müssen separiert werden und von dupletten bereinigt werden.
# Es muss geprüft werden: # Es muss geprüft werden:
@ -366,13 +392,16 @@ def update_dir_structure_file(book_id, calibrepath, first_author, orignal_filepa
if first_author: if first_author:
new_authordir = get_valid_filename(first_author) new_authordir = get_valid_filename(first_author)
for r in renamed_author: for r in renamed_author:
if first_author.lower() == r.lower(): new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
old_author_dir = get_valid_filename(r)
new_author_rename_dir = get_valid_filename(new_author.name)
if os.path.isdir(os.path.join(calibrepath, old_author_dir)):
try: try:
new_author_path = os.path.join(calibrepath, new_authordir) old_author_path = os.path.join(calibrepath, old_author_dir)
old_author_path = os.path.join(calibrepath, r) new_author_path = os.path.join(calibrepath, new_author_rename_dir)
shutil.move(os.path.normcase(old_author_path), os.path.normcase(new_author_path)) shutil.move(os.path.normcase(old_author_path), os.path.normcase(new_author_path))
except (OSError) as ex: except (OSError) as ex:
log.error("Rename author from: %s to %s: %s", r, new_authordir, ex) log.error("Rename author from: %s to %s: %s", old_author_path, new_author_path, ex)
log.debug(ex, exc_info=True) log.debug(ex, exc_info=True)
return _("Rename author from: '%(src)s' to '%(dest)s' failed with error: %(error)s", return _("Rename author from: '%(src)s' to '%(dest)s' failed with error: %(error)s",
src=old_author_path, dest=new_author_path, error=str(ex)) src=old_author_path, dest=new_author_path, error=str(ex))
@ -411,26 +440,16 @@ def update_dir_structure_file(book_id, calibrepath, first_author, orignal_filepa
# Rename all files from old names to new names # Rename all files from old names to new names
try: try:
all_books = calibre_db.session.query(db.Books)\ clean_author_database(renamed_author, calibrepath)
.filter(db.Books.authors.any(db.Authors.name == renamed_author)).all()
for book in all_books: if first_author not in renamed_author:
all_titledir = book.path.split('/')[1] clean_author_database([first_author], calibrepath, localbook)
all_new_path = os.path.join(calibrepath, new_authordir, all_titledir)
all_new_name = get_valid_filename(book.title) + ' - ' + new_authordir
# change location in database to new author/title path
book.path = os.path.join(new_authordir, all_titledir).replace('\\', '/')
for file_format in book.data:
shutil.move(os.path.normcase(
os.path.join(all_new_path, file_format.name + '.' + file_format.format.lower())),
os.path.normcase(os.path.join(all_new_path, all_new_name + '.' + file_format.format.lower())))
file_format.name = all_new_name
if not renamed_author and not orignal_filepath and len(os.listdir(os.path.dirname(path))) == 0: if not renamed_author and not orignal_filepath and len(os.listdir(os.path.dirname(path))) == 0:
shutil.rmtree(os.path.dirname(path)) shutil.rmtree(os.path.dirname(path))
except (OSError) as ex: except (OSError) as ex:
log.error("Rename file in path %s to %s: %s", all_new_path, all_new_name, ex) log.error("Error in rename file in path %s", ex)
log.debug(ex, exc_info=True) log.debug(ex, exc_info=True)
return _("Rename file in path '%(src)s' to '%(dest)s' failed with error: %(error)s", return _("Error in rename file in path: %(error)s", error=str(ex))
src=new_path, dest=new_name, error=str(ex))
return False return False
def update_dir_structure_gdrive(book_id, first_author): def update_dir_structure_gdrive(book_id, first_author):

View File

@ -20,23 +20,21 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import datetime import datetime
from urllib.parse import unquote_plus
from functools import wraps from functools import wraps
from flask import Blueprint, request, render_template, Response, g, make_response, abort from flask import Blueprint, request, render_template, Response, g, make_response, abort
from flask_login import current_user from flask_login import current_user
from sqlalchemy.sql.expression import func, text, or_, and_, true from sqlalchemy.sql.expression import func, text, or_, and_, true
from werkzeug.security import check_password_hash from werkzeug.security import check_password_hash
from tornado.httputil import HTTPServerRequest
from . import constants, logger, config, db, calibre_db, ub, services, get_locale, isoLanguages from . import constants, logger, config, db, calibre_db, ub, services, get_locale, isoLanguages
from .helper import get_download_link, get_book_cover from .helper import get_download_link, get_book_cover
from .pagination import Pagination from .pagination import Pagination
from .web import render_read_books from .web import render_read_books
from .usermanagement import load_user_from_request from .usermanagement import load_user_from_request
from flask_babel import gettext as _ from flask_babel import gettext as _
from babel import Locale as LC
from babel.core import UnknownLocaleError
opds = Blueprint('opds', __name__) opds = Blueprint('opds', __name__)
@ -84,10 +82,12 @@ def feed_osd():
@opds.route("/opds/search", defaults={'query': ""}) @opds.route("/opds/search", defaults={'query': ""})
@opds.route("/opds/search/<query>") @opds.route("/opds/search/<path:query>")
@requires_basic_auth_if_no_ano @requires_basic_auth_if_no_ano
def feed_cc_search(query): def feed_cc_search(query):
return feed_search(query.strip()) # Handle strange query from Libera Reader with + instead of spaces
plus_query = unquote_plus(request.base_url.split('/opds/search/')[1]).strip()
return feed_search(plus_query)
@opds.route("/opds/search", methods=["GET"]) @opds.route("/opds/search", methods=["GET"])
@ -527,7 +527,7 @@ def get_metadata_calibre_companion(uuid, library):
def feed_search(term): def feed_search(term):
if term: if term:
entries, __, ___ = calibre_db.get_search_results(term) entries, __, ___ = calibre_db.get_search_results(term, config_read_column=config.config_read_column)
entriescount = len(entries) if len(entries) > 0 else 1 entriescount = len(entries) if len(entries) > 0 else 1
pagination = Pagination(1, entriescount, entriescount) pagination = Pagination(1, entriescount, entriescount)
return render_xml_template('feed.xml', searchterm=term, entries=entries, pagination=pagination) return render_xml_template('feed.xml', searchterm=term, entries=entries, pagination=pagination)

View File

@ -83,6 +83,8 @@ def signal_store_user_session(object, user):
store_user_session() store_user_session()
def store_user_session(): def store_user_session():
if flask_session.get('user_id', ""):
flask_session['_user_id'] = flask_session.get('user_id', "")
if flask_session.get('_user_id', ""): if flask_session.get('_user_id', ""):
try: try:
if not check_user_session(flask_session.get('_user_id', ""), flask_session.get('_id', "")): if not check_user_session(flask_session.get('_user_id', ""), flask_session.get('_id', "")):

View File

@ -543,7 +543,6 @@ def render_author_books(page, author_id, order):
if services.goodreads_support and config.config_use_goodreads: if services.goodreads_support and config.config_use_goodreads:
author_info = services.goodreads_support.get_author_info(author_name) author_info = services.goodreads_support.get_author_info(author_name)
other_books = services.goodreads_support.get_other_books(author_info, entries) other_books = services.goodreads_support.get_other_books(author_info, entries)
return render_title_template('author.html', entries=entries, pagination=pagination, id=author_id, return render_title_template('author.html', entries=entries, pagination=pagination, id=author_id,
title=_(u"Author: %(name)s", name=author_name), author=author_info, title=_(u"Author: %(name)s", name=author_name), author=author_info,
other_books=other_books, page="author", order=order[1]) other_books=other_books, page="author", order=order[1])
@ -753,7 +752,12 @@ def render_prepare_search_form(cc):
def render_search_results(term, offset=None, order=None, limit=None): def render_search_results(term, offset=None, order=None, limit=None):
join = db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series join = db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series
entries, result_count, pagination = calibre_db.get_search_results(term, offset, order, limit, *join) entries, result_count, pagination = calibre_db.get_search_results(term,
offset,
order,
limit,
config.config_read_column,
*join)
return render_title_template('search.html', return render_title_template('search.html',
searchterm=term, searchterm=term,
pagination=pagination, pagination=pagination,
@ -832,7 +836,7 @@ def list_books():
total_count = filtered_count = calibre_db.session.query(db.Books).filter(calibre_db.common_filters(False)).count() total_count = filtered_count = calibre_db.session.query(db.Books).filter(calibre_db.common_filters(False)).count()
if state is not None: if state is not None:
if search: if search:
books = calibre_db.search_query(search).all() books = calibre_db.search_query(search, config.config_read_column).all()
filtered_count = len(books) filtered_count = len(books)
else: else:
if not config.config_read_column: if not config.config_read_column:
@ -1424,10 +1428,11 @@ def render_adv_search_results(term, offset=None, order=None, limit=None):
else: else:
offset = 0 offset = 0
limit_all = result_count limit_all = result_count
entries = calibre_db.order_authors(q[offset:limit_all], True)
return render_title_template('search.html', return render_title_template('search.html',
adv_searchterm=searchterm, adv_searchterm=searchterm,
pagination=pagination, pagination=pagination,
entries=q[offset:limit_all], entries=entries,
result_count=result_count, result_count=result_count,
title=_(u"Advanced Search"), page="advsearch", title=_(u"Advanced Search"), page="advsearch",
order=order[1]) order=order[1])
@ -1830,7 +1835,7 @@ def show_book(book_id):
entry.tags = sort(entry.tags, key=lambda tag: tag.name) entry.tags = sort(entry.tags, key=lambda tag: tag.name)
entry.authors = calibre_db.order_authors(entry) entry.authors = calibre_db.order_authors([entry])
entry.kindle_list = check_send_to_kindle(entry) entry.kindle_list = check_send_to_kindle(entry)
entry.reader_list = check_read_formats(entry) entry.reader_list = check_read_formats(entry)