From 93e8c5be32e0e912d8e31a1fa92a6a7b46a16947 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 5 Jun 2021 18:41:42 +0200 Subject: [PATCH] Fixes for flask version 2.1 Fixes for compatibility with sqlalchemy 2.0 --- cps/db.py | 2 +- cps/debug_info.py | 25 ++++++++++++++++++------- cps/gdriveutils.py | 3 ++- cps/ub.py | 7 ++++--- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/cps/db.py b/cps/db.py index 6b1d754f..3dd446bc 100644 --- a/cps/db.py +++ b/cps/db.py @@ -584,7 +584,7 @@ class CalibreDB(): if not cc_classes: try: - cc = conn.execute("SELECT id, datatype FROM custom_columns") + cc = conn.execute(text("SELECT id, datatype FROM custom_columns")) cls.setup_db_cc_classes(cc) except OperationalError as e: log.debug_or_exception(e) diff --git a/cps/debug_info.py b/cps/debug_info.py index 8f0cdeee..cfa549f9 100644 --- a/cps/debug_info.py +++ b/cps/debug_info.py @@ -29,7 +29,7 @@ except ImportError: import os -from flask import send_file +from flask import send_file, __version__ from . import logger, config from .about import collect_stats @@ -43,9 +43,15 @@ def assemble_logfiles(file_name): with open(f, 'r') as fd: shutil.copyfileobj(fd, wfd) wfd.seek(0) - return send_file(wfd, - as_attachment=True, - attachment_filename=os.path.basename(file_name)) + if int(__version__.split('.')[0]) < 2: + return send_file(wfd, + as_attachment=True, + attachment_filename=os.path.basename(file_name)) + else: + return send_file(wfd, + as_attachment=True, + download_name=os.path.basename(file_name)) + def send_debug(): file_list = glob.glob(logger.get_logfile(config.config_logfile) + '*') @@ -60,6 +66,11 @@ def send_debug(): for fp in file_list: zf.write(fp, os.path.basename(fp)) memory_zip.seek(0) - return send_file(memory_zip, - as_attachment=True, - attachment_filename="Calibre-Web-debug-pack.zip") + if int(__version__.split('.')[0]) < 2: + return send_file(memory_zip, + as_attachment=True, + attachment_filename="Calibre-Web-debug-pack.zip") + else: + return send_file(memory_zip, + as_attachment=True, + download_name="Calibre-Web-debug-pack.zip") diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 7c8c23b0..c63b0393 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -34,6 +34,7 @@ try: except ImportError: from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.exc import OperationalError, InvalidRequestError +from sqlalchemy.sql.expression import text try: from apiclient import errors @@ -168,7 +169,7 @@ class PermissionAdded(Base): def migrate(): if not engine.dialect.has_table(engine.connect(), "permissions_added"): PermissionAdded.__table__.create(bind = engine) - for sql in session.execute("select sql from sqlite_master where type='table'"): + for sql in session.execute(text("select sql from sqlite_master where type='table'")): if 'CREATE TABLE gdrive_ids' in sql[0]: currUniqueConstraint = 'UNIQUE (gdrive_id)' if currUniqueConstraint in sql[0]: diff --git a/cps/ub.py b/cps/ub.py index 0d060ed1..c334ff59 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -485,11 +485,12 @@ def migrate_registration_table(engine, session): # Remove login capability of user Guest -def migrate_guest_password(engine, session): +def migrate_guest_password(engine): try: with engine.connect() as conn: + trans = conn.begin() conn.execute(text("UPDATE user SET password='' where name = 'Guest' and password !=''")) - session.commit() + trans.commit() except exc.OperationalError: print('Settings database is not writeable. Exiting...') sys.exit(2) @@ -648,7 +649,7 @@ def migrate_Database(session): is None: create_anonymous_user(session) - migrate_guest_password(engine, session) + migrate_guest_password(engine) def clean_database(session):