From f13522559d1b7749037e2cecb2033a1425218716 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Mon, 7 Dec 2020 13:06:29 +0100 Subject: [PATCH] Fixed problems on startup with config session --- cps/__init__.py | 5 ++--- cps/config_sql.py | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/cps/__init__.py b/cps/__init__.py index 575a5a8a..1f28b918 100644 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -71,7 +71,7 @@ lm.session_protection = 'strong' ub.init_db(cli.settingspath) # pylint: disable=no-member -config = config_sql.load_configuration(ub.Scoped_Session) +config = config_sql.load_configuration() web_server = WebServer() @@ -100,7 +100,7 @@ def create_app(): log.info('Starting Calibre Web...') Principal(app) lm.init_app(app) - app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.Scoped_Session)) + app.secret_key = os.getenv('SECRET_KEY', config.get_flask_session_key()) web_server.init_app(app, config) @@ -114,7 +114,6 @@ def create_app(): services.goodreads_support.connect(config.config_goodreads_api_key, config.config_goodreads_api_secret, config.config_use_goodreads) - return app @babel.localeselector diff --git a/cps/config_sql.py b/cps/config_sql.py index eabe4ba7..3e531eef 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -139,6 +139,7 @@ class _ConfigSQL(object): # pylint: disable=no-member def __init__(self, session): self._session = session + self._session.expire_on_commit = False self._settings = None self.db_configured = None self.config_calibre_dir = None @@ -241,6 +242,14 @@ class _ConfigSQL(object): def get_mail_server_configured(self): return not bool(self.mail_server == constants.DEFAULT_MAIL_SERVER) + def get_flask_session_key(self): + flask_settings = self._session.query(_Flask_Settings).one_or_none() + if flask_settings == None: + flask_settings = _Flask_Settings(os.urandom(32)) + self._session.add(flask_settings) + self._session.commit() + return flask_settings.flask_session_key + def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None): '''Possibly updates a field of this object. @@ -273,6 +282,10 @@ class _ConfigSQL(object): def load(self): '''Load all configuration values from the underlying storage.''' + #own = False + #if not self._session: + # self._session = ub.Scoped_Session() + # own = True s = self._read_from_storage() # type: _Settings for k, v in s.__dict__.items(): if k[0] != '_': @@ -395,8 +408,8 @@ def _migrate_database(session): _migrate_table(session, _Flask_Settings) -def load_configuration(Session): - session = Session() +def load_configuration(): + session = ub.Scoped_Session() _migrate_database(session) if not session.query(_Settings).count(): @@ -410,15 +423,4 @@ def load_configuration(Session): session.query(ub.User).filter(ub.User.mature_content != True). \ update({"denied_tags": conf.config_mature_content_tags}, synchronize_session=False) session.commit() - session.close() return conf - -def get_flask_session_key(Session): - session = Session() - flask_settings = session.query(_Flask_Settings).one_or_none() - if flask_settings == None: - flask_settings = _Flask_Settings(os.urandom(32)) - session.add(flask_settings) - session.commit() - session.close() - return flask_settings.flask_session_key