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) 2019 OzzieIsaacs, pwr
|
|
|
|
#
|
|
|
|
# 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
|
2019-07-14 11:20:40 +00:00
|
|
|
import sys
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-08-15 13:07:03 +00:00
|
|
|
from sqlalchemy import exc, Column, String, Integer, SmallInteger, Boolean, BLOB, JSON
|
2020-12-07 18:53:34 +00:00
|
|
|
from sqlalchemy.exc import OperationalError
|
2019-07-13 18:45:48 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
2020-01-05 15:43:48 +00:00
|
|
|
from . import constants, cli, logger, ub
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
_Base = declarative_base()
|
|
|
|
|
2020-05-04 17:02:03 +00:00
|
|
|
class _Flask_Settings(_Base):
|
|
|
|
__tablename__ = 'flask_settings'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
flask_session_key = Column(BLOB, default="")
|
|
|
|
|
|
|
|
def __init__(self, key):
|
|
|
|
self.flask_session_key = key
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Baseclass for representing settings in app.db with email server settings and Calibre database settings
|
|
|
|
# (application settings)
|
|
|
|
class _Settings(_Base):
|
|
|
|
__tablename__ = 'settings'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2019-12-28 15:18:21 +00:00
|
|
|
mail_server = Column(String, default=constants.DEFAULT_MAIL_SERVER)
|
2019-07-13 18:45:48 +00:00
|
|
|
mail_port = Column(Integer, default=25)
|
|
|
|
mail_use_ssl = Column(SmallInteger, default=0)
|
|
|
|
mail_login = Column(String, default='mail@example.com')
|
|
|
|
mail_password = Column(String, default='mypassword')
|
|
|
|
mail_from = Column(String, default='automailer <mail@example.com>')
|
2020-05-09 18:29:17 +00:00
|
|
|
mail_size = Column(Integer, default=25*1024*1024)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_calibre_dir = Column(String)
|
|
|
|
config_port = Column(Integer, default=constants.DEFAULT_PORT)
|
2020-07-03 16:19:11 +00:00
|
|
|
config_external_port = Column(Integer, default=constants.DEFAULT_PORT)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_certfile = Column(String)
|
|
|
|
config_keyfile = Column(String)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_calibre_web_title = Column(String, default=u'Calibre-Web')
|
|
|
|
config_books_per_page = Column(Integer, default=60)
|
|
|
|
config_random_books = Column(Integer, default=4)
|
|
|
|
config_authors_max = Column(Integer, default=0)
|
|
|
|
config_read_column = Column(Integer, default=0)
|
|
|
|
config_title_regex = Column(String, default=u'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+')
|
2020-01-05 15:43:48 +00:00
|
|
|
config_mature_content_tags = Column(String, default='')
|
2019-07-14 11:20:40 +00:00
|
|
|
config_theme = Column(Integer, default=0)
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_log_level = Column(SmallInteger, default=logger.DEFAULT_LOG_LEVEL)
|
2019-07-14 11:20:40 +00:00
|
|
|
config_logfile = Column(String)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_access_log = Column(SmallInteger, default=0)
|
2019-07-14 11:20:40 +00:00
|
|
|
config_access_logfile = Column(String)
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_uploading = Column(SmallInteger, default=0)
|
|
|
|
config_anonbrowse = Column(SmallInteger, default=0)
|
|
|
|
config_public_reg = Column(SmallInteger, default=0)
|
2019-07-14 11:20:40 +00:00
|
|
|
config_remote_login = Column(Boolean, default=False)
|
2020-01-26 15:52:40 +00:00
|
|
|
config_kobo_sync = Column(Boolean, default=False)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_default_role = Column(SmallInteger, default=0)
|
2020-04-13 20:23:58 +00:00
|
|
|
config_default_show = Column(SmallInteger, default=constants.ADMIN_USER_SIDEBAR)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_columns_to_ignore = Column(String)
|
2020-01-05 15:43:48 +00:00
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
config_denied_tags = Column(String, default="")
|
2020-01-05 07:43:00 +00:00
|
|
|
config_allowed_tags = Column(String, default="")
|
2019-12-30 14:15:07 +00:00
|
|
|
config_restricted_column = Column(SmallInteger, default=0)
|
2020-02-15 09:21:45 +00:00
|
|
|
config_denied_column_value = Column(String, default="")
|
2020-01-05 07:43:00 +00:00
|
|
|
config_allowed_column_value = Column(String, default="")
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_use_google_drive = Column(Boolean, default=False)
|
|
|
|
config_google_drive_folder = Column(String)
|
2020-08-15 13:07:03 +00:00
|
|
|
config_google_drive_watch_changes_response = Column(JSON, default={})
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_use_goodreads = Column(Boolean, default=False)
|
|
|
|
config_goodreads_api_key = Column(String)
|
|
|
|
config_goodreads_api_secret = Column(String)
|
2020-05-02 08:18:01 +00:00
|
|
|
config_register_email = Column(Boolean, default=False)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_login_type = Column(Integer, default=0)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-01-26 15:52:40 +00:00
|
|
|
config_kobo_proxy = Column(Boolean, default=False)
|
|
|
|
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_provider_url = Column(String, default='example.org')
|
2019-07-13 18:45:48 +00:00
|
|
|
config_ldap_port = Column(SmallInteger, default=389)
|
2020-04-17 16:16:21 +00:00
|
|
|
config_ldap_authentication = Column(SmallInteger, default=constants.LDAP_AUTH_SIMPLE)
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_serv_username = Column(String, default='cn=admin,dc=example,dc=org')
|
2020-04-13 20:23:58 +00:00
|
|
|
config_ldap_serv_password = Column(String, default="")
|
2020-04-05 15:31:41 +00:00
|
|
|
config_ldap_encryption = Column(SmallInteger, default=0)
|
2020-11-20 18:47:50 +00:00
|
|
|
config_ldap_cacert_path = Column(String, default="")
|
2020-04-13 20:23:58 +00:00
|
|
|
config_ldap_cert_path = Column(String, default="")
|
2020-11-20 18:47:50 +00:00
|
|
|
config_ldap_key_path = Column(String, default="")
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_dn = Column(String, default='dc=example,dc=org')
|
|
|
|
config_ldap_user_object = Column(String, default='uid=%s')
|
2020-12-03 15:01:15 +00:00
|
|
|
config_ldap_member_user_object = Column(String, default='') #
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_openldap = Column(Boolean, default=True)
|
|
|
|
config_ldap_group_object_filter = Column(String, default='(&(objectclass=posixGroup)(cn=%s))')
|
|
|
|
config_ldap_group_members_field = Column(String, default='memberUid')
|
|
|
|
config_ldap_group_name = Column(String, default='calibreweb')
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
config_kepubifypath = Column(String, default=None)
|
|
|
|
config_converterpath = Column(String, default=None)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_calibre = Column(String)
|
2020-05-09 09:03:11 +00:00
|
|
|
config_rarfile_location = Column(String, default=None)
|
2020-05-02 09:24:30 +00:00
|
|
|
config_upload_formats = Column(String, default=','.join(constants.EXTENSIONS_UPLOAD))
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_updatechannel = Column(Integer, default=constants.UPDATE_STABLE)
|
|
|
|
|
2020-01-05 07:43:00 +00:00
|
|
|
config_reverse_proxy_login_header_name = Column(String)
|
|
|
|
config_allow_reverse_proxy_header_login = Column(Boolean, default=False)
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
|
# Class holds all application specific settings in calibre-web
|
|
|
|
class _ConfigSQL(object):
|
|
|
|
# pylint: disable=no-member
|
|
|
|
def __init__(self, session):
|
|
|
|
self._session = session
|
|
|
|
self._settings = None
|
|
|
|
self.db_configured = None
|
|
|
|
self.config_calibre_dir = None
|
|
|
|
self.load()
|
|
|
|
|
2020-05-09 10:04:00 +00:00
|
|
|
change = False
|
2021-03-14 12:28:52 +00:00
|
|
|
if self.config_converterpath == None: # pylint: disable=access-member-before-definition
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
|
|
|
self.config_converterpath = autodetect_calibre_binary()
|
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
if self.config_kepubifypath == None: # pylint: disable=access-member-before-definition
|
|
|
|
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
|
|
|
self.config_kepubifypath = autodetect_kepubify_binary()
|
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
if self.config_rarfile_location == None: # pylint: disable=access-member-before-definition
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
|
|
|
self.config_rarfile_location = autodetect_unrar_binary()
|
|
|
|
if change:
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def _read_from_storage(self):
|
|
|
|
if self._settings is None:
|
|
|
|
log.debug("_ConfigSQL._read_from_storage")
|
|
|
|
self._settings = self._session.query(_Settings).first()
|
|
|
|
return self._settings
|
|
|
|
|
|
|
|
def get_config_certfile(self):
|
|
|
|
if cli.certfilepath:
|
|
|
|
return cli.certfilepath
|
|
|
|
if cli.certfilepath == "":
|
|
|
|
return None
|
|
|
|
return self.config_certfile
|
|
|
|
|
|
|
|
def get_config_keyfile(self):
|
|
|
|
if cli.keyfilepath:
|
|
|
|
return cli.keyfilepath
|
|
|
|
if cli.certfilepath == "":
|
|
|
|
return None
|
|
|
|
return self.config_keyfile
|
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_config_ipaddress():
|
2019-07-13 18:45:48 +00:00
|
|
|
return cli.ipadress or ""
|
|
|
|
|
|
|
|
def _has_role(self, role_flag):
|
|
|
|
return constants.has_flag(self.config_default_role, role_flag)
|
|
|
|
|
|
|
|
def role_admin(self):
|
|
|
|
return self._has_role(constants.ROLE_ADMIN)
|
|
|
|
|
|
|
|
def role_download(self):
|
|
|
|
return self._has_role(constants.ROLE_DOWNLOAD)
|
|
|
|
|
|
|
|
def role_viewer(self):
|
|
|
|
return self._has_role(constants.ROLE_VIEWER)
|
|
|
|
|
|
|
|
def role_upload(self):
|
|
|
|
return self._has_role(constants.ROLE_UPLOAD)
|
|
|
|
|
|
|
|
def role_edit(self):
|
|
|
|
return self._has_role(constants.ROLE_EDIT)
|
|
|
|
|
|
|
|
def role_passwd(self):
|
|
|
|
return self._has_role(constants.ROLE_PASSWD)
|
|
|
|
|
|
|
|
def role_edit_shelfs(self):
|
|
|
|
return self._has_role(constants.ROLE_EDIT_SHELFS)
|
|
|
|
|
|
|
|
def role_delete_books(self):
|
|
|
|
return self._has_role(constants.ROLE_DELETE_BOOKS)
|
|
|
|
|
|
|
|
def show_element_new_user(self, value):
|
|
|
|
return constants.has_flag(self.config_default_show, value)
|
|
|
|
|
|
|
|
def show_detail_random(self):
|
|
|
|
return self.show_element_new_user(constants.DETAIL_RANDOM)
|
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
def list_denied_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_denied_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-01-01 16:26:47 +00:00
|
|
|
def list_allowed_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_allowed_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2020-01-01 16:26:47 +00:00
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
def list_denied_column_values(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_denied_column_value or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2020-01-01 16:26:47 +00:00
|
|
|
|
|
|
|
def list_allowed_column_values(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_allowed_column_value or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def get_log_level(self):
|
|
|
|
return logger.get_level_name(self.config_log_level)
|
|
|
|
|
|
|
|
def get_mail_settings(self):
|
|
|
|
return {k:v for k, v in self.__dict__.items() if k.startswith('mail_')}
|
|
|
|
|
2019-12-28 15:18:21 +00:00
|
|
|
def get_mail_server_configured(self):
|
|
|
|
return not bool(self.mail_server == constants.DEFAULT_MAIL_SERVER)
|
|
|
|
|
|
|
|
|
2020-04-05 15:31:41 +00:00
|
|
|
def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None):
|
2019-07-13 18:45:48 +00:00
|
|
|
'''Possibly updates a field of this object.
|
|
|
|
The new value, if present, is grabbed from the given dictionary, and optionally passed through a convertor.
|
|
|
|
|
|
|
|
:returns: `True` if the field has changed value
|
|
|
|
'''
|
|
|
|
new_value = dictionary.get(field, default)
|
|
|
|
if new_value is None:
|
|
|
|
# log.debug("_ConfigSQL set_from_dictionary field '%s' not found", field)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if field not in self.__dict__:
|
|
|
|
log.warning("_ConfigSQL trying to set unknown field '%s' = %r", field, new_value)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if convertor is not None:
|
2020-04-05 15:31:41 +00:00
|
|
|
if encode:
|
|
|
|
new_value = convertor(new_value.encode(encode))
|
|
|
|
else:
|
|
|
|
new_value = convertor(new_value)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
current_value = self.__dict__.get(field)
|
|
|
|
if current_value == new_value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# log.debug("_ConfigSQL set_from_dictionary '%s' = %r (was %r)", field, new_value, current_value)
|
|
|
|
setattr(self, field, new_value)
|
|
|
|
return True
|
|
|
|
|
2020-12-12 09:02:11 +00:00
|
|
|
def toDict(self):
|
|
|
|
storage = {}
|
|
|
|
for k, v in self.__dict__.items():
|
2021-01-03 18:27:24 +00:00
|
|
|
if k[0] != '_' and not k.endswith("password") and not k.endswith("secret"):
|
2020-12-12 09:02:11 +00:00
|
|
|
storage[k] = v
|
|
|
|
return storage
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def load(self):
|
|
|
|
'''Load all configuration values from the underlying storage.'''
|
|
|
|
s = self._read_from_storage() # type: _Settings
|
|
|
|
for k, v in s.__dict__.items():
|
|
|
|
if k[0] != '_':
|
|
|
|
if v is None:
|
|
|
|
# if the storage column has no value, apply the (possible) default
|
|
|
|
column = s.__class__.__dict__.get(k)
|
|
|
|
if column.default is not None:
|
|
|
|
v = column.default.arg
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
2019-07-14 11:43:40 +00:00
|
|
|
have_metadata_db = bool(self.config_calibre_dir)
|
|
|
|
if have_metadata_db:
|
|
|
|
if not self.config_use_google_drive:
|
|
|
|
db_file = os.path.join(self.config_calibre_dir, 'metadata.db')
|
|
|
|
have_metadata_db = os.path.isfile(db_file)
|
|
|
|
self.db_configured = have_metadata_db
|
2020-09-08 18:57:39 +00:00
|
|
|
constants.EXTENSIONS_UPLOAD = [x.lstrip().rstrip().lower() for x in self.config_upload_formats.split(',')]
|
2021-03-14 12:28:52 +00:00
|
|
|
# pylint: disable=access-member-before-definition
|
2020-05-24 07:59:45 +00:00
|
|
|
logfile = logger.setup(self.config_logfile, self.config_log_level)
|
|
|
|
if logfile != self.config_logfile:
|
|
|
|
log.warning("Log path %s not valid, falling back to default", self.config_logfile)
|
|
|
|
self.config_logfile = logfile
|
|
|
|
self._session.merge(s)
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
self._session.commit()
|
|
|
|
except OperationalError as e:
|
|
|
|
log.error('Database error: %s', e)
|
|
|
|
self._session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
'''Apply all configuration values to the underlying storage.'''
|
|
|
|
s = self._read_from_storage() # type: _Settings
|
|
|
|
|
|
|
|
for k, v in self.__dict__.items():
|
|
|
|
if k[0] == '_':
|
|
|
|
continue
|
2020-01-05 07:43:00 +00:00
|
|
|
if hasattr(s, k):
|
2019-07-13 18:45:48 +00:00
|
|
|
setattr(s, k, v)
|
|
|
|
|
|
|
|
log.debug("_ConfigSQL updating storage")
|
|
|
|
self._session.merge(s)
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
self._session.commit()
|
|
|
|
except OperationalError as e:
|
|
|
|
log.error('Database error: %s', e)
|
|
|
|
self._session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
self.load()
|
|
|
|
|
2020-04-13 20:23:58 +00:00
|
|
|
def invalidate(self, error=None):
|
|
|
|
if error:
|
|
|
|
log.error(error)
|
2019-07-13 18:45:48 +00:00
|
|
|
log.warning("invalidating configuration")
|
|
|
|
self.db_configured = False
|
|
|
|
self.config_calibre_dir = None
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
|
|
def _migrate_table(session, orm_class):
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
for column_name, column in orm_class.__dict__.items():
|
|
|
|
if column_name[0] != '_':
|
|
|
|
try:
|
|
|
|
session.query(column).first()
|
|
|
|
except exc.OperationalError as err:
|
2020-01-05 07:43:00 +00:00
|
|
|
log.debug("%s: %s", column_name, err.args[0])
|
2019-07-21 14:23:31 +00:00
|
|
|
if column.default is not None:
|
|
|
|
if sys.version_info < (3, 0):
|
2020-05-04 17:02:03 +00:00
|
|
|
if isinstance(column.default.arg, unicode):
|
2019-07-21 14:23:31 +00:00
|
|
|
column.default.arg = column.default.arg.encode('utf-8')
|
2020-01-05 07:43:00 +00:00
|
|
|
if column.default is None:
|
|
|
|
column_default = ""
|
|
|
|
else:
|
|
|
|
if isinstance(column.default.arg, bool):
|
|
|
|
column_default = ("DEFAULT %r" % int(column.default.arg))
|
|
|
|
else:
|
|
|
|
column_default = ("DEFAULT %r" % column.default.arg)
|
2019-07-21 14:23:31 +00:00
|
|
|
alter_table = "ALTER TABLE %s ADD COLUMN `%s` %s %s" % (orm_class.__tablename__,
|
|
|
|
column_name,
|
|
|
|
column.type,
|
|
|
|
column_default)
|
2019-07-14 11:43:40 +00:00
|
|
|
log.debug(alter_table)
|
2019-07-13 18:45:48 +00:00
|
|
|
session.execute(alter_table)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if changed:
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
session.commit()
|
|
|
|
except OperationalError:
|
|
|
|
session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
|
2019-07-14 11:20:40 +00:00
|
|
|
def autodetect_calibre_binary():
|
|
|
|
if sys.platform == "win32":
|
2020-08-09 13:11:54 +00:00
|
|
|
calibre_path = ["C:\\program files\\calibre\\ebook-convert.exe",
|
|
|
|
"C:\\program files(x86)\\calibre\\ebook-convert.exe",
|
|
|
|
"C:\\program files(x86)\\calibre2\\ebook-convert.exe",
|
|
|
|
"C:\\program files\\calibre2\\ebook-convert.exe"]
|
2019-07-14 11:20:40 +00:00
|
|
|
else:
|
|
|
|
calibre_path = ["/opt/calibre/ebook-convert"]
|
|
|
|
for element in calibre_path:
|
|
|
|
if os.path.isfile(element) and os.access(element, os.X_OK):
|
|
|
|
return element
|
2020-05-09 09:03:11 +00:00
|
|
|
return ""
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
def autodetect_unrar_binary():
|
|
|
|
if sys.platform == "win32":
|
|
|
|
calibre_path = ["C:\\program files\\WinRar\\unRAR.exe",
|
|
|
|
"C:\\program files(x86)\\WinRar\\unRAR.exe"]
|
|
|
|
else:
|
|
|
|
calibre_path = ["/usr/bin/unrar"]
|
|
|
|
for element in calibre_path:
|
|
|
|
if os.path.isfile(element) and os.access(element, os.X_OK):
|
|
|
|
return element
|
|
|
|
return ""
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
def autodetect_kepubify_binary():
|
|
|
|
if sys.platform == "win32":
|
|
|
|
calibre_path = ["C:\\program files\\kepubify\\kepubify-windows-64Bit.exe",
|
|
|
|
"C:\\program files(x86)\\kepubify\\kepubify-windows-64Bit.exe"]
|
|
|
|
else:
|
|
|
|
calibre_path = ["/opt/kepubify/kepubify-linux-64bit", "/opt/kepubify/kepubify-linux-32bit"]
|
|
|
|
for element in calibre_path:
|
|
|
|
if os.path.isfile(element) and os.access(element, os.X_OK):
|
|
|
|
return element
|
|
|
|
return ""
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def _migrate_database(session):
|
|
|
|
# make sure the table is created, if it does not exist
|
|
|
|
_Base.metadata.create_all(session.bind)
|
|
|
|
_migrate_table(session, _Settings)
|
2020-05-04 17:02:03 +00:00
|
|
|
_migrate_table(session, _Flask_Settings)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_configuration(session):
|
|
|
|
_migrate_database(session)
|
|
|
|
|
|
|
|
if not session.query(_Settings).count():
|
|
|
|
session.add(_Settings())
|
|
|
|
session.commit()
|
2020-01-05 15:43:48 +00:00
|
|
|
conf = _ConfigSQL(session)
|
|
|
|
# Migrate from global restrictions to user based restrictions
|
2020-02-15 09:21:45 +00:00
|
|
|
if bool(conf.config_default_show & constants.MATURE_CONTENT) and conf.config_denied_tags == "":
|
|
|
|
conf.config_denied_tags = conf.config_mature_content_tags
|
2020-01-05 15:43:48 +00:00
|
|
|
conf.save()
|
|
|
|
session.query(ub.User).filter(ub.User.mature_content != True). \
|
restricted_tags -> denied_tags
Fix for the next error after update:
Traceback (most recent call last):
File "cps.py", line 34, in <module>
from cps import create_app
File "/home/rewerson/lib/web/cps/__init__.py", line 68, in <module>
config = config_sql.load_configuration(ub.session)
File "/home/rewerson/lib/web/cps/config_sql.py", line 348, in load_configuration
update({"restricted_tags": conf.config_mature_content_tags}, synchronize_session=False)
File "/home/rewerson/.local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3862, in update
update_op.exec_()
File "/home/rewerson/.local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1693, in exec_
self._do_exec()
File "/home/rewerson/.local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1874, in _do_exec
values = self._resolved_values
File "/home/rewerson/.local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1840, in _resolved_values
desc = _entity_descriptor(self.mapper, k)
File "/home/rewerson/.local/lib/python2.7/site-packages/sqlalchemy/orm/base.py", line 402, in _entity_descriptor
"Entity '%s' has no property '%s'" % (description, key)
sqlalchemy.exc.InvalidRequestError: Entity '<class 'cps.ub.User'>' has no property 'restricted_tags'
2020-02-25 13:30:51 +00:00
|
|
|
update({"denied_tags": conf.config_mature_content_tags}, synchronize_session=False)
|
2020-01-05 15:43:48 +00:00
|
|
|
session.commit()
|
|
|
|
return conf
|
2020-05-04 17:02:03 +00:00
|
|
|
|
|
|
|
def get_flask_session_key(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()
|
|
|
|
return flask_settings.flask_session_key
|