2019-07-13 18:45:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
import os
|
2020-04-20 16:56:39 +00:00
|
|
|
from datetime import datetime
|
2019-07-13 18:45:48 +00:00
|
|
|
import json
|
2020-05-09 09:03:11 +00:00
|
|
|
from shutil import copyfile
|
2019-07-13 18:45:48 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
from babel import Locale as LC
|
|
|
|
from babel.core import UnknownLocaleError
|
2019-07-13 18:45:48 +00:00
|
|
|
from flask import Blueprint, request, flash, redirect, url_for, abort, Markup, Response
|
|
|
|
from flask_babel import gettext as _
|
2019-07-14 17:28:32 +00:00
|
|
|
from flask_login import current_user, login_required
|
2021-01-10 09:23:14 +00:00
|
|
|
from sqlalchemy.exc import OperationalError, IntegrityError
|
2021-01-10 14:02:04 +00:00
|
|
|
from sqlite3 import OperationalError as sqliteOperationalError
|
2019-07-13 18:45:48 +00:00
|
|
|
from . import constants, logger, isoLanguages, gdriveutils, uploader, helper
|
2020-08-23 03:35:48 +00:00
|
|
|
from . import config, get_locale, ub, db
|
2020-05-21 16:16:11 +00:00
|
|
|
from . import calibre_db
|
2020-08-23 02:44:28 +00:00
|
|
|
from .services.worker import WorkerThread
|
|
|
|
from .tasks.upload import TaskUpload
|
2020-12-12 10:23:17 +00:00
|
|
|
from .render_template import render_title_template
|
|
|
|
from .usermanagement import login_required_if_no_ano
|
|
|
|
|
|
|
|
try:
|
|
|
|
from functools import wraps
|
|
|
|
except ImportError:
|
|
|
|
pass # We're not using Python 3
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
editbook = Blueprint('editbook', __name__)
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
2020-12-12 10:23:17 +00:00
|
|
|
def upload_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_upload() or current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
def edit_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_edit() or current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
2021-03-21 13:17:07 +00:00
|
|
|
def search_objects_remove(db_book_object, db_type, input_elements):
|
2019-07-13 18:45:48 +00:00
|
|
|
del_elements = []
|
|
|
|
for c_elements in db_book_object:
|
|
|
|
found = False
|
|
|
|
if db_type == 'languages':
|
|
|
|
type_elements = c_elements.lang_code
|
|
|
|
elif db_type == 'custom':
|
|
|
|
type_elements = c_elements.value
|
|
|
|
else:
|
|
|
|
type_elements = c_elements.name
|
|
|
|
for inp_element in input_elements:
|
|
|
|
if inp_element.lower() == type_elements.lower():
|
|
|
|
# if inp_element == type_elements:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
# if the element was not found in the new list, add it to remove list
|
|
|
|
if not found:
|
|
|
|
del_elements.append(c_elements)
|
2021-03-21 13:17:07 +00:00
|
|
|
return del_elements
|
|
|
|
|
|
|
|
|
|
|
|
def search_objects_add(db_book_object, db_type, input_elements):
|
2019-07-13 18:45:48 +00:00
|
|
|
add_elements = []
|
|
|
|
for inp_element in input_elements:
|
|
|
|
found = False
|
|
|
|
for c_elements in db_book_object:
|
|
|
|
if db_type == 'languages':
|
|
|
|
type_elements = c_elements.lang_code
|
|
|
|
elif db_type == 'custom':
|
|
|
|
type_elements = c_elements.value
|
|
|
|
else:
|
|
|
|
type_elements = c_elements.name
|
|
|
|
if inp_element == type_elements:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
add_elements.append(inp_element)
|
2021-03-21 13:17:07 +00:00
|
|
|
return add_elements
|
|
|
|
|
|
|
|
|
|
|
|
def remove_objects(db_book_object, db_session, del_elements):
|
2021-03-23 16:57:49 +00:00
|
|
|
changed = False
|
2019-07-13 18:45:48 +00:00
|
|
|
if len(del_elements) > 0:
|
|
|
|
for del_element in del_elements:
|
|
|
|
db_book_object.remove(del_element)
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = True
|
2019-07-13 18:45:48 +00:00
|
|
|
if len(del_element.books) == 0:
|
|
|
|
db_session.delete(del_element)
|
2021-03-23 16:57:49 +00:00
|
|
|
return changed
|
2021-03-21 13:17:07 +00:00
|
|
|
|
|
|
|
def add_objects(db_book_object, db_object, db_session, db_type, add_elements):
|
|
|
|
changed = False
|
|
|
|
if db_type == 'languages':
|
|
|
|
db_filter = db_object.lang_code
|
|
|
|
elif db_type == 'custom':
|
|
|
|
db_filter = db_object.value
|
|
|
|
else:
|
|
|
|
db_filter = db_object.name
|
|
|
|
for add_element in add_elements:
|
|
|
|
# check if a element with that name exists
|
|
|
|
db_element = db_session.query(db_object).filter(db_filter == add_element).first()
|
|
|
|
# if no element is found add it
|
|
|
|
# if new_element is None:
|
|
|
|
if db_type == 'author':
|
|
|
|
new_element = db_object(add_element, helper.get_sorted_author(add_element.replace('|', ',')), "")
|
|
|
|
elif db_type == 'series':
|
|
|
|
new_element = db_object(add_element, add_element)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif db_type == 'custom':
|
2021-03-21 13:17:07 +00:00
|
|
|
new_element = db_object(value=add_element)
|
|
|
|
elif db_type == 'publisher':
|
|
|
|
new_element = db_object(add_element, None)
|
|
|
|
else: # db_type should be tag or language
|
|
|
|
new_element = db_object(add_element)
|
|
|
|
if db_element is None:
|
|
|
|
changed = True
|
|
|
|
db_session.add(new_element)
|
|
|
|
db_book_object.append(new_element)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2021-03-21 13:17:07 +00:00
|
|
|
db_element = create_objects_for_addition(db_element, add_element, db_type)
|
|
|
|
changed = True
|
|
|
|
# add element to book
|
2021-03-23 16:57:49 +00:00
|
|
|
changed = True
|
2021-03-21 13:17:07 +00:00
|
|
|
db_book_object.append(db_element)
|
2020-04-20 16:56:39 +00:00
|
|
|
return changed
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-03-21 13:17:07 +00:00
|
|
|
def create_objects_for_addition(db_element, add_element, db_type):
|
|
|
|
if db_type == 'custom':
|
|
|
|
if db_element.value != add_element:
|
2021-03-23 16:57:49 +00:00
|
|
|
db_element.value = add_element # ToDo: Before new_element, but this is not plausible
|
2021-03-21 13:17:07 +00:00
|
|
|
elif db_type == 'languages':
|
|
|
|
if db_element.lang_code != add_element:
|
|
|
|
db_element.lang_code = add_element
|
|
|
|
elif db_type == 'series':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
db_element.sort = add_element
|
|
|
|
elif db_type == 'author':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
db_element.sort = add_element.replace('|', ',')
|
|
|
|
elif db_type == 'publisher':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
db_element.sort = None
|
|
|
|
elif db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
2021-03-23 16:57:49 +00:00
|
|
|
return db_element
|
2021-03-21 13:17:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Modifies different Database objects, first check if elements if elements have to be deleted,
|
|
|
|
# because they are no longer used, than check if elements have to be added to database
|
|
|
|
def modify_database_object(input_elements, db_book_object, db_object, db_session, db_type):
|
|
|
|
# passing input_elements not as a list may lead to undesired results
|
|
|
|
if not isinstance(input_elements, list):
|
|
|
|
raise TypeError(str(input_elements) + " should be passed as a list")
|
|
|
|
input_elements = [x for x in input_elements if x != '']
|
|
|
|
# we have all input element (authors, series, tags) names now
|
|
|
|
# 1. search for elements to remove
|
|
|
|
del_elements = search_objects_remove(db_book_object, db_type, input_elements)
|
|
|
|
# 2. search for elements that need to be added
|
|
|
|
add_elements = search_objects_add(db_book_object, db_type, input_elements)
|
|
|
|
# if there are elements to remove, we remove them now
|
2021-03-23 16:57:49 +00:00
|
|
|
changed = remove_objects(db_book_object, db_session, del_elements)
|
2021-03-21 13:17:07 +00:00
|
|
|
# if there are elements to add, we add them now!
|
|
|
|
if len(add_elements) > 0:
|
2021-03-23 16:57:49 +00:00
|
|
|
changed |= add_objects(db_book_object, db_object, db_session, db_type, add_elements)
|
|
|
|
return changed
|
2021-03-21 13:17:07 +00:00
|
|
|
|
|
|
|
|
2020-01-12 22:23:43 +00:00
|
|
|
def modify_identifiers(input_identifiers, db_identifiers, db_session):
|
|
|
|
"""Modify Identifiers to match input information.
|
|
|
|
input_identifiers is a list of read-to-persist Identifiers objects.
|
|
|
|
db_identifiers is a list of already persisted list of Identifiers objects."""
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = False
|
2020-09-05 16:46:11 +00:00
|
|
|
error = False
|
|
|
|
input_dict = dict([(identifier.type.lower(), identifier) for identifier in input_identifiers])
|
|
|
|
if len(input_identifiers) != len(input_dict):
|
|
|
|
error = True
|
|
|
|
db_dict = dict([(identifier.type.lower(), identifier) for identifier in db_identifiers ])
|
2020-01-12 22:23:43 +00:00
|
|
|
# delete db identifiers not present in input or modify them with input val
|
|
|
|
for identifier_type, identifier in db_dict.items():
|
|
|
|
if identifier_type not in input_dict.keys():
|
|
|
|
db_session.delete(identifier)
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = True
|
2020-01-12 22:23:43 +00:00
|
|
|
else:
|
|
|
|
input_identifier = input_dict[identifier_type]
|
|
|
|
identifier.type = input_identifier.type
|
|
|
|
identifier.val = input_identifier.val
|
|
|
|
# add input identifiers not present in db
|
|
|
|
for identifier_type, identifier in input_dict.items():
|
|
|
|
if identifier_type not in db_dict.keys():
|
|
|
|
db_session.add(identifier)
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = True
|
2020-09-05 16:46:11 +00:00
|
|
|
return changed, error
|
2020-01-12 22:23:43 +00:00
|
|
|
|
2020-06-18 18:39:45 +00:00
|
|
|
@editbook.route("/ajax/delete/<int:book_id>")
|
|
|
|
@login_required
|
|
|
|
def delete_book_from_details(book_id):
|
2021-04-07 16:19:48 +00:00
|
|
|
return Response(delete_book(book_id, "", True), mimetype='application/json')
|
2020-06-18 18:39:45 +00:00
|
|
|
|
2020-01-12 22:23:43 +00:00
|
|
|
|
2020-06-18 18:39:45 +00:00
|
|
|
@editbook.route("/delete/<int:book_id>", defaults={'book_format': ""})
|
|
|
|
@editbook.route("/delete/<int:book_id>/<string:book_format>")
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
2020-06-18 18:39:45 +00:00
|
|
|
def delete_book_ajax(book_id, book_format):
|
2021-04-07 16:19:48 +00:00
|
|
|
return delete_book(book_id, book_format, False)
|
2020-06-18 18:39:45 +00:00
|
|
|
|
2021-03-15 08:55:59 +00:00
|
|
|
|
|
|
|
def delete_whole_book(book_id, book):
|
|
|
|
# delete book from Shelfs, Downloads, Read list
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete()
|
|
|
|
ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id).delete()
|
|
|
|
ub.delete_download(book_id)
|
|
|
|
ub.session_commit()
|
|
|
|
|
|
|
|
# check if only this book links to:
|
|
|
|
# author, language, series, tags, custom columns
|
|
|
|
modify_database_object([u''], book.authors, db.Authors, calibre_db.session, 'author')
|
|
|
|
modify_database_object([u''], book.tags, db.Tags, calibre_db.session, 'tags')
|
|
|
|
modify_database_object([u''], book.series, db.Series, calibre_db.session, 'series')
|
|
|
|
modify_database_object([u''], book.languages, db.Languages, calibre_db.session, 'languages')
|
|
|
|
modify_database_object([u''], book.publishers, db.Publishers, calibre_db.session, 'publishers')
|
|
|
|
|
|
|
|
cc = calibre_db.session.query(db.Custom_Columns). \
|
|
|
|
filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
|
|
|
for c in cc:
|
|
|
|
cc_string = "custom_column_" + str(c.id)
|
|
|
|
if not c.is_multiple:
|
|
|
|
if len(getattr(book, cc_string)) > 0:
|
|
|
|
if c.datatype == 'bool' or c.datatype == 'integer' or c.datatype == 'float':
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
log.debug('remove ' + str(c.id))
|
|
|
|
calibre_db.session.delete(del_cc)
|
|
|
|
calibre_db.session.commit()
|
|
|
|
elif c.datatype == 'rating':
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
if len(del_cc.books) == 0:
|
|
|
|
log.debug('remove ' + str(c.id))
|
|
|
|
calibre_db.session.delete(del_cc)
|
|
|
|
calibre_db.session.commit()
|
|
|
|
else:
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
log.debug('remove ' + str(c.id))
|
|
|
|
calibre_db.session.delete(del_cc)
|
|
|
|
calibre_db.session.commit()
|
|
|
|
else:
|
|
|
|
modify_database_object([u''], getattr(book, cc_string), db.cc_classes[c.id],
|
|
|
|
calibre_db.session, 'custom')
|
|
|
|
calibre_db.session.query(db.Books).filter(db.Books.id == book_id).delete()
|
|
|
|
|
|
|
|
|
|
|
|
def render_delete_book_result(book_format, jsonResponse, warning, book_id):
|
|
|
|
if book_format:
|
|
|
|
if jsonResponse:
|
|
|
|
return json.dumps([warning, {"location": url_for("editbook.edit_book", book_id=book_id),
|
|
|
|
"type": "success",
|
|
|
|
"format": book_format,
|
|
|
|
"message": _('Book Format Successfully Deleted')}])
|
|
|
|
else:
|
|
|
|
flash(_('Book Format Successfully Deleted'), category="success")
|
|
|
|
return redirect(url_for('editbook.edit_book', book_id=book_id))
|
|
|
|
else:
|
|
|
|
if jsonResponse:
|
|
|
|
return json.dumps([warning, {"location": url_for('web.index'),
|
|
|
|
"type": "success",
|
|
|
|
"format": book_format,
|
|
|
|
"message": _('Book Successfully Deleted')}])
|
|
|
|
else:
|
|
|
|
flash(_('Book Successfully Deleted'), category="success")
|
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
|
2020-06-18 18:39:45 +00:00
|
|
|
def delete_book(book_id, book_format, jsonResponse):
|
|
|
|
warning = {}
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user.role_delete_books():
|
2020-05-23 08:16:29 +00:00
|
|
|
book = calibre_db.get_book(book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
if book:
|
2020-04-26 18:44:37 +00:00
|
|
|
try:
|
2020-05-01 08:26:35 +00:00
|
|
|
result, error = helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper())
|
|
|
|
if not result:
|
2020-06-18 18:39:45 +00:00
|
|
|
if jsonResponse:
|
2021-04-13 17:08:02 +00:00
|
|
|
return json.dumps([{"location": url_for("editbook.edit_book", book_id=book_id),
|
|
|
|
"type": "danger",
|
2020-06-29 18:14:48 +00:00
|
|
|
"format": "",
|
2021-04-13 17:08:02 +00:00
|
|
|
"message": error}])
|
2020-06-18 18:39:45 +00:00
|
|
|
else:
|
|
|
|
flash(error, category="error")
|
|
|
|
return redirect(url_for('editbook.edit_book', book_id=book_id))
|
2020-05-04 16:19:30 +00:00
|
|
|
if error:
|
2020-06-18 18:39:45 +00:00
|
|
|
if jsonResponse:
|
2021-04-13 17:08:02 +00:00
|
|
|
warning = {"location": url_for("editbook.edit_book", book_id=book_id),
|
2020-06-18 18:39:45 +00:00
|
|
|
"type": "warning",
|
|
|
|
"format": "",
|
2021-04-13 17:08:02 +00:00
|
|
|
"message": error}
|
2020-06-18 18:39:45 +00:00
|
|
|
else:
|
|
|
|
flash(error, category="warning")
|
2020-04-26 18:44:37 +00:00
|
|
|
if not book_format:
|
2021-03-15 08:55:59 +00:00
|
|
|
delete_whole_book(book_id, book)
|
2020-04-26 18:44:37 +00:00
|
|
|
else:
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.query(db.Data).filter(db.Data.book == book.id).\
|
2020-04-26 18:44:37 +00:00
|
|
|
filter(db.Data.format == book_format).delete()
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.commit()
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.debug_or_exception(ex)
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.rollback()
|
2021-04-13 17:08:02 +00:00
|
|
|
if jsonResponse:
|
|
|
|
return json.dumps([{"location": url_for("editbook.edit_book", book_id=book_id),
|
|
|
|
"type": "danger",
|
|
|
|
"format": "",
|
|
|
|
"message": ex}])
|
|
|
|
else:
|
|
|
|
flash(str(ex), category="error")
|
|
|
|
return redirect(url_for('editbook.edit_book', book_id=book_id))
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
# book not found
|
|
|
|
log.error('Book with id "%s" could not be deleted: not found', book_id)
|
2021-03-15 08:55:59 +00:00
|
|
|
return render_delete_book_result(book_format, jsonResponse, warning, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_edit_book(book_id):
|
2020-05-21 16:16:11 +00:00
|
|
|
cc = calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
2020-12-09 18:02:10 +00:00
|
|
|
book = calibre_db.get_filtered_book(book_id, allow_show_archived=True)
|
2019-07-13 18:45:48 +00:00
|
|
|
if not book:
|
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible"), category="error")
|
|
|
|
return redirect(url_for("web.index"))
|
|
|
|
|
|
|
|
for lang in book.languages:
|
|
|
|
lang.language_name = isoLanguages.get_language_name(get_locale(), lang.lang_code)
|
|
|
|
|
2020-05-23 08:16:29 +00:00
|
|
|
book = calibre_db.order_authors(book)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
author_names = []
|
|
|
|
for authr in book.authors:
|
|
|
|
author_names.append(authr.name.replace('|', ','))
|
|
|
|
|
|
|
|
# Option for showing convertbook button
|
|
|
|
valid_source_formats=list()
|
2020-05-09 09:03:11 +00:00
|
|
|
allowed_conversion_formats = list()
|
|
|
|
kepub_possible=None
|
2020-05-02 08:18:01 +00:00
|
|
|
if config.config_converterpath:
|
2019-07-13 18:45:48 +00:00
|
|
|
for file in book.data:
|
2020-10-16 15:51:59 +00:00
|
|
|
if file.format.lower() in constants.EXTENSIONS_CONVERT_FROM:
|
2019-07-13 18:45:48 +00:00
|
|
|
valid_source_formats.append(file.format.lower())
|
2020-05-09 09:03:11 +00:00
|
|
|
if config.config_kepubifypath and 'epub' in [file.format.lower() for file in book.data]:
|
|
|
|
kepub_possible = True
|
|
|
|
if not config.config_converterpath:
|
|
|
|
valid_source_formats.append('epub')
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Determine what formats don't already exist
|
2020-05-09 09:03:11 +00:00
|
|
|
if config.config_converterpath:
|
2020-10-16 15:51:59 +00:00
|
|
|
allowed_conversion_formats = constants.EXTENSIONS_CONVERT_TO[:]
|
2020-05-09 09:03:11 +00:00
|
|
|
for file in book.data:
|
2020-05-23 14:20:19 +00:00
|
|
|
if file.format.lower() in allowed_conversion_formats:
|
2020-05-09 09:03:11 +00:00
|
|
|
allowed_conversion_formats.remove(file.format.lower())
|
|
|
|
if kepub_possible:
|
|
|
|
allowed_conversion_formats.append('kepub')
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,
|
|
|
|
title=_(u"edit metadata"), page="editbook",
|
|
|
|
conversion_formats=allowed_conversion_formats,
|
2020-01-26 21:20:10 +00:00
|
|
|
config=config,
|
2019-07-13 18:45:48 +00:00
|
|
|
source_formats=valid_source_formats)
|
|
|
|
|
|
|
|
|
2020-04-20 16:56:39 +00:00
|
|
|
def edit_book_ratings(to_save, book):
|
|
|
|
changed = False
|
|
|
|
if to_save["rating"].strip():
|
|
|
|
old_rating = False
|
|
|
|
if len(book.ratings) > 0:
|
|
|
|
old_rating = book.ratings[0].rating
|
|
|
|
ratingx2 = int(float(to_save["rating"]) * 2)
|
|
|
|
if ratingx2 != old_rating:
|
|
|
|
changed = True
|
2020-05-21 16:16:11 +00:00
|
|
|
is_rating = calibre_db.session.query(db.Ratings).filter(db.Ratings.rating == ratingx2).first()
|
2020-04-20 16:56:39 +00:00
|
|
|
if is_rating:
|
|
|
|
book.ratings.append(is_rating)
|
|
|
|
else:
|
|
|
|
new_rating = db.Ratings(rating=ratingx2)
|
|
|
|
book.ratings.append(new_rating)
|
|
|
|
if old_rating:
|
|
|
|
book.ratings.remove(book.ratings[0])
|
|
|
|
else:
|
|
|
|
if len(book.ratings) > 0:
|
|
|
|
book.ratings.remove(book.ratings[0])
|
|
|
|
changed = True
|
|
|
|
return changed
|
|
|
|
|
2020-05-24 18:19:43 +00:00
|
|
|
def edit_book_tags(tags, book):
|
|
|
|
input_tags = tags.split(',')
|
|
|
|
input_tags = list(map(lambda it: it.strip(), input_tags))
|
2020-06-22 17:11:03 +00:00
|
|
|
# Remove duplicates
|
|
|
|
input_tags = helper.uniq(input_tags)
|
2020-05-24 18:19:43 +00:00
|
|
|
return modify_database_object(input_tags, book.tags, db.Tags, calibre_db.session, 'tags')
|
2020-04-20 16:56:39 +00:00
|
|
|
|
2020-05-24 18:19:43 +00:00
|
|
|
|
|
|
|
def edit_book_series(series, book):
|
|
|
|
input_series = [series.strip()]
|
|
|
|
input_series = [x for x in input_series if x != '']
|
|
|
|
return modify_database_object(input_series, book.series, db.Series, calibre_db.session, 'series')
|
|
|
|
|
|
|
|
|
|
|
|
def edit_book_series_index(series_index, book):
|
|
|
|
# Add default series_index to book
|
|
|
|
modif_date = False
|
|
|
|
series_index = series_index or '1'
|
|
|
|
if book.series_index != series_index:
|
|
|
|
book.series_index = series_index
|
|
|
|
modif_date = True
|
|
|
|
return modif_date
|
|
|
|
|
|
|
|
# Handle book comments/description
|
|
|
|
def edit_book_comments(comments, book):
|
|
|
|
modif_date = False
|
|
|
|
if len(book.comments):
|
|
|
|
if book.comments[0].text != comments:
|
|
|
|
book.comments[0].text = comments
|
|
|
|
modif_date = True
|
|
|
|
else:
|
|
|
|
if comments:
|
|
|
|
book.comments.append(db.Comments(text=comments, book=book.id))
|
|
|
|
modif_date = True
|
|
|
|
return modif_date
|
|
|
|
|
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
def edit_book_languages(languages, book, upload=False, invalid=None):
|
2020-05-24 18:19:43 +00:00
|
|
|
input_languages = languages.split(',')
|
2020-04-20 16:56:39 +00:00
|
|
|
unknown_languages = []
|
2020-09-20 09:41:44 +00:00
|
|
|
if not upload:
|
|
|
|
input_l = isoLanguages.get_language_codes(get_locale(), input_languages, unknown_languages)
|
|
|
|
else:
|
|
|
|
input_l = isoLanguages.get_valid_language_codes(get_locale(), input_languages, unknown_languages)
|
2020-04-20 16:56:39 +00:00
|
|
|
for l in unknown_languages:
|
|
|
|
log.error('%s is not a valid language', l)
|
2021-04-03 12:21:38 +00:00
|
|
|
if isinstance(invalid, list):
|
|
|
|
invalid.append(l)
|
|
|
|
else:
|
|
|
|
flash(_(u"%(langname)s is not a valid language", langname=l), category="warning")
|
2020-05-24 18:19:43 +00:00
|
|
|
# ToDo: Not working correct
|
|
|
|
if upload and len(input_l) == 1:
|
|
|
|
# If the language of the file is excluded from the users view, it's not imported, to allow the user to view
|
|
|
|
# the book it's language is set to the filter language
|
|
|
|
if input_l[0] != current_user.filter_language() and current_user.filter_language() != "all":
|
|
|
|
input_l[0] = calibre_db.session.query(db.Languages). \
|
2020-11-07 10:44:02 +00:00
|
|
|
filter(db.Languages.lang_code == current_user.filter_language()).first().lang_code
|
2020-06-22 17:11:03 +00:00
|
|
|
# Remove duplicates
|
|
|
|
input_l = helper.uniq(input_l)
|
2020-05-24 18:19:43 +00:00
|
|
|
return modify_database_object(input_l, book.languages, db.Languages, calibre_db.session, 'languages')
|
2020-04-20 16:56:39 +00:00
|
|
|
|
|
|
|
|
2021-03-17 18:06:51 +00:00
|
|
|
def edit_book_publisher(publishers, book):
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = False
|
2021-03-17 18:06:51 +00:00
|
|
|
if publishers:
|
|
|
|
publisher = publishers.rstrip().strip()
|
2020-04-20 16:56:39 +00:00
|
|
|
if len(book.publishers) == 0 or (len(book.publishers) > 0 and publisher != book.publishers[0].name):
|
2020-08-23 08:53:18 +00:00
|
|
|
changed |= modify_database_object([publisher], book.publishers, db.Publishers, calibre_db.session,
|
|
|
|
'publisher')
|
2020-04-20 16:56:39 +00:00
|
|
|
elif len(book.publishers):
|
2020-05-21 16:16:11 +00:00
|
|
|
changed |= modify_database_object([], book.publishers, db.Publishers, calibre_db.session, 'publisher')
|
2020-04-20 16:56:39 +00:00
|
|
|
return changed
|
|
|
|
|
|
|
|
|
2021-05-13 12:00:01 +00:00
|
|
|
def edit_cc_data_value(book_id, book, c, to_save, cc_db_value, cc_string):
|
2021-03-15 08:55:59 +00:00
|
|
|
changed = False
|
|
|
|
if to_save[cc_string] == 'None':
|
|
|
|
to_save[cc_string] = None
|
|
|
|
elif c.datatype == 'bool':
|
|
|
|
to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0
|
2021-05-13 08:39:36 +00:00
|
|
|
elif c.datatype == 'datetime':
|
|
|
|
try:
|
|
|
|
to_save[cc_string] = datetime.strptime(to_save[cc_string], "%Y-%m-%d")
|
|
|
|
except ValueError:
|
|
|
|
to_save[cc_string] = db.Books.DEFAULT_PUBDATE
|
2021-03-15 08:55:59 +00:00
|
|
|
|
|
|
|
if to_save[cc_string] != cc_db_value:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
if to_save[cc_string] is not None:
|
|
|
|
setattr(getattr(book, cc_string)[0], 'value', to_save[cc_string])
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
calibre_db.session.delete(del_cc)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
cc_class = db.cc_classes[c.id]
|
|
|
|
new_cc = cc_class(value=to_save[cc_string], book=book_id)
|
|
|
|
calibre_db.session.add(new_cc)
|
|
|
|
changed = True
|
|
|
|
return changed, to_save
|
|
|
|
|
|
|
|
|
|
|
|
def edit_cc_data_string(book, c, to_save, cc_db_value, cc_string):
|
|
|
|
changed = False
|
|
|
|
if c.datatype == 'rating':
|
|
|
|
to_save[cc_string] = str(int(float(to_save[cc_string]) * 2))
|
|
|
|
if to_save[cc_string].strip() != cc_db_value:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
# remove old cc_val
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
if len(del_cc.books) == 0:
|
|
|
|
calibre_db.session.delete(del_cc)
|
|
|
|
changed = True
|
|
|
|
cc_class = db.cc_classes[c.id]
|
|
|
|
new_cc = calibre_db.session.query(cc_class).filter(
|
|
|
|
cc_class.value == to_save[cc_string].strip()).first()
|
|
|
|
# if no cc val is found add it
|
|
|
|
if new_cc is None:
|
|
|
|
new_cc = cc_class(value=to_save[cc_string].strip())
|
|
|
|
calibre_db.session.add(new_cc)
|
|
|
|
changed = True
|
|
|
|
calibre_db.session.flush()
|
|
|
|
new_cc = calibre_db.session.query(cc_class).filter(
|
|
|
|
cc_class.value == to_save[cc_string].strip()).first()
|
|
|
|
# add cc value to book
|
|
|
|
getattr(book, cc_string).append(new_cc)
|
|
|
|
return changed, to_save
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def edit_cc_data(book_id, book, to_save):
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = False
|
2020-05-21 16:16:11 +00:00
|
|
|
cc = calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
for c in cc:
|
|
|
|
cc_string = "custom_column_" + str(c.id)
|
|
|
|
if not c.is_multiple:
|
|
|
|
if len(getattr(book, cc_string)) > 0:
|
|
|
|
cc_db_value = getattr(book, cc_string)[0].value
|
|
|
|
else:
|
|
|
|
cc_db_value = None
|
|
|
|
if to_save[cc_string].strip():
|
2021-05-13 12:00:01 +00:00
|
|
|
if c.datatype in ['int', 'bool', 'float', "datetime", "comments"]:
|
|
|
|
changed, to_save = edit_cc_data_value(book_id, book, c, to_save, cc_db_value, cc_string)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2021-03-15 08:55:59 +00:00
|
|
|
changed, to_save = edit_cc_data_string(book, c, to_save, cc_db_value, cc_string)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
# remove old cc_val
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
2019-07-17 17:02:53 +00:00
|
|
|
if not del_cc.books or len(del_cc.books) == 0:
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.delete(del_cc)
|
2020-04-20 16:56:39 +00:00
|
|
|
changed = True
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
input_tags = to_save[cc_string].split(',')
|
|
|
|
input_tags = list(map(lambda it: it.strip(), input_tags))
|
2020-05-24 18:54:23 +00:00
|
|
|
changed |= modify_database_object(input_tags,
|
|
|
|
getattr(book, cc_string),
|
|
|
|
db.cc_classes[c.id],
|
|
|
|
calibre_db.session,
|
|
|
|
'custom')
|
2020-04-20 16:56:39 +00:00
|
|
|
return changed
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def upload_single_file(request, book, book_id):
|
|
|
|
# Check and handle Uploaded file
|
|
|
|
if 'btn-upload-format' in request.files:
|
|
|
|
requested_file = request.files['btn-upload-format']
|
|
|
|
# check for empty request
|
|
|
|
if requested_file.filename != '':
|
2020-09-26 05:54:38 +00:00
|
|
|
if not current_user.role_upload():
|
|
|
|
abort(403)
|
2019-07-13 18:45:48 +00:00
|
|
|
if '.' in requested_file.filename:
|
|
|
|
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
2020-09-08 18:57:39 +00:00
|
|
|
if file_ext not in constants.EXTENSIONS_UPLOAD and '' not in constants.EXTENSIONS_UPLOAD:
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_("File extension '%(ext)s' is not allowed to be uploaded to this server", ext=file_ext),
|
|
|
|
category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
else:
|
|
|
|
flash(_('File to be uploaded must have an extension'), category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
|
|
|
|
file_name = book.path.rsplit('/', 1)[-1]
|
|
|
|
filepath = os.path.normpath(os.path.join(config.config_calibre_dir, book.path))
|
|
|
|
saved_filename = os.path.join(filepath, file_name + '.' + file_ext)
|
|
|
|
|
|
|
|
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
try:
|
|
|
|
os.makedirs(filepath)
|
|
|
|
except OSError:
|
|
|
|
flash(_(u"Failed to create path %(path)s (Permission denied).", path=filepath), category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
try:
|
|
|
|
requested_file.save(saved_filename)
|
|
|
|
except OSError:
|
|
|
|
flash(_(u"Failed to store file %(file)s.", file=saved_filename), category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
|
|
|
|
file_size = os.path.getsize(saved_filename)
|
2020-05-23 08:16:29 +00:00
|
|
|
is_format = calibre_db.get_book_format(book_id, file_ext.upper())
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Format entry already exists, no need to update the database
|
|
|
|
if is_format:
|
|
|
|
log.warning('Book format %s already existing', file_ext.upper())
|
|
|
|
else:
|
2020-05-06 16:47:33 +00:00
|
|
|
try:
|
|
|
|
db_format = db.Data(book_id, file_ext.upper(), file_size, file_name)
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.add(db_format)
|
|
|
|
calibre_db.session.commit()
|
|
|
|
calibre_db.update_title_sort(config)
|
2021-01-10 09:23:14 +00:00
|
|
|
except (OperationalError, IntegrityError) as e:
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.rollback()
|
2020-05-06 16:47:33 +00:00
|
|
|
log.error('Database error: %s', e)
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e), category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Queue uploader info
|
|
|
|
uploadText=_(u"File format %(ext)s added to %(book)s", ext=file_ext.upper(), book=book.title)
|
2021-03-21 17:55:02 +00:00
|
|
|
WorkerThread.add(current_user.name, TaskUpload(
|
2020-08-23 02:44:28 +00:00
|
|
|
"<a href=\"" + url_for('web.show_book', book_id=book.id) + "\">" + uploadText + "</a>"))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-09-26 07:42:40 +00:00
|
|
|
return uploader.process(
|
|
|
|
saved_filename, *os.path.splitext(requested_file.filename),
|
|
|
|
rarExecutable=config.config_rarfile_location)
|
2019-12-06 14:00:01 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def upload_cover(request, book):
|
|
|
|
if 'btn-upload-cover' in request.files:
|
|
|
|
requested_file = request.files['btn-upload-cover']
|
|
|
|
# check for empty request
|
|
|
|
if requested_file.filename != '':
|
2020-09-23 18:50:34 +00:00
|
|
|
if not current_user.role_upload():
|
|
|
|
abort(403)
|
2020-03-07 10:07:35 +00:00
|
|
|
ret, message = helper.save_cover(requested_file, book.path)
|
|
|
|
if ret is True:
|
2019-07-13 18:45:48 +00:00
|
|
|
return True
|
|
|
|
else:
|
2020-03-07 10:07:35 +00:00
|
|
|
flash(message, category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return False
|
|
|
|
return None
|
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
|
|
|
|
def handle_title_on_edit(book, book_title):
|
2021-03-21 13:17:07 +00:00
|
|
|
# handle book title
|
2021-04-03 12:21:38 +00:00
|
|
|
book_title = book_title.rstrip().strip()
|
|
|
|
if book.title != book_title:
|
|
|
|
if book_title == '':
|
|
|
|
book_title = _(u'Unknown')
|
|
|
|
book.title = book_title
|
2021-03-21 13:17:07 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
|
|
|
|
def handle_author_on_edit(book, author_name, update_stored=True):
|
2021-03-21 13:17:07 +00:00
|
|
|
# handle author(s)
|
2021-04-03 12:21:38 +00:00
|
|
|
input_authors = author_name.split('&')
|
2021-03-21 13:17:07 +00:00
|
|
|
input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
|
|
|
|
# Remove duplicates in authors list
|
|
|
|
input_authors = helper.uniq(input_authors)
|
|
|
|
# we have all author names now
|
|
|
|
if input_authors == ['']:
|
|
|
|
input_authors = [_(u'Unknown')] # prevent empty Author
|
|
|
|
|
|
|
|
change = modify_database_object(input_authors, book.authors, db.Authors, calibre_db.session, 'author')
|
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
# Search for each author if author is in database, if not, author name and sorted author name is generated new
|
2021-03-21 13:17:07 +00:00
|
|
|
# everything then is assembled for sorted author field in database
|
|
|
|
sort_authors_list = list()
|
|
|
|
for inp in input_authors:
|
|
|
|
stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
|
|
|
|
if not stored_author:
|
|
|
|
stored_author = helper.get_sorted_author(inp)
|
|
|
|
else:
|
|
|
|
stored_author = stored_author.sort
|
|
|
|
sort_authors_list.append(helper.get_sorted_author(stored_author))
|
|
|
|
sort_authors = ' & '.join(sort_authors_list)
|
2021-04-03 12:21:38 +00:00
|
|
|
if book.author_sort != sort_authors and update_stored:
|
2021-03-21 13:17:07 +00:00
|
|
|
book.author_sort = sort_authors
|
|
|
|
change = True
|
|
|
|
return input_authors, change
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
@editbook.route("/admin/book/<int:book_id>", methods=['GET', 'POST'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
@edit_required
|
|
|
|
def edit_book(book_id):
|
2020-04-20 16:56:39 +00:00
|
|
|
modif_date = False
|
2021-01-10 14:02:04 +00:00
|
|
|
|
|
|
|
# create the function for sorting...
|
|
|
|
try:
|
|
|
|
calibre_db.update_title_sort(config)
|
|
|
|
except sqliteOperationalError as e:
|
|
|
|
log.debug_or_exception(e)
|
|
|
|
calibre_db.session.rollback()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# Show form
|
|
|
|
if request.method != 'POST':
|
|
|
|
return render_edit_book(book_id)
|
|
|
|
|
2020-12-04 18:23:36 +00:00
|
|
|
book = calibre_db.get_filtered_book(book_id, allow_show_archived=True)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Book not found
|
|
|
|
if not book:
|
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible"), category="error")
|
|
|
|
return redirect(url_for("web.index"))
|
|
|
|
|
2019-12-06 14:00:01 +00:00
|
|
|
meta = upload_single_file(request, book, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
if upload_cover(request, book) is True:
|
|
|
|
book.has_cover = 1
|
2020-04-20 16:56:39 +00:00
|
|
|
modif_date = True
|
2019-07-13 18:45:48 +00:00
|
|
|
try:
|
|
|
|
to_save = request.form.to_dict()
|
2019-12-06 14:00:01 +00:00
|
|
|
merge_metadata(to_save, meta)
|
2019-07-13 18:45:48 +00:00
|
|
|
# Update book
|
|
|
|
edited_books_id = None
|
2020-09-07 19:26:59 +00:00
|
|
|
|
2021-03-21 13:17:07 +00:00
|
|
|
# handle book title
|
2021-04-04 17:40:34 +00:00
|
|
|
title_change = handle_title_on_edit(book, to_save["book_title"])
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
input_authors, authorchange = handle_author_on_edit(book, to_save["author_name"])
|
|
|
|
if authorchange or title_change:
|
2019-07-13 18:45:48 +00:00
|
|
|
edited_books_id = book.id
|
2020-04-20 16:56:39 +00:00
|
|
|
modif_date = True
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
|
|
|
|
|
|
|
error = False
|
|
|
|
if edited_books_id:
|
|
|
|
error = helper.update_dir_stucture(edited_books_id, config.config_calibre_dir, input_authors[0])
|
|
|
|
|
|
|
|
if not error:
|
2020-09-24 08:54:02 +00:00
|
|
|
if "cover_url" in to_save:
|
2020-09-24 09:02:12 +00:00
|
|
|
if to_save["cover_url"]:
|
|
|
|
if not current_user.role_upload():
|
|
|
|
return "", (403)
|
2020-11-20 19:35:07 +00:00
|
|
|
if to_save["cover_url"].endswith('/static/generic_cover.jpg'):
|
|
|
|
book.has_cover = 0
|
2020-09-24 09:02:12 +00:00
|
|
|
else:
|
2020-11-20 19:35:07 +00:00
|
|
|
result, error = helper.save_cover_from_url(to_save["cover_url"], book.path)
|
|
|
|
if result is True:
|
|
|
|
book.has_cover = 1
|
|
|
|
modif_date = True
|
|
|
|
else:
|
|
|
|
flash(error, category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-24 18:19:43 +00:00
|
|
|
# Add default series_index to book
|
|
|
|
modif_date |= edit_book_series_index(to_save["series_index"], book)
|
2019-07-13 18:45:48 +00:00
|
|
|
# Handle book comments/description
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date |= edit_book_comments(to_save["description"], book)
|
2020-09-05 16:46:11 +00:00
|
|
|
# Handle identifiers
|
2020-01-12 22:23:43 +00:00
|
|
|
input_identifiers = identifier_list(to_save, book)
|
2020-09-05 16:46:11 +00:00
|
|
|
modification, warning = modify_identifiers(input_identifiers, book.identifiers, calibre_db.session)
|
|
|
|
if warning:
|
|
|
|
flash(_("Identifiers are not Case Sensitive, Overwriting Old Identifier"), category="warning")
|
|
|
|
modif_date |= modification
|
2019-07-13 18:45:48 +00:00
|
|
|
# Handle book tags
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date |= edit_book_tags(to_save['tags'], book)
|
2019-07-13 18:45:48 +00:00
|
|
|
# Handle book series
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date |= edit_book_series(to_save["series"], book)
|
2020-04-20 16:56:39 +00:00
|
|
|
# handle book publisher
|
2021-03-17 18:06:51 +00:00
|
|
|
modif_date |= edit_book_publisher(to_save['publisher'], book)
|
2019-07-13 18:45:48 +00:00
|
|
|
# handle book languages
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date |= edit_book_languages(to_save['languages'], book)
|
2019-07-13 18:45:48 +00:00
|
|
|
# handle book ratings
|
2020-04-20 16:56:39 +00:00
|
|
|
modif_date |= edit_book_ratings(to_save, book)
|
2019-07-13 18:45:48 +00:00
|
|
|
# handle cc data
|
2020-04-20 16:56:39 +00:00
|
|
|
modif_date |= edit_cc_data(book_id, book, to_save)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-03-21 13:17:07 +00:00
|
|
|
if to_save["pubdate"]:
|
|
|
|
try:
|
|
|
|
book.pubdate = datetime.strptime(to_save["pubdate"], "%Y-%m-%d")
|
|
|
|
except ValueError:
|
|
|
|
book.pubdate = db.Books.DEFAULT_PUBDATE
|
|
|
|
else:
|
|
|
|
book.pubdate = db.Books.DEFAULT_PUBDATE
|
|
|
|
|
2020-04-20 16:56:39 +00:00
|
|
|
if modif_date:
|
|
|
|
book.last_modified = datetime.utcnow()
|
2020-09-05 16:23:14 +00:00
|
|
|
calibre_db.session.merge(book)
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.commit()
|
2019-07-13 18:45:48 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
|
|
|
if "detail_view" in to_save:
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
else:
|
|
|
|
flash(_("Metadata successfully updated"), category="success")
|
|
|
|
return render_edit_book(book_id)
|
|
|
|
else:
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(error, category="error")
|
|
|
|
return render_edit_book(book_id)
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.debug_or_exception(ex)
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_("Error editing book, please check logfile for details"), category="error")
|
|
|
|
return redirect(url_for('web.show_book', book_id=book.id))
|
|
|
|
|
|
|
|
|
2019-12-06 14:00:01 +00:00
|
|
|
def merge_metadata(to_save, meta):
|
2019-12-09 19:53:16 +00:00
|
|
|
if to_save['author_name'] == _(u'Unknown'):
|
2019-12-06 14:00:01 +00:00
|
|
|
to_save['author_name'] = ''
|
2019-12-09 19:53:16 +00:00
|
|
|
if to_save['book_title'] == _(u'Unknown'):
|
2019-12-06 14:00:01 +00:00
|
|
|
to_save['book_title'] = ''
|
|
|
|
for s_field, m_field in [
|
|
|
|
('tags', 'tags'), ('author_name', 'author'), ('series', 'series'),
|
|
|
|
('series_index', 'series_id'), ('languages', 'languages'),
|
|
|
|
('book_title', 'title')]:
|
|
|
|
to_save[s_field] = to_save[s_field] or getattr(meta, m_field, '')
|
|
|
|
to_save["description"] = to_save["description"] or Markup(
|
|
|
|
getattr(meta, 'description', '')).unescape()
|
|
|
|
|
2021-03-15 08:55:59 +00:00
|
|
|
|
2020-01-12 22:23:43 +00:00
|
|
|
def identifier_list(to_save, book):
|
|
|
|
"""Generate a list of Identifiers from form information"""
|
|
|
|
id_type_prefix = 'identifier-type-'
|
|
|
|
id_val_prefix = 'identifier-val-'
|
|
|
|
result = []
|
|
|
|
for type_key, type_value in to_save.items():
|
|
|
|
if not type_key.startswith(id_type_prefix):
|
|
|
|
continue
|
|
|
|
val_key = id_val_prefix + type_key[len(id_type_prefix):]
|
|
|
|
if val_key not in to_save.keys():
|
|
|
|
continue
|
2020-09-05 16:46:11 +00:00
|
|
|
result.append(db.Identifiers(to_save[val_key], type_value, book.id))
|
2020-01-12 22:23:43 +00:00
|
|
|
return result
|
2019-12-06 14:00:01 +00:00
|
|
|
|
2021-03-15 08:55:59 +00:00
|
|
|
|
|
|
|
def prepare_authors_on_upload(title, authr):
|
|
|
|
if title != _(u'Unknown') and authr != _(u'Unknown'):
|
|
|
|
entry = calibre_db.check_exists_book(authr, title)
|
|
|
|
if entry:
|
|
|
|
log.info("Uploaded book probably exists in library")
|
|
|
|
flash(_(u"Uploaded book probably exists in the library, consider to change before upload new: ")
|
|
|
|
+ Markup(render_title_template('book_exists_flash.html', entry=entry)), category="warning")
|
|
|
|
|
|
|
|
# handle authors
|
|
|
|
input_authors = authr.split('&')
|
|
|
|
# handle_authors(input_authors)
|
|
|
|
input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
|
|
|
|
# Remove duplicates in authors list
|
|
|
|
input_authors = helper.uniq(input_authors)
|
|
|
|
|
|
|
|
# we have all author names now
|
|
|
|
if input_authors == ['']:
|
|
|
|
input_authors = [_(u'Unknown')] # prevent empty Author
|
|
|
|
|
|
|
|
sort_authors_list = list()
|
|
|
|
db_author = None
|
|
|
|
for inp in input_authors:
|
|
|
|
stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
|
|
|
|
if not stored_author:
|
|
|
|
if not db_author:
|
|
|
|
db_author = db.Authors(inp, helper.get_sorted_author(inp), "")
|
|
|
|
calibre_db.session.add(db_author)
|
|
|
|
calibre_db.session.commit()
|
|
|
|
sort_author = helper.get_sorted_author(inp)
|
|
|
|
else:
|
|
|
|
if not db_author:
|
|
|
|
db_author = stored_author
|
|
|
|
sort_author = stored_author.sort
|
|
|
|
sort_authors_list.append(sort_author)
|
|
|
|
sort_authors = ' & '.join(sort_authors_list)
|
|
|
|
return sort_authors, input_authors, db_author
|
|
|
|
|
|
|
|
|
|
|
|
def create_book_on_upload(modif_date, meta):
|
|
|
|
title = meta.title
|
|
|
|
authr = meta.author
|
|
|
|
sort_authors, input_authors, db_author = prepare_authors_on_upload(title, authr)
|
|
|
|
|
|
|
|
title_dir = helper.get_valid_filename(title)
|
|
|
|
author_dir = helper.get_valid_filename(db_author.name)
|
|
|
|
|
|
|
|
# combine path and normalize path from windows systems
|
|
|
|
path = os.path.join(author_dir, title_dir).replace('\\', '/')
|
|
|
|
|
|
|
|
# Calibre adds books with utc as timezone
|
|
|
|
db_book = db.Books(title, "", sort_authors, datetime.utcnow(), datetime(101, 1, 1),
|
|
|
|
'1', datetime.utcnow(), path, meta.cover, db_author, [], "")
|
|
|
|
|
|
|
|
modif_date |= modify_database_object(input_authors, db_book.authors, db.Authors, calibre_db.session,
|
|
|
|
'author')
|
|
|
|
|
|
|
|
# Add series_index to book
|
|
|
|
modif_date |= edit_book_series_index(meta.series_id, db_book)
|
|
|
|
|
|
|
|
# add languages
|
|
|
|
modif_date |= edit_book_languages(meta.languages, db_book, upload=True)
|
|
|
|
|
|
|
|
# handle tags
|
|
|
|
modif_date |= edit_book_tags(meta.tags, db_book)
|
|
|
|
|
2021-03-17 18:06:51 +00:00
|
|
|
# handle publisher
|
|
|
|
modif_date |= edit_book_publisher(meta.publisher, db_book)
|
|
|
|
|
2021-03-15 08:55:59 +00:00
|
|
|
# handle series
|
|
|
|
modif_date |= edit_book_series(meta.series, db_book)
|
|
|
|
|
|
|
|
# Add file to book
|
|
|
|
file_size = os.path.getsize(meta.file_path)
|
|
|
|
db_data = db.Data(db_book, meta.extension.upper()[1:], file_size, title_dir)
|
|
|
|
db_book.data.append(db_data)
|
|
|
|
calibre_db.session.add(db_book)
|
|
|
|
|
|
|
|
# flush content, get db_book.id available
|
|
|
|
calibre_db.session.flush()
|
|
|
|
return db_book, input_authors, title_dir
|
|
|
|
|
2021-03-21 07:19:54 +00:00
|
|
|
def file_handling_on_upload(requested_file):
|
|
|
|
# check if file extension is correct
|
|
|
|
if '.' in requested_file.filename:
|
|
|
|
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
|
|
|
if file_ext not in constants.EXTENSIONS_UPLOAD and '' not in constants.EXTENSIONS_UPLOAD:
|
|
|
|
flash(
|
|
|
|
_("File extension '%(ext)s' is not allowed to be uploaded to this server",
|
|
|
|
ext=file_ext), category="error")
|
|
|
|
return None, Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
|
|
|
|
else:
|
|
|
|
flash(_('File to be uploaded must have an extension'), category="error")
|
|
|
|
return None, Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
|
|
|
|
|
|
|
|
# extract metadata from file
|
|
|
|
try:
|
|
|
|
meta = uploader.upload(requested_file, config.config_rarfile_location)
|
|
|
|
except (IOError, OSError):
|
|
|
|
log.error("File %s could not saved to temp dir", requested_file.filename)
|
|
|
|
flash(_(u"File %(filename)s could not saved to temp dir",
|
|
|
|
filename=requested_file.filename), category="error")
|
|
|
|
return None, Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
|
|
|
|
return meta, None
|
|
|
|
|
|
|
|
|
|
|
|
def move_coverfile(meta, db_book):
|
|
|
|
# move cover to final directory, including book id
|
|
|
|
if meta.cover:
|
|
|
|
coverfile = meta.cover
|
|
|
|
else:
|
|
|
|
coverfile = os.path.join(constants.STATIC_DIR, 'generic_cover.jpg')
|
|
|
|
new_coverpath = os.path.join(config.config_calibre_dir, db_book.path, "cover.jpg")
|
|
|
|
try:
|
|
|
|
copyfile(coverfile, new_coverpath)
|
|
|
|
if meta.cover:
|
|
|
|
os.unlink(meta.cover)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Failed to move cover file %s: %s", new_coverpath, e)
|
|
|
|
flash(_(u"Failed to Move Cover File %(file)s: %(error)s", file=new_coverpath,
|
|
|
|
error=e),
|
|
|
|
category="error")
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@editbook.route("/upload", methods=["GET", "POST"])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
@upload_required
|
|
|
|
def upload():
|
|
|
|
if not config.config_uploading:
|
|
|
|
abort(404)
|
|
|
|
if request.method == 'POST' and 'btn-upload' in request.files:
|
|
|
|
for requested_file in request.files.getlist("btn-upload"):
|
2019-12-08 08:40:54 +00:00
|
|
|
try:
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date = False
|
2020-05-06 16:47:33 +00:00
|
|
|
# create the function for sorting...
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.update_title_sort(config)
|
|
|
|
calibre_db.session.connection().connection.connection.create_function('uuid4', 0, lambda: str(uuid4()))
|
2020-05-06 16:47:33 +00:00
|
|
|
|
2021-03-23 16:57:49 +00:00
|
|
|
meta, error = file_handling_on_upload(requested_file)
|
2021-03-21 07:19:54 +00:00
|
|
|
if error:
|
2021-03-23 16:57:49 +00:00
|
|
|
return error
|
2020-05-24 18:54:23 +00:00
|
|
|
|
2021-03-15 08:55:59 +00:00
|
|
|
db_book, input_authors, title_dir = create_book_on_upload(modif_date, meta)
|
2020-05-24 18:19:43 +00:00
|
|
|
|
2021-03-21 07:19:54 +00:00
|
|
|
# Comments needs book id therefore only possible after flush
|
2020-05-24 18:19:43 +00:00
|
|
|
modif_date |= edit_book_comments(Markup(meta.description).unescape(), db_book)
|
|
|
|
|
2020-05-06 16:47:33 +00:00
|
|
|
book_id = db_book.id
|
2020-05-24 18:40:58 +00:00
|
|
|
title = db_book.title
|
2020-05-06 16:47:33 +00:00
|
|
|
|
2020-09-12 10:11:33 +00:00
|
|
|
error = helper.update_dir_structure_file(book_id,
|
2020-09-07 19:26:59 +00:00
|
|
|
config.config_calibre_dir,
|
|
|
|
input_authors[0],
|
|
|
|
meta.file_path,
|
|
|
|
title_dir + meta.extension)
|
2020-05-06 16:47:33 +00:00
|
|
|
|
2021-03-21 07:19:54 +00:00
|
|
|
move_coverfile(meta, db_book)
|
2020-05-24 18:19:43 +00:00
|
|
|
|
|
|
|
# save data to database, reread data
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.commit()
|
2020-09-12 10:11:33 +00:00
|
|
|
|
2020-05-06 16:47:33 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
|
|
|
if error:
|
|
|
|
flash(error, category="error")
|
2020-05-24 18:40:58 +00:00
|
|
|
uploadText=_(u"File %(file)s uploaded", file=title)
|
2021-03-21 17:55:02 +00:00
|
|
|
WorkerThread.add(current_user.name, TaskUpload(
|
2020-08-23 02:44:28 +00:00
|
|
|
"<a href=\"" + url_for('web.show_book', book_id=book_id) + "\">" + uploadText + "</a>"))
|
2020-05-06 16:47:33 +00:00
|
|
|
|
|
|
|
if len(request.files.getlist("btn-upload")) < 2:
|
|
|
|
if current_user.role_edit() or current_user.role_admin():
|
2020-05-24 18:40:58 +00:00
|
|
|
resp = {"location": url_for('editbook.edit_book', book_id=book_id)}
|
2020-05-06 16:47:33 +00:00
|
|
|
return Response(json.dumps(resp), mimetype='application/json')
|
|
|
|
else:
|
2020-05-24 18:40:58 +00:00
|
|
|
resp = {"location": url_for('web.show_book', book_id=book_id)}
|
2020-05-06 16:47:33 +00:00
|
|
|
return Response(json.dumps(resp), mimetype='application/json')
|
2021-01-10 09:23:14 +00:00
|
|
|
except (OperationalError, IntegrityError) as e:
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.session.rollback()
|
2020-05-06 16:47:33 +00:00
|
|
|
log.error("Database error: %s", e)
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e), category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
|
|
|
|
|
|
|
|
@editbook.route("/admin/book/convert/<int:book_id>", methods=['POST'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
@edit_required
|
|
|
|
def convert_bookformat(book_id):
|
|
|
|
# check to see if we have form fields to work with - if not send user back
|
|
|
|
book_format_from = request.form.get('book_format_from', None)
|
|
|
|
book_format_to = request.form.get('book_format_to', None)
|
|
|
|
|
|
|
|
if (book_format_from is None) or (book_format_to is None):
|
|
|
|
flash(_(u"Source or destination format for conversion missing"), category="error")
|
2019-12-15 17:44:02 +00:00
|
|
|
return redirect(url_for('editbook.edit_book', book_id=book_id))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
log.info('converting: book id: %s from: %s to: %s', book_id, book_format_from, book_format_to)
|
|
|
|
rtn = helper.convert_book_format(book_id, config.config_calibre_dir, book_format_from.upper(),
|
2021-03-21 17:55:02 +00:00
|
|
|
book_format_to.upper(), current_user.name)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
if rtn is None:
|
|
|
|
flash(_(u"Book successfully queued for converting to %(book_format)s",
|
|
|
|
book_format=book_format_to),
|
|
|
|
category="success")
|
|
|
|
else:
|
|
|
|
flash(_(u"There was an error converting this book: %(res)s", res=rtn), category="error")
|
2019-12-15 17:44:02 +00:00
|
|
|
return redirect(url_for('editbook.edit_book', book_id=book_id))
|
2020-06-11 06:48:23 +00:00
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
|
2020-06-11 06:48:23 +00:00
|
|
|
@editbook.route("/ajax/editbooks/<param>", methods=['POST'])
|
|
|
|
@login_required_if_no_ano
|
2020-10-29 13:52:20 +00:00
|
|
|
@edit_required
|
2020-06-11 06:48:23 +00:00
|
|
|
def edit_list_book(param):
|
|
|
|
vals = request.form.to_dict()
|
|
|
|
book = calibre_db.get_book(vals['pk'])
|
2021-04-02 14:41:34 +00:00
|
|
|
ret = ""
|
2020-06-11 06:48:23 +00:00
|
|
|
if param =='series_index':
|
|
|
|
edit_book_series_index(vals['value'], book)
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': book.series_index}), mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='tags':
|
|
|
|
edit_book_tags(vals['value'], book)
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': ', '.join([tag.name for tag in book.tags])}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='series':
|
|
|
|
edit_book_series(vals['value'], book)
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': ', '.join([serie.name for serie in book.series])}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='publishers':
|
2021-04-03 12:21:38 +00:00
|
|
|
edit_book_publisher(vals['value'], book)
|
|
|
|
ret = Response(json.dumps({'success': True,
|
|
|
|
'newValue': ', '.join([publisher.name for publisher in book.publishers])}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='languages':
|
2021-04-03 12:21:38 +00:00
|
|
|
invalid = list()
|
|
|
|
edit_book_languages(vals['value'], book, invalid=invalid)
|
|
|
|
if invalid:
|
|
|
|
ret = Response(json.dumps({'success': False,
|
|
|
|
'msg': 'Invalid languages in request: {}'.format(','.join(invalid))}),
|
|
|
|
mimetype='application/json')
|
|
|
|
else:
|
|
|
|
lang_names = list()
|
|
|
|
for lang in book.languages:
|
|
|
|
try:
|
|
|
|
lang_names.append(LC.parse(lang.lang_code).get_language_name(get_locale()))
|
|
|
|
except UnknownLocaleError:
|
|
|
|
lang_names.append(_(isoLanguages.get(part3=lang.lang_code).name))
|
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': ', '.join(lang_names)}),
|
|
|
|
mimetype='application/json')
|
2020-06-11 19:19:09 +00:00
|
|
|
elif param =='author_sort':
|
|
|
|
book.author_sort = vals['value']
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': book.author_sort}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2021-04-03 12:21:38 +00:00
|
|
|
elif param == 'title':
|
|
|
|
sort = book.sort
|
|
|
|
handle_title_on_edit(book, vals.get('value', ""))
|
2020-06-12 11:45:07 +00:00
|
|
|
helper.update_dir_stucture(book.id, config.config_calibre_dir)
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': book.title}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='sort':
|
2020-06-11 19:19:09 +00:00
|
|
|
book.sort = vals['value']
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True, 'newValue': book.sort}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
elif param =='authors':
|
2021-04-03 12:21:38 +00:00
|
|
|
input_authors, __ = handle_author_on_edit(book, vals['value'], vals.get('checkA', None) == "true")
|
2020-06-12 11:45:07 +00:00
|
|
|
helper.update_dir_stucture(book.id, config.config_calibre_dir, input_authors[0])
|
2021-04-03 12:21:38 +00:00
|
|
|
ret = Response(json.dumps({'success': True,
|
|
|
|
'newValue': ' & '.join([author.replace('|',',') for author in input_authors])}),
|
2021-04-02 14:41:34 +00:00
|
|
|
mimetype='application/json')
|
2020-06-11 06:48:23 +00:00
|
|
|
book.last_modified = datetime.utcnow()
|
|
|
|
calibre_db.session.commit()
|
2021-04-03 12:21:38 +00:00
|
|
|
# revert change for sort if automatic fields link is deactivated
|
|
|
|
if param == 'title' and vals.get('checkT') == "false":
|
|
|
|
book.sort = sort
|
|
|
|
calibre_db.session.commit()
|
2021-04-02 14:41:34 +00:00
|
|
|
return ret
|
2020-06-11 06:48:23 +00:00
|
|
|
|
2021-04-03 12:21:38 +00:00
|
|
|
|
2020-06-12 11:45:07 +00:00
|
|
|
@editbook.route("/ajax/sort_value/<field>/<int:bookid>")
|
2020-06-11 19:19:09 +00:00
|
|
|
@login_required
|
2020-06-12 11:45:07 +00:00
|
|
|
def get_sorted_entry(field, bookid):
|
2021-04-03 12:21:38 +00:00
|
|
|
if field in ['title', 'authors', 'sort', 'author_sort']:
|
2020-06-12 11:45:07 +00:00
|
|
|
book = calibre_db.get_filtered_book(bookid)
|
|
|
|
if book:
|
|
|
|
if field == 'title':
|
|
|
|
return json.dumps({'sort': book.sort})
|
|
|
|
elif field == 'authors':
|
|
|
|
return json.dumps({'author_sort': book.author_sort})
|
2021-04-03 12:21:38 +00:00
|
|
|
if field == 'sort':
|
|
|
|
return json.dumps({'sort': book.title})
|
|
|
|
if field == 'author_sort':
|
|
|
|
return json.dumps({'author_sort': book.author})
|
2020-06-12 14:15:54 +00:00
|
|
|
return ""
|
2020-06-11 19:19:09 +00:00
|
|
|
|
2020-06-18 18:39:45 +00:00
|
|
|
|
2020-08-22 08:27:09 +00:00
|
|
|
@editbook.route("/ajax/simulatemerge", methods=['POST'])
|
|
|
|
@login_required
|
2020-10-31 19:04:12 +00:00
|
|
|
@edit_required
|
2020-08-22 08:27:09 +00:00
|
|
|
def simulate_merge_list_book():
|
|
|
|
vals = request.get_json().get('Merge_books')
|
|
|
|
if vals:
|
|
|
|
to_book = calibre_db.get_book(vals[0]).title
|
|
|
|
vals.pop(0)
|
|
|
|
if to_book:
|
|
|
|
for book_id in vals:
|
|
|
|
from_book = []
|
|
|
|
from_book.append(calibre_db.get_book(book_id).title)
|
|
|
|
return json.dumps({'to': to_book, 'from': from_book})
|
|
|
|
return ""
|
|
|
|
|
2020-06-11 19:19:09 +00:00
|
|
|
|
|
|
|
@editbook.route("/ajax/mergebooks", methods=['POST'])
|
|
|
|
@login_required
|
2020-10-31 19:04:12 +00:00
|
|
|
@edit_required
|
2020-06-11 19:19:09 +00:00
|
|
|
def merge_list_book():
|
2020-06-29 18:14:48 +00:00
|
|
|
vals = request.get_json().get('Merge_books')
|
2020-08-22 08:27:09 +00:00
|
|
|
to_file = list()
|
2020-06-29 18:14:48 +00:00
|
|
|
if vals:
|
|
|
|
# load all formats from target book
|
|
|
|
to_book = calibre_db.get_book(vals[0])
|
|
|
|
vals.pop(0)
|
|
|
|
if to_book:
|
|
|
|
for file in to_book.data:
|
|
|
|
to_file.append(file.format)
|
|
|
|
to_name = helper.get_valid_filename(to_book.title) + ' - ' + \
|
|
|
|
helper.get_valid_filename(to_book.authors[0].name)
|
|
|
|
for book_id in vals:
|
|
|
|
from_book = calibre_db.get_book(book_id)
|
|
|
|
if from_book:
|
|
|
|
for element in from_book.data:
|
|
|
|
if element.format not in to_file:
|
|
|
|
# create new data entry with: book_id, book_format, uncompressed_size, name
|
|
|
|
filepath_new = os.path.normpath(os.path.join(config.config_calibre_dir,
|
|
|
|
to_book.path,
|
|
|
|
to_name + "." + element.format.lower()))
|
|
|
|
filepath_old = os.path.normpath(os.path.join(config.config_calibre_dir,
|
|
|
|
from_book.path,
|
|
|
|
element.name + "." + element.format.lower()))
|
|
|
|
copyfile(filepath_old, filepath_new)
|
|
|
|
to_book.data.append(db.Data(to_book.id,
|
|
|
|
element.format,
|
|
|
|
element.uncompressed_size,
|
|
|
|
to_name))
|
2021-04-21 17:23:11 +00:00
|
|
|
delete_book(from_book.id,"", True)
|
2020-08-22 08:27:09 +00:00
|
|
|
return json.dumps({'success': True})
|
2020-06-11 19:19:09 +00:00
|
|
|
return ""
|