From 35f6f4c727c887f8f3607fe3233dbc1980d15020 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 23 Jan 2022 13:11:02 +0100 Subject: [PATCH] Deleted book formats remove book from synced to kobo table updated teststatus --- cps/editbooks.py | 3 +- cps/helper.py | 14 +- cps/kobo_sync_status.py | 13 +- cps/tasks/convert.py | 8 +- cps/ub.py | 15 +- test/Calibre-Web TestSummary_Linux.html | 226 ++++++++++-------------- 6 files changed, 128 insertions(+), 151 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index 8ea00469..f0b8fb41 100755 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -341,7 +341,8 @@ def delete_book_from_table(book_id, book_format, jsonResponse): else: calibre_db.session.query(db.Data).filter(db.Data.book == book.id).\ filter(db.Data.format == book_format).delete() - kobo_sync_status.remove_synced_book(book.id, True) + if book_format.upper() in ['KEPUB', 'EPUB', 'EPUB3']: + kobo_sync_status.remove_synced_book(book.id, True) calibre_db.session.commit() except Exception as ex: log.debug_or_exception(ex) diff --git a/cps/helper.py b/cps/helper.py index 5cfefe50..622af817 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -17,18 +17,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os import io import mimetypes import re import shutil -import time +import socket import unicodedata from datetime import datetime, timedelta from tempfile import gettempdir - +from urllib.parse import urlparse import requests + from babel.dates import format_datetime from babel.units import format_unit from flask import send_from_directory, make_response, redirect, abort, url_for @@ -584,10 +584,16 @@ def get_book_cover_internal(book, use_generic_cover_on_failure): # saves book cover from url def save_cover_from_url(url, book_path): try: + # 127.0.x.x, localhost, [::1], [::ffff:7f00:1] + ip = socket.getaddrinfo(urlparse(url).hostname, 0)[0][4][0] + if ip.startswith("127.") or ip.startswith('::ffff:7f') or ip == "::1": + log.error("Localhost was accessed for cover upload") + return False, _("You are not allowed to access localhost for cover uploads") img = requests.get(url, timeout=(10, 200)) # ToDo: Error Handling img.raise_for_status() return save_cover(img, book_path) - except (requests.exceptions.HTTPError, + except (socket.gaierror, + requests.exceptions.HTTPError, requests.exceptions.ConnectionError, requests.exceptions.Timeout) as ex: log.info(u'Cover Download Error %s', ex) diff --git a/cps/kobo_sync_status.py b/cps/kobo_sync_status.py index 6df0a503..2e99394b 100644 --- a/cps/kobo_sync_status.py +++ b/cps/kobo_sync_status.py @@ -21,6 +21,7 @@ from flask_login import current_user from . import ub import datetime from sqlalchemy.sql.expression import or_, and_, true +from sqlalchemy import exc # Add the current book id to kobo_synced_books table for current user, if entry is already present, # do nothing (safety precaution) @@ -36,14 +37,18 @@ def add_synced_books(book_id): # Select all entries of current book in kobo_synced_books table, which are from current user and delete them -def remove_synced_book(book_id, all=False): +def remove_synced_book(book_id, all=False, session=None): if not all: user = ub.KoboSyncedBooks.user_id == current_user.id else: user = true() - ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.book_id == book_id) \ - .filter(user).delete() - ub.session_commit() + if not session: + ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.book_id == book_id).filter(user).delete() + ub.session_commit() + else: + session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.book_id == book_id).filter(user).delete() + ub.session_commit(sess=session) + def change_archived_books(book_id, state=None, message=None): diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index 59ad6909..596bf95c 100644 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -16,7 +16,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import os import re @@ -31,7 +30,8 @@ from cps import db from cps import logger, config from cps.subproc_wrapper import process_open from flask_babel import gettext as _ -from flask import url_for +from cps.kobo_sync_status import remove_synced_book +from cps.ub import ini from cps.tasks.mail import TaskEmail from cps import gdriveutils @@ -147,6 +147,10 @@ class TaskConvert(CalibreTask): try: local_db.session.merge(new_format) local_db.session.commit() + if self.settings['new_book_format'].upper() in ['KEPUB', 'EPUB', 'EPUB3']: + ub_session = ini() + remove_synced_book(book_id, True, ub_session) + ub_session.close() except SQLAlchemyError as e: local_db.session.rollback() log.error("Database error: %s", e) diff --git a/cps/ub.py b/cps/ub.py index 1d5db58a..27e9ef8f 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -773,6 +773,14 @@ def create_admin_user(session): except Exception: session.rollback() +def ini(): + global app_DB_path + engine = create_engine(u'sqlite:///{0}'.format(app_DB_path), echo=False) + + Session = scoped_session(sessionmaker()) + Session.configure(bind=engine) + return Session() + def init_db(app_db_path): # Open session for database connection @@ -830,12 +838,13 @@ def dispose(): except Exception: pass -def session_commit(success=None): +def session_commit(success=None, sess=None): + s = sess if sess else session try: - session.commit() + s.commit() if success: log.info(success) except (exc.OperationalError, exc.InvalidRequestError) as e: - session.rollback() + s.rollback() log.debug_or_exception(e) return "" diff --git a/test/Calibre-Web TestSummary_Linux.html b/test/Calibre-Web TestSummary_Linux.html index 84bdae18..652248a2 100644 --- a/test/Calibre-Web TestSummary_Linux.html +++ b/test/Calibre-Web TestSummary_Linux.html @@ -37,20 +37,20 @@
-

Start Time: 2022-01-18 21:11:17

+

Start Time: 2022-01-23 05:50:54

-

Stop Time: 2022-01-19 01:03:52

+

Stop Time: 2022-01-23 10:25:22

-

Duration: 3h 12 min

+

Duration: 3h 54 min

@@ -714,15 +714,15 @@ - + TestEditAdditionalBooks - 17 + 18 15 0 - 0 + 1 2 - Detail + Detail @@ -809,7 +809,36 @@ - + + +
TestEditAdditionalBooks - test_upload_cbz_coverformats
+ + +
+ ERROR +
+ + + + + + + + +
TestEditAdditionalBooks - test_upload_edit_role
@@ -818,7 +847,7 @@ - +
TestEditAdditionalBooks - test_upload_metadata_cbr
@@ -827,7 +856,7 @@ - +
TestEditAdditionalBooks - test_upload_metadata_cbt
@@ -836,19 +865,19 @@ - +
TestEditAdditionalBooks - test_writeonly_calibre_database
- SKIP + SKIP
-