2015-08-02 18:59:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2012-2019 mutschler, jkrehm, cervinko, janeczku, OzzieIsaacs, csitko
|
|
|
|
# ok11, issmirnov, idalin
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
import atexit
|
2015-08-02 18:59:11 +00:00
|
|
|
import os
|
2020-06-27 10:31:26 +00:00
|
|
|
import sys
|
2017-07-08 01:18:03 +00:00
|
|
|
import datetime
|
2020-02-15 22:27:07 +00:00
|
|
|
import itertools
|
2020-03-12 23:31:42 +00:00
|
|
|
import uuid
|
2020-09-27 17:12:10 +00:00
|
|
|
from flask import session as flask_session
|
2017-07-08 01:18:03 +00:00
|
|
|
from binascii import hexlify
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2020-10-10 08:32:53 +00:00
|
|
|
from flask_login import AnonymousUserMixin, current_user
|
2021-07-30 09:43:26 +00:00
|
|
|
from flask_login import user_logged_in
|
2020-12-12 10:23:17 +00:00
|
|
|
|
2019-02-09 20:26:17 +00:00
|
|
|
try:
|
2019-02-16 06:23:08 +00:00
|
|
|
from flask_dance.consumer.backend.sqla import OAuthConsumerMixin
|
|
|
|
oauth_support = True
|
2021-03-24 17:45:59 +00:00
|
|
|
except ImportError as e:
|
2020-04-14 16:28:16 +00:00
|
|
|
# fails on flask-dance >1.3, due to renaming
|
|
|
|
try:
|
|
|
|
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin
|
|
|
|
oauth_support = True
|
2021-03-24 17:45:59 +00:00
|
|
|
except ImportError as e:
|
2022-02-06 13:22:55 +00:00
|
|
|
OAuthConsumerMixin = BaseException
|
2020-04-14 16:28:16 +00:00
|
|
|
oauth_support = False
|
2021-03-20 09:09:08 +00:00
|
|
|
from sqlalchemy import create_engine, exc, exists, event, text
|
2019-06-06 15:10:22 +00:00
|
|
|
from sqlalchemy import Column, ForeignKey
|
2020-07-08 19:18:38 +00:00
|
|
|
from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime, Float, JSON
|
2020-09-27 10:37:41 +00:00
|
|
|
from sqlalchemy.orm.attributes import flag_modified
|
2021-01-17 08:17:46 +00:00
|
|
|
from sqlalchemy.sql.expression import func
|
2021-03-20 09:09:08 +00:00
|
|
|
try:
|
2021-04-08 17:37:08 +00:00
|
|
|
# Compatibility with sqlalchemy 2.0
|
2021-03-20 09:09:08 +00:00
|
|
|
from sqlalchemy.orm import declarative_base
|
|
|
|
except ImportError:
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2020-12-08 10:39:23 +00:00
|
|
|
from sqlalchemy.orm import backref, relationship, sessionmaker, Session, scoped_session
|
2019-06-06 15:10:22 +00:00
|
|
|
from werkzeug.security import generate_password_hash
|
2019-02-09 20:26:17 +00:00
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
from . import constants, logger
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2023-02-15 18:53:35 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
log = logger.create()
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2019-02-17 08:09:20 +00:00
|
|
|
session = None
|
2020-05-21 07:28:35 +00:00
|
|
|
app_DB_path = None
|
2019-02-06 20:52:24 +00:00
|
|
|
Base = declarative_base()
|
2020-10-10 08:32:53 +00:00
|
|
|
searched_ids = {}
|
2019-02-06 20:52:24 +00:00
|
|
|
|
2021-07-25 03:24:03 +00:00
|
|
|
logged_in = dict()
|
|
|
|
|
2019-02-06 20:52:24 +00:00
|
|
|
|
2021-07-30 09:43:26 +00:00
|
|
|
def signal_store_user_session(object, user):
|
|
|
|
store_user_session()
|
|
|
|
|
2022-03-20 10:21:15 +00:00
|
|
|
|
2021-07-30 09:43:26 +00:00
|
|
|
def store_user_session():
|
2021-11-13 13:57:01 +00:00
|
|
|
if flask_session.get('user_id', ""):
|
|
|
|
flask_session['_user_id'] = flask_session.get('user_id', "")
|
2021-07-30 09:43:26 +00:00
|
|
|
if flask_session.get('_user_id', ""):
|
|
|
|
try:
|
|
|
|
if not check_user_session(flask_session.get('_user_id', ""), flask_session.get('_id', "")):
|
|
|
|
user_session = User_Sessions(flask_session.get('_user_id', ""), flask_session.get('_id', ""))
|
|
|
|
session.add(user_session)
|
|
|
|
session.commit()
|
2021-08-27 07:43:32 +00:00
|
|
|
log.debug("Login and store session : " + flask_session.get('_id', ""))
|
2021-08-15 10:43:19 +00:00
|
|
|
else:
|
2021-08-27 07:43:32 +00:00
|
|
|
log.debug("Found stored session: " + flask_session.get('_id', ""))
|
2021-08-15 10:43:19 +00:00
|
|
|
except (exc.OperationalError, exc.InvalidRequestError) as e:
|
2021-07-30 09:43:26 +00:00
|
|
|
session.rollback()
|
2021-08-15 10:43:19 +00:00
|
|
|
log.exception(e)
|
|
|
|
else:
|
|
|
|
log.error("No user id in session")
|
2021-07-30 09:43:26 +00:00
|
|
|
|
2022-03-20 10:21:15 +00:00
|
|
|
|
2021-07-30 09:43:26 +00:00
|
|
|
def delete_user_session(user_id, session_key):
|
|
|
|
try:
|
2021-08-27 07:43:32 +00:00
|
|
|
log.debug("Deleted session_key: " + session_key)
|
2022-03-20 10:21:15 +00:00
|
|
|
session.query(User_Sessions).filter(User_Sessions.user_id == user_id,
|
|
|
|
User_Sessions.session_key == session_key).delete()
|
2021-07-30 09:43:26 +00:00
|
|
|
session.commit()
|
2022-03-20 10:21:15 +00:00
|
|
|
except (exc.OperationalError, exc.InvalidRequestError) as ex:
|
2021-07-30 09:43:26 +00:00
|
|
|
session.rollback()
|
2022-03-20 10:21:15 +00:00
|
|
|
log.exception(ex)
|
2021-07-30 09:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_user_session(user_id, session_key):
|
2021-08-15 10:43:19 +00:00
|
|
|
try:
|
|
|
|
return bool(session.query(User_Sessions).filter(User_Sessions.user_id==user_id,
|
|
|
|
User_Sessions.session_key==session_key).one_or_none())
|
2023-11-02 15:34:04 +00:00
|
|
|
except (exc.OperationalError, exc.InvalidRequestError) as e:
|
2021-08-15 10:43:19 +00:00
|
|
|
session.rollback()
|
|
|
|
log.exception(e)
|
|
|
|
|
2021-07-30 09:43:26 +00:00
|
|
|
|
|
|
|
user_logged_in.connect(signal_store_user_session)
|
|
|
|
|
2020-10-10 08:32:53 +00:00
|
|
|
def store_ids(result):
|
|
|
|
ids = list()
|
|
|
|
for element in result:
|
|
|
|
ids.append(element.id)
|
|
|
|
searched_ids[current_user.id] = ids
|
|
|
|
|
2021-10-24 08:57:29 +00:00
|
|
|
def store_combo_ids(result):
|
|
|
|
ids = list()
|
|
|
|
for element in result:
|
|
|
|
ids.append(element[0].id)
|
|
|
|
searched_ids[current_user.id] = ids
|
|
|
|
|
2020-10-10 08:32:53 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
class UserBase:
|
2018-08-19 08:14:20 +00:00
|
|
|
|
2017-10-10 22:14:19 +00:00
|
|
|
@property
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_authenticated(self):
|
2021-07-25 03:24:03 +00:00
|
|
|
return self.is_active
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-06-06 15:10:22 +00:00
|
|
|
def _has_role(self, role_flag):
|
|
|
|
return constants.has_flag(self.role, role_flag)
|
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_admin(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_ADMIN)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_download(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_DOWNLOAD)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_upload(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_UPLOAD)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_edit(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_EDIT)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 14:00:58 +00:00
|
|
|
def role_passwd(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_PASSWD)
|
2016-04-27 08:35:23 +00:00
|
|
|
|
2017-01-12 19:43:36 +00:00
|
|
|
def role_anonymous(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_ANONYMOUS)
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2017-03-19 19:29:35 +00:00
|
|
|
def role_edit_shelfs(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_EDIT_SHELFS)
|
2017-03-19 19:29:35 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
def role_delete_books(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_DELETE_BOOKS)
|
2019-05-19 16:39:34 +00:00
|
|
|
|
|
|
|
def role_viewer(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self._has_role(constants.ROLE_VIEWER)
|
2019-05-19 16:39:34 +00:00
|
|
|
|
2017-10-10 22:14:19 +00:00
|
|
|
@property
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
2017-10-10 22:14:19 +00:00
|
|
|
@property
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_anonymous(self):
|
2019-07-07 12:53:38 +00:00
|
|
|
return self.role_anonymous()
|
2016-04-27 08:35:23 +00:00
|
|
|
|
|
|
|
def get_id(self):
|
2017-03-05 09:40:39 +00:00
|
|
|
return str(self.id)
|
2016-04-27 08:35:23 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
def filter_language(self):
|
|
|
|
return self.default_language
|
|
|
|
|
2019-03-03 18:42:17 +00:00
|
|
|
def check_visibility(self, value):
|
2020-04-29 16:57:39 +00:00
|
|
|
if value == constants.SIDEBAR_RECENT:
|
|
|
|
return True
|
2019-06-06 15:10:22 +00:00
|
|
|
return constants.has_flag(self.sidebar_view, value)
|
2019-03-03 18:42:17 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def show_detail_random(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return self.check_visibility(constants.DETAIL_RANDOM)
|
2017-01-22 20:30:36 +00:00
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
def list_denied_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.denied_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-12-30 14:15:07 +00:00
|
|
|
|
|
|
|
def list_allowed_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.allowed_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-12-30 14:15:07 +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.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.allowed_column_value or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2017-01-22 20:30:36 +00:00
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
def get_view_property(self, page, prop):
|
2020-09-27 10:37:41 +00:00
|
|
|
if not self.view_settings.get(page):
|
|
|
|
return None
|
2021-03-14 12:28:52 +00:00
|
|
|
return self.view_settings[page].get(prop)
|
2020-09-27 10:37:41 +00:00
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
def set_view_property(self, page, prop, value):
|
2020-09-27 10:37:41 +00:00
|
|
|
if not self.view_settings.get(page):
|
|
|
|
self.view_settings[page] = dict()
|
2021-03-14 12:28:52 +00:00
|
|
|
self.view_settings[page][prop] = value
|
2020-09-27 10:37:41 +00:00
|
|
|
try:
|
|
|
|
flag_modified(self, "view_settings")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
session.commit()
|
2022-03-20 10:21:15 +00:00
|
|
|
except (exc.OperationalError, exc.InvalidRequestError) as e:
|
2020-09-27 10:37:41 +00:00
|
|
|
session.rollback()
|
2022-03-20 10:21:15 +00:00
|
|
|
log.error_or_exception(e)
|
2020-09-27 10:37:41 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def __repr__(self):
|
2021-03-21 17:55:02 +00:00
|
|
|
return '<User %r>' % self.name
|
2017-01-22 15:44:37 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
# Baseclass for Users in Calibre-Web, settings which are depending on certain users are stored here. It is derived from
|
2017-01-28 19:16:40 +00:00
|
|
|
# User Base (all access methods are declared there)
|
|
|
|
class User(UserBase, Base):
|
2017-01-12 19:43:36 +00:00
|
|
|
__tablename__ = 'user'
|
2019-06-22 08:55:09 +00:00
|
|
|
__table_args__ = {'sqlite_autoincrement': True}
|
2017-01-12 19:43:36 +00:00
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2021-03-21 17:55:02 +00:00
|
|
|
name = Column(String(64), unique=True)
|
2017-01-12 19:43:36 +00:00
|
|
|
email = Column(String(120), unique=True, default="")
|
2019-06-06 15:10:22 +00:00
|
|
|
role = Column(SmallInteger, default=constants.ROLE_USER)
|
2017-01-12 19:43:36 +00:00
|
|
|
password = Column(String)
|
|
|
|
kindle_mail = Column(String(120), default="")
|
2017-11-01 15:55:51 +00:00
|
|
|
shelf = relationship('Shelf', backref='user', lazy='dynamic', order_by='Shelf.name')
|
2017-01-12 19:43:36 +00:00
|
|
|
downloads = relationship('Downloads', backref='user', lazy='dynamic')
|
|
|
|
locale = Column(String(2), default="en")
|
2017-01-28 19:16:40 +00:00
|
|
|
sidebar_view = Column(Integer, default=1)
|
2017-01-12 19:43:36 +00:00
|
|
|
default_language = Column(String(3), default="all")
|
2020-02-15 09:21:45 +00:00
|
|
|
denied_tags = Column(String, default="")
|
2019-12-30 14:15:07 +00:00
|
|
|
allowed_tags = Column(String, default="")
|
2020-02-15 09:21:45 +00:00
|
|
|
denied_column_value = Column(String, default="")
|
2019-12-30 14:15:07 +00:00
|
|
|
allowed_column_value = Column(String, default="")
|
2020-01-11 18:10:39 +00:00
|
|
|
remote_auth_token = relationship('RemoteAuthToken', backref='user', lazy='dynamic')
|
2020-07-08 19:18:38 +00:00
|
|
|
view_settings = Column(JSON, default={})
|
2021-05-26 11:35:35 +00:00
|
|
|
kobo_only_shelves_sync = Column(Integer, default=0)
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2019-07-21 06:10:23 +00:00
|
|
|
|
2019-02-16 06:23:08 +00:00
|
|
|
if oauth_support:
|
|
|
|
class OAuth(OAuthConsumerMixin, Base):
|
|
|
|
provider_user_id = Column(String(256))
|
|
|
|
user_id = Column(Integer, ForeignKey(User.id))
|
|
|
|
user = relationship(User)
|
2018-10-11 11:52:30 +00:00
|
|
|
|
2019-07-21 06:10:23 +00:00
|
|
|
|
2019-07-20 18:01:05 +00:00
|
|
|
class OAuthProvider(Base):
|
|
|
|
__tablename__ = 'oauthProvider'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
provider_name = Column(String)
|
|
|
|
oauth_client_id = Column(String)
|
|
|
|
oauth_client_secret = Column(String)
|
|
|
|
active = Column(Boolean)
|
|
|
|
|
2018-10-11 11:52:30 +00:00
|
|
|
|
2022-07-01 13:26:06 +00:00
|
|
|
# Class for anonymous user is derived from User base and completely overrides methods and properties for the
|
2017-01-28 19:16:40 +00:00
|
|
|
# anonymous user
|
|
|
|
class Anonymous(AnonymousUserMixin, UserBase):
|
2017-01-12 19:43:36 +00:00
|
|
|
def __init__(self):
|
2024-06-18 18:13:26 +00:00
|
|
|
self.kobo_only_shelves_sync = None
|
|
|
|
self.view_settings = None
|
|
|
|
self.allowed_column_value = None
|
|
|
|
self.allowed_tags = None
|
|
|
|
self.denied_tags = None
|
|
|
|
self.kindle_mail = None
|
|
|
|
self.locale = None
|
|
|
|
self.default_language = None
|
|
|
|
self.sidebar_view = None
|
|
|
|
self.id = None
|
|
|
|
self.role = None
|
|
|
|
self.name = None
|
2017-01-12 19:43:36 +00:00
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
2020-04-19 17:08:58 +00:00
|
|
|
data = session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS)\
|
|
|
|
.first() # type: User
|
2021-03-21 17:55:02 +00:00
|
|
|
self.name = data.name
|
2017-01-12 19:43:36 +00:00
|
|
|
self.role = data.role
|
2017-10-09 20:36:47 +00:00
|
|
|
self.id=data.id
|
2017-01-28 19:16:40 +00:00
|
|
|
self.sidebar_view = data.sidebar_view
|
2017-01-12 19:43:36 +00:00
|
|
|
self.default_language = data.default_language
|
2017-01-14 14:05:49 +00:00
|
|
|
self.locale = data.locale
|
2019-04-14 14:37:57 +00:00
|
|
|
self.kindle_mail = data.kindle_mail
|
2020-02-15 09:21:45 +00:00
|
|
|
self.denied_tags = data.denied_tags
|
2019-12-30 14:15:07 +00:00
|
|
|
self.allowed_tags = data.allowed_tags
|
2020-02-15 09:21:45 +00:00
|
|
|
self.denied_column_value = data.denied_column_value
|
2019-12-30 14:15:07 +00:00
|
|
|
self.allowed_column_value = data.allowed_column_value
|
2020-08-24 18:38:06 +00:00
|
|
|
self.view_settings = data.view_settings
|
2021-05-15 08:45:51 +00:00
|
|
|
self.kobo_only_shelves_sync = data.kobo_only_shelves_sync
|
2020-07-08 19:18:38 +00:00
|
|
|
|
2019-07-07 12:53:38 +00:00
|
|
|
|
2017-01-12 19:43:36 +00:00
|
|
|
def role_admin(self):
|
|
|
|
return False
|
|
|
|
|
2017-10-10 14:18:28 +00:00
|
|
|
@property
|
2017-01-12 19:43:36 +00:00
|
|
|
def is_active(self):
|
|
|
|
return False
|
|
|
|
|
2017-10-10 14:18:28 +00:00
|
|
|
@property
|
2017-01-12 19:43:36 +00:00
|
|
|
def is_anonymous(self):
|
2020-04-19 17:08:58 +00:00
|
|
|
return True
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2017-10-10 14:18:28 +00:00
|
|
|
@property
|
2017-10-09 20:36:47 +00:00
|
|
|
def is_authenticated(self):
|
|
|
|
return False
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2020-09-27 17:12:10 +00:00
|
|
|
def get_view_property(self, page, prop):
|
2021-01-24 06:31:40 +00:00
|
|
|
if 'view' in flask_session:
|
|
|
|
if not flask_session['view'].get(page):
|
|
|
|
return None
|
|
|
|
return flask_session['view'][page].get(prop)
|
|
|
|
return None
|
2020-09-27 17:12:10 +00:00
|
|
|
|
|
|
|
def set_view_property(self, page, prop, value):
|
2021-11-23 18:32:11 +00:00
|
|
|
if not 'view' in flask_session:
|
|
|
|
flask_session['view'] = dict()
|
|
|
|
if not flask_session['view'].get(page):
|
|
|
|
flask_session['view'][page] = dict()
|
|
|
|
flask_session['view'][page][prop] = value
|
2020-09-27 17:12:10 +00:00
|
|
|
|
2021-07-30 09:43:26 +00:00
|
|
|
class User_Sessions(Base):
|
|
|
|
__tablename__ = 'user_session'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
session_key = Column(String, default="")
|
|
|
|
|
|
|
|
def __init__(self, user_id, session_key):
|
2024-06-18 18:13:26 +00:00
|
|
|
super().__init__()
|
2021-07-30 09:43:26 +00:00
|
|
|
self.user_id = user_id
|
|
|
|
self.session_key = session_key
|
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# Baseclass representing Shelfs in calibre-web in app.db
|
2015-08-02 18:59:11 +00:00
|
|
|
class Shelf(Base):
|
2016-04-27 08:35:23 +00:00
|
|
|
__tablename__ = 'shelf'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
2020-04-19 17:08:58 +00:00
|
|
|
uuid = Column(String, default=lambda: str(uuid.uuid4()))
|
2016-04-27 08:35:23 +00:00
|
|
|
name = Column(String)
|
|
|
|
is_public = Column(Integer, default=0)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2021-02-06 19:29:43 +00:00
|
|
|
kobo_sync = Column(Boolean, default=False)
|
2020-03-12 23:31:42 +00:00
|
|
|
books = relationship("BookShelf", backref="ub_shelf", cascade="all, delete-orphan", lazy="dynamic")
|
|
|
|
created = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def __repr__(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
return '<Shelf %d:%r>' % (self.id, self.name)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
# Baseclass representing Relationship between books and Shelfs in Calibre-Web in app.db (N:M)
|
2015-08-02 18:59:11 +00:00
|
|
|
class BookShelf(Base):
|
2016-04-27 08:35:23 +00:00
|
|
|
__tablename__ = 'book_shelf_link'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
2016-12-26 10:33:32 +00:00
|
|
|
order = Column(Integer)
|
2016-04-27 08:35:23 +00:00
|
|
|
shelf = Column(Integer, ForeignKey('shelf.id'))
|
2020-03-12 23:31:42 +00:00
|
|
|
date_added = Column(DateTime, default=datetime.datetime.utcnow)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def __repr__(self):
|
2016-12-23 08:53:39 +00:00
|
|
|
return '<Book %r>' % self.id
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
# This table keeps track of deleted Shelves so that deletes can be propagated to any paired Kobo device.
|
|
|
|
class ShelfArchive(Base):
|
|
|
|
__tablename__ = 'shelf_archive'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
uuid = Column(String)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
class ReadBook(Base):
|
|
|
|
__tablename__ = 'book_read_link'
|
|
|
|
|
2020-02-01 21:45:34 +00:00
|
|
|
STATUS_UNREAD = 0
|
|
|
|
STATUS_FINISHED = 1
|
|
|
|
STATUS_IN_PROGRESS = 2
|
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
2017-02-19 20:08:22 +00:00
|
|
|
book_id = Column(Integer, unique=False)
|
2017-08-23 15:52:52 +00:00
|
|
|
user_id = Column(Integer, ForeignKey('user.id'), unique=False)
|
2020-02-15 22:27:07 +00:00
|
|
|
read_status = Column(Integer, unique=False, default=STATUS_UNREAD, nullable=False)
|
2020-04-19 17:08:58 +00:00
|
|
|
kobo_reading_state = relationship("KoboReadingState", uselist=False,
|
|
|
|
primaryjoin="and_(ReadBook.user_id == foreign(KoboReadingState.user_id), "
|
|
|
|
"ReadBook.book_id == foreign(KoboReadingState.book_id))",
|
|
|
|
cascade="all",
|
|
|
|
backref=backref("book_read_link",
|
|
|
|
uselist=False))
|
2020-02-15 22:27:07 +00:00
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
|
|
|
last_time_started_reading = Column(DateTime, nullable=True)
|
|
|
|
times_started_reading = Column(Integer, default=0, nullable=False)
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
class Bookmark(Base):
|
|
|
|
__tablename__ = 'bookmark'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
book_id = Column(Integer)
|
|
|
|
format = Column(String(collation='NOCASE'))
|
|
|
|
bookmark_key = Column(String)
|
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2020-01-24 05:04:16 +00:00
|
|
|
# Baseclass representing books that are archived on the user's Kobo device.
|
|
|
|
class ArchivedBook(Base):
|
|
|
|
__tablename__ = 'archived_book'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
book_id = Column(Integer)
|
|
|
|
is_archived = Column(Boolean, unique=False)
|
2020-01-26 04:54:12 +00:00
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow)
|
2020-01-24 05:04:16 +00:00
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
|
2021-10-03 07:53:46 +00:00
|
|
|
class KoboSyncedBooks(Base):
|
|
|
|
__tablename__ = 'kobo_synced_books'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
book_id = Column(Integer)
|
|
|
|
|
2020-02-15 22:27:07 +00:00
|
|
|
# The Kobo ReadingState API keeps track of 4 timestamped entities:
|
|
|
|
# ReadingState, StatusInfo, Statistics, CurrentBookmark
|
|
|
|
# Which we map to the following 4 tables:
|
|
|
|
# KoboReadingState, ReadBook, KoboStatistics and KoboBookmark
|
|
|
|
class KoboReadingState(Base):
|
|
|
|
__tablename__ = 'kobo_reading_state'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
book_id = Column(Integer)
|
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
|
|
|
priority_timestamp = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
2022-01-09 12:54:35 +00:00
|
|
|
current_bookmark = relationship("KoboBookmark", uselist=False, backref="kobo_reading_state", cascade="all, delete")
|
|
|
|
statistics = relationship("KoboStatistics", uselist=False, backref="kobo_reading_state", cascade="all, delete")
|
2020-02-15 22:27:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KoboBookmark(Base):
|
|
|
|
__tablename__ = 'kobo_bookmark'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
kobo_reading_state_id = Column(Integer, ForeignKey('kobo_reading_state.id'))
|
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
|
|
|
location_source = Column(String)
|
|
|
|
location_type = Column(String)
|
|
|
|
location_value = Column(String)
|
|
|
|
progress_percent = Column(Float)
|
|
|
|
content_source_progress_percent = Column(Float)
|
|
|
|
|
|
|
|
|
|
|
|
class KoboStatistics(Base):
|
|
|
|
__tablename__ = 'kobo_statistics'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
kobo_reading_state_id = Column(Integer, ForeignKey('kobo_reading_state.id'))
|
|
|
|
last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
|
|
|
remaining_time_minutes = Column(Integer)
|
|
|
|
spent_reading_minutes = Column(Integer)
|
|
|
|
|
|
|
|
|
|
|
|
# Updates the last_modified timestamp in the KoboReadingState table if any of its children tables are modified.
|
|
|
|
@event.listens_for(Session, 'before_flush')
|
|
|
|
def receive_before_flush(session, flush_context, instances):
|
|
|
|
for change in itertools.chain(session.new, session.dirty):
|
|
|
|
if isinstance(change, (ReadBook, KoboStatistics, KoboBookmark)):
|
|
|
|
if change.kobo_reading_state:
|
|
|
|
change.kobo_reading_state.last_modified = datetime.datetime.utcnow()
|
2020-03-12 23:31:42 +00:00
|
|
|
# Maintain the last_modified bit for the Shelf table.
|
|
|
|
for change in itertools.chain(session.new, session.deleted):
|
|
|
|
if isinstance(change, BookShelf):
|
|
|
|
change.ub_shelf.last_modified = datetime.datetime.utcnow()
|
2020-02-15 22:27:07 +00:00
|
|
|
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Baseclass representing Downloads from calibre-web in app.db
|
2015-08-02 18:59:11 +00:00
|
|
|
class Downloads(Base):
|
2016-04-27 08:35:23 +00:00
|
|
|
__tablename__ = 'downloads'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def __repr__(self):
|
2016-12-23 08:53:39 +00:00
|
|
|
return '<Download %r' % self.book_id
|
|
|
|
|
2019-01-20 16:45:42 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
# Baseclass representing allowed domains for registration
|
|
|
|
class Registration(Base):
|
|
|
|
__tablename__ = 'registration'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
domain = Column(String)
|
2019-12-29 12:54:52 +00:00
|
|
|
allow = Column(Integer)
|
2018-08-24 13:48:09 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2023-01-21 14:23:18 +00:00
|
|
|
return "<Registration('{0}')>".format(self.domain)
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-07-08 01:18:03 +00:00
|
|
|
class RemoteAuthToken(Base):
|
|
|
|
__tablename__ = 'remote_auth_token'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2020-01-20 05:14:53 +00:00
|
|
|
auth_token = Column(String, unique=True)
|
2017-07-08 01:18:03 +00:00
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
verified = Column(Boolean, default=False)
|
|
|
|
expiration = Column(DateTime)
|
2020-01-11 18:10:39 +00:00
|
|
|
token_type = Column(Integer, default=0)
|
2017-07-08 01:18:03 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2024-06-18 18:13:26 +00:00
|
|
|
super().__init__()
|
2019-05-26 09:31:09 +00:00
|
|
|
self.auth_token = (hexlify(os.urandom(4))).decode('utf-8')
|
2017-07-08 01:18:03 +00:00
|
|
|
self.expiration = datetime.datetime.now() + datetime.timedelta(minutes=10) # 10 min from now
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Token %r>' % self.id
|
|
|
|
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
def filename(context):
|
|
|
|
file_format = context.get_current_parameters()['format']
|
|
|
|
if file_format == 'jpeg':
|
|
|
|
return context.get_current_parameters()['uuid'] + '.jpg'
|
|
|
|
else:
|
|
|
|
return context.get_current_parameters()['uuid'] + '.' + file_format
|
|
|
|
|
|
|
|
|
|
|
|
class Thumbnail(Base):
|
|
|
|
__tablename__ = 'thumbnail'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2021-09-25 08:04:38 +00:00
|
|
|
entity_id = Column(Integer)
|
2021-03-20 10:32:50 +00:00
|
|
|
uuid = Column(String, default=lambda: str(uuid.uuid4()), unique=True)
|
|
|
|
format = Column(String, default='jpeg')
|
2021-09-25 08:04:38 +00:00
|
|
|
type = Column(SmallInteger, default=constants.THUMBNAIL_TYPE_COVER)
|
|
|
|
resolution = Column(SmallInteger, default=constants.COVER_THUMBNAIL_SMALL)
|
2021-03-20 10:32:50 +00:00
|
|
|
filename = Column(String, default=filename)
|
|
|
|
generated_at = Column(DateTime, default=lambda: datetime.datetime.utcnow())
|
2021-09-24 08:11:14 +00:00
|
|
|
expiration = Column(DateTime, nullable=True)
|
2021-03-20 10:32:50 +00:00
|
|
|
|
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
# Add missing tables during migration of database
|
2022-02-06 13:22:55 +00:00
|
|
|
def add_missing_tables(engine, _session):
|
2019-02-09 17:46:36 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "book_read_link"):
|
|
|
|
ReadBook.__table__.create(bind=engine)
|
2017-08-23 15:52:52 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "bookmark"):
|
|
|
|
Bookmark.__table__.create(bind=engine)
|
2020-02-15 22:27:07 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "kobo_reading_state"):
|
|
|
|
KoboReadingState.__table__.create(bind=engine)
|
|
|
|
if not engine.dialect.has_table(engine.connect(), "kobo_bookmark"):
|
|
|
|
KoboBookmark.__table__.create(bind=engine)
|
|
|
|
if not engine.dialect.has_table(engine.connect(), "kobo_statistics"):
|
|
|
|
KoboStatistics.__table__.create(bind=engine)
|
2020-01-24 05:04:16 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "archived_book"):
|
|
|
|
ArchivedBook.__table__.create(bind=engine)
|
2021-03-20 10:32:50 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "thumbnail"):
|
|
|
|
Thumbnail.__table__.create(bind=engine)
|
2018-08-24 13:48:09 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "registration"):
|
2020-12-27 09:24:51 +00:00
|
|
|
Registration.__table__.create(bind=engine)
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2020-08-14 17:43:54 +00:00
|
|
|
conn.execute("insert into registration (domain, allow) values('%.%',1)")
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# migrate all settings missing in registration table
|
2022-02-06 13:22:55 +00:00
|
|
|
def migrate_registration_table(engine, _session):
|
2019-12-29 12:54:52 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(Registration.allow)).scalar()
|
|
|
|
_session.commit()
|
2019-12-29 12:54:52 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some columns are missing
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE registration ADD column 'allow' INTEGER"))
|
|
|
|
conn.execute(text("update registration set 'allow' = 1"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2020-01-11 18:10:39 +00:00
|
|
|
try:
|
2021-03-14 12:28:52 +00:00
|
|
|
# Handle table exists, but no content
|
2022-02-06 13:22:55 +00:00
|
|
|
cnt = _session.query(Registration).count()
|
2021-03-14 12:28:52 +00:00
|
|
|
if not cnt:
|
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("insert into registration (domain, allow) values('%.%',1)"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
except exc.OperationalError: # Database is not writeable
|
|
|
|
print('Settings database is not writeable. Exiting...')
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
|
|
|
# Remove login capability of user Guest
|
2021-06-05 16:41:42 +00:00
|
|
|
def migrate_guest_password(engine):
|
2020-02-01 21:45:34 +00:00
|
|
|
try:
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2021-06-05 16:41:42 +00:00
|
|
|
trans = conn.begin()
|
2021-03-21 17:55:02 +00:00
|
|
|
conn.execute(text("UPDATE user SET password='' where name = 'Guest' and password !=''"))
|
2021-06-05 16:41:42 +00:00
|
|
|
trans.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
except exc.OperationalError:
|
|
|
|
print('Settings database is not writeable. Exiting...')
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def migrate_shelfs(engine, _session):
|
2020-03-12 23:31:42 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(Shelf.uuid)).scalar()
|
2020-03-12 23:31:42 +00:00
|
|
|
except exc.OperationalError:
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE shelf ADD column 'uuid' STRING"))
|
|
|
|
conn.execute(text("ALTER TABLE shelf ADD column 'created' DATETIME"))
|
|
|
|
conn.execute(text("ALTER TABLE shelf ADD column 'last_modified' DATETIME"))
|
|
|
|
conn.execute(text("ALTER TABLE book_shelf_link ADD column 'date_added' DATETIME"))
|
|
|
|
conn.execute(text("ALTER TABLE shelf ADD column 'kobo_sync' BOOLEAN DEFAULT false"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2022-02-06 13:22:55 +00:00
|
|
|
for shelf in _session.query(Shelf).all():
|
2020-03-12 23:31:42 +00:00
|
|
|
shelf.uuid = str(uuid.uuid4())
|
|
|
|
shelf.created = datetime.datetime.now()
|
|
|
|
shelf.last_modified = datetime.datetime.now()
|
2022-02-06 13:22:55 +00:00
|
|
|
for book_shelf in _session.query(BookShelf).all():
|
2020-03-12 23:31:42 +00:00
|
|
|
book_shelf.date_added = datetime.datetime.now()
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.commit()
|
2021-02-06 19:29:43 +00:00
|
|
|
|
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(Shelf.kobo_sync)).scalar()
|
2021-02-06 19:29:43 +00:00
|
|
|
except exc.OperationalError:
|
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE shelf ADD column 'kobo_sync' BOOLEAN DEFAULT false"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2017-02-02 18:36:31 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(BookShelf.order)).scalar()
|
2019-12-29 12:54:52 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some columns are missing
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE book_shelf_link ADD column 'order' INTEGER DEFAULT 1"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def migrate_readBook(engine, _session):
|
2021-03-14 12:28:52 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(ReadBook.read_status)).scalar()
|
2021-03-14 12:28:52 +00:00
|
|
|
except exc.OperationalError:
|
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE book_read_link ADD column 'read_status' INTEGER DEFAULT 0"))
|
|
|
|
conn.execute(text("UPDATE book_read_link SET 'read_status' = 1 WHERE is_read"))
|
|
|
|
conn.execute(text("ALTER TABLE book_read_link ADD column 'last_modified' DATETIME"))
|
|
|
|
conn.execute(text("ALTER TABLE book_read_link ADD column 'last_time_started_reading' DATETIME"))
|
|
|
|
conn.execute(text("ALTER TABLE book_read_link ADD column 'times_started_reading' INTEGER DEFAULT 0"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2022-02-06 13:22:55 +00:00
|
|
|
test = _session.query(ReadBook).filter(ReadBook.last_modified == None).all()
|
2021-03-14 12:28:52 +00:00
|
|
|
for book in test:
|
|
|
|
book.last_modified = datetime.datetime.utcnow()
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def migrate_remoteAuthToken(engine, _session):
|
2021-03-14 12:28:52 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(RemoteAuthToken.token_type)).scalar()
|
|
|
|
_session.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some columns are missing
|
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE remote_auth_token ADD column 'token_type' INTEGER DEFAULT 0"))
|
|
|
|
conn.execute(text("update remote_auth_token set 'token_type' = 0"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
# Migrate database to current version, has to be updated after every database change. Currently migration from
|
|
|
|
# everywhere to current should work. Migration is done by checking if relevant columns are existing, and than adding
|
|
|
|
# rows with SQL commands
|
2022-02-06 13:22:55 +00:00
|
|
|
def migrate_Database(_session):
|
|
|
|
engine = _session.bind
|
|
|
|
add_missing_tables(engine, _session)
|
|
|
|
migrate_registration_table(engine, _session)
|
|
|
|
migrate_readBook(engine, _session)
|
|
|
|
migrate_remoteAuthToken(engine, _session)
|
|
|
|
migrate_shelfs(engine, _session)
|
2017-01-28 19:16:40 +00:00
|
|
|
try:
|
|
|
|
create = False
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(User.sidebar_view)).scalar()
|
2019-12-29 12:54:52 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some columns are missing
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE user ADD column `sidebar_view` Integer DEFAULT 1"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2017-11-30 15:49:46 +00:00
|
|
|
create = True
|
2017-01-28 19:16:40 +00:00
|
|
|
try:
|
|
|
|
if create:
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("SELECT language_books FROM user"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2017-01-28 19:16:40 +00:00
|
|
|
except exc.OperationalError:
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("UPDATE user SET 'sidebar_view' = (random_books* :side_random + language_books * :side_lang "
|
2020-04-19 17:08:58 +00:00
|
|
|
"+ series_books * :side_series + category_books * :side_category + hot_books * "
|
2023-02-11 06:43:48 +00:00
|
|
|
":side_hot + :side_autor + :detail_random)"),
|
2020-04-19 17:08:58 +00:00
|
|
|
{'side_random': constants.SIDEBAR_RANDOM, 'side_lang': constants.SIDEBAR_LANGUAGE,
|
|
|
|
'side_series': constants.SIDEBAR_SERIES, 'side_category': constants.SIDEBAR_CATEGORY,
|
|
|
|
'side_hot': constants.SIDEBAR_HOT, 'side_autor': constants.SIDEBAR_AUTHOR,
|
|
|
|
'detail_random': constants.DETAIL_RANDOM})
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2017-07-08 01:18:03 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(User.denied_tags)).scalar()
|
2019-12-30 14:15:07 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some columns are missing
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE user ADD column `denied_tags` String DEFAULT ''"))
|
|
|
|
conn.execute(text("ALTER TABLE user ADD column `allowed_tags` String DEFAULT ''"))
|
|
|
|
conn.execute(text("ALTER TABLE user ADD column `denied_column_value` String DEFAULT ''"))
|
|
|
|
conn.execute(text("ALTER TABLE user ADD column `allowed_column_value` String DEFAULT ''"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2020-06-12 14:15:54 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(User.view_settings)).scalar()
|
2020-06-12 14:15:54 +00:00
|
|
|
except exc.OperationalError:
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2023-02-11 06:43:48 +00:00
|
|
|
conn.execute(text("ALTER TABLE user ADD column `view_settings` VARCHAR(10) DEFAULT '{}'"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2021-05-15 08:45:51 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(User.kobo_only_shelves_sync)).scalar()
|
2021-05-15 08:45:51 +00:00
|
|
|
except exc.OperationalError:
|
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
|
|
|
conn.execute(text("ALTER TABLE user ADD column `kobo_only_shelves_sync` SMALLINT DEFAULT 0"))
|
|
|
|
trans.commit()
|
2019-06-22 08:55:09 +00:00
|
|
|
try:
|
2021-03-21 17:55:02 +00:00
|
|
|
# check if name is in User table instead of nickname
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(exists().where(User.name)).scalar()
|
2019-06-22 08:55:09 +00:00
|
|
|
except exc.OperationalError:
|
|
|
|
# Create new table user_id and copy contents of table user into it
|
2020-08-14 17:43:54 +00:00
|
|
|
with engine.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
trans = conn.begin()
|
2021-03-20 09:09:08 +00:00
|
|
|
conn.execute(text("CREATE TABLE user_id (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
|
2021-03-21 17:55:02 +00:00
|
|
|
"name VARCHAR(64),"
|
2020-04-19 17:08:58 +00:00
|
|
|
"email VARCHAR(120),"
|
|
|
|
"role SMALLINT,"
|
|
|
|
"password VARCHAR,"
|
|
|
|
"kindle_mail VARCHAR(120),"
|
|
|
|
"locale VARCHAR(2),"
|
|
|
|
"sidebar_view INTEGER,"
|
2021-03-21 17:55:02 +00:00
|
|
|
"default_language VARCHAR(3),"
|
|
|
|
"denied_tags VARCHAR,"
|
|
|
|
"allowed_tags VARCHAR,"
|
|
|
|
"denied_column_value VARCHAR,"
|
|
|
|
"allowed_column_value VARCHAR,"
|
2021-05-15 08:45:51 +00:00
|
|
|
"view_settings JSON,"
|
|
|
|
"kobo_only_shelves_sync SMALLINT,"
|
2021-03-21 17:55:02 +00:00
|
|
|
"UNIQUE (name),"
|
2021-03-20 09:09:08 +00:00
|
|
|
"UNIQUE (email))"))
|
2021-03-21 17:55:02 +00:00
|
|
|
conn.execute(text("INSERT INTO user_id(id, name, email, role, password, kindle_mail,locale,"
|
|
|
|
"sidebar_view, default_language, denied_tags, allowed_tags, denied_column_value, "
|
2021-05-15 08:45:51 +00:00
|
|
|
"allowed_column_value, view_settings, kobo_only_shelves_sync)"
|
2019-12-20 05:47:29 +00:00
|
|
|
"SELECT id, nickname, email, role, password, kindle_mail, locale,"
|
2021-03-21 17:55:02 +00:00
|
|
|
"sidebar_view, default_language, denied_tags, allowed_tags, denied_column_value, "
|
2021-05-15 08:45:51 +00:00
|
|
|
"allowed_column_value, view_settings, kobo_only_shelves_sync FROM user"))
|
2020-08-22 07:23:29 +00:00
|
|
|
# delete old user table and rename new user_id table to user:
|
2021-03-20 09:09:08 +00:00
|
|
|
conn.execute(text("DROP TABLE user"))
|
|
|
|
conn.execute(text("ALTER TABLE user_id RENAME TO user"))
|
2023-04-12 16:56:21 +00:00
|
|
|
trans.commit()
|
2022-02-06 13:22:55 +00:00
|
|
|
if _session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() \
|
2021-03-21 17:55:02 +00:00
|
|
|
is None:
|
2022-02-06 13:22:55 +00:00
|
|
|
create_anonymous_user(_session)
|
2021-03-21 17:55:02 +00:00
|
|
|
|
2021-06-05 16:41:42 +00:00
|
|
|
migrate_guest_password(engine)
|
2017-07-08 01:18:03 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def clean_database(_session):
|
2017-07-08 01:18:03 +00:00
|
|
|
# Remove expired remote login tokens
|
|
|
|
now = datetime.datetime.now()
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.query(RemoteAuthToken).filter(now > RemoteAuthToken.expiration).\
|
2020-04-19 17:08:58 +00:00
|
|
|
filter(RemoteAuthToken.token_type != 1).delete()
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.commit()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-07-14 06:31:52 +00:00
|
|
|
# Save downloaded books per user in calibre-web's own database
|
|
|
|
def update_download(book_id, user_id):
|
2020-04-19 17:08:58 +00:00
|
|
|
check = session.query(Downloads).filter(Downloads.user_id == user_id).filter(Downloads.book_id == book_id).first()
|
2018-07-14 06:31:52 +00:00
|
|
|
|
|
|
|
if not check:
|
|
|
|
new_download = Downloads(user_id=user_id, book_id=book_id)
|
2020-12-08 12:56:17 +00:00
|
|
|
session.add(new_download)
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
2020-12-08 12:56:17 +00:00
|
|
|
session.commit()
|
2020-12-07 18:53:34 +00:00
|
|
|
except exc.OperationalError:
|
2020-12-08 12:56:17 +00:00
|
|
|
session.rollback()
|
2018-07-14 06:31:52 +00:00
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2022-07-01 13:26:06 +00:00
|
|
|
# Delete non existing downloaded books in calibre-web's own database
|
2018-07-14 06:31:52 +00:00
|
|
|
def delete_download(book_id):
|
2020-12-08 12:56:17 +00:00
|
|
|
session.query(Downloads).filter(book_id == Downloads.book_id).delete()
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
2020-12-08 12:56:17 +00:00
|
|
|
session.commit()
|
2020-12-07 18:53:34 +00:00
|
|
|
except exc.OperationalError:
|
2020-12-08 12:56:17 +00:00
|
|
|
session.rollback()
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2020-04-13 21:15:44 +00:00
|
|
|
# Generate user Guest (translated text), as anonymous user, no rights
|
2022-02-06 13:22:55 +00:00
|
|
|
def create_anonymous_user(_session):
|
2017-01-12 19:43:36 +00:00
|
|
|
user = User()
|
2021-03-21 17:55:02 +00:00
|
|
|
user.name = "Guest"
|
2017-01-28 19:16:40 +00:00
|
|
|
user.email = 'no@email'
|
2019-06-06 15:10:22 +00:00
|
|
|
user.role = constants.ROLE_ANONYMOUS
|
2018-07-03 17:34:29 +00:00
|
|
|
user.password = ''
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.add(user)
|
2017-01-12 19:43:36 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.commit()
|
2020-04-27 18:01:13 +00:00
|
|
|
except Exception:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.rollback()
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Generate User admin with admin123 password, and access to everything
|
2022-02-06 13:22:55 +00:00
|
|
|
def create_admin_user(_session):
|
2016-04-27 08:35:23 +00:00
|
|
|
user = User()
|
2021-03-21 17:55:02 +00:00
|
|
|
user.name = "admin"
|
2022-09-24 04:46:24 +00:00
|
|
|
user.email = "admin@example.org"
|
2019-06-06 15:10:22 +00:00
|
|
|
user.role = constants.ADMIN_USER_ROLES
|
|
|
|
user.sidebar_view = constants.ADMIN_USER_SIDEBAR
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2019-06-06 15:10:22 +00:00
|
|
|
user.password = generate_password_hash(constants.DEFAULT_PASSWORD)
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.add(user)
|
2016-04-29 20:54:38 +00:00
|
|
|
try:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.commit()
|
2017-03-30 19:17:18 +00:00
|
|
|
except Exception:
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.rollback()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def init_db_thread():
|
2022-01-23 12:11:02 +00:00
|
|
|
global app_DB_path
|
2023-01-21 14:23:18 +00:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(app_DB_path), echo=False)
|
2022-01-23 12:11:02 +00:00
|
|
|
|
|
|
|
Session = scoped_session(sessionmaker())
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
return Session()
|
|
|
|
|
2019-07-07 12:53:38 +00:00
|
|
|
|
2023-02-15 19:17:10 +00:00
|
|
|
def init_db(app_db_path):
|
2019-02-08 19:11:44 +00:00
|
|
|
# Open session for database connection
|
|
|
|
global session
|
2020-05-21 07:28:35 +00:00
|
|
|
global app_DB_path
|
2019-07-07 12:53:38 +00:00
|
|
|
|
2020-05-21 07:28:35 +00:00
|
|
|
app_DB_path = app_db_path
|
2023-01-21 14:23:18 +00:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(app_db_path), echo=False)
|
2019-07-07 12:53:38 +00:00
|
|
|
|
2020-12-08 10:39:23 +00:00
|
|
|
Session = scoped_session(sessionmaker())
|
2019-02-08 19:11:44 +00:00
|
|
|
Session.configure(bind=engine)
|
|
|
|
session = Session()
|
|
|
|
|
2019-07-07 12:53:38 +00:00
|
|
|
if os.path.exists(app_db_path):
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
migrate_Database(session)
|
|
|
|
clean_database(session)
|
2019-02-08 19:11:44 +00:00
|
|
|
else:
|
2016-04-27 08:35:23 +00:00
|
|
|
Base.metadata.create_all(engine)
|
2019-07-07 12:53:38 +00:00
|
|
|
create_admin_user(session)
|
|
|
|
create_anonymous_user(session)
|
|
|
|
|
2023-02-15 19:17:10 +00:00
|
|
|
def password_change(user_credentials=None):
|
2022-04-26 09:04:00 +00:00
|
|
|
if user_credentials:
|
|
|
|
username, password = user_credentials.split(':', 1)
|
2021-03-21 17:55:02 +00:00
|
|
|
user = session.query(User).filter(func.lower(User.name) == username.lower()).first()
|
2021-01-17 08:17:46 +00:00
|
|
|
if user:
|
2021-04-26 17:03:01 +00:00
|
|
|
if not password:
|
|
|
|
print("Empty password is not allowed")
|
|
|
|
sys.exit(4)
|
2023-02-15 18:53:35 +00:00
|
|
|
try:
|
|
|
|
from .helper import valid_password
|
|
|
|
user.password = generate_password_hash(valid_password(password))
|
|
|
|
except Exception:
|
|
|
|
print("Password doesn't comply with password validation rules")
|
|
|
|
sys.exit(4)
|
2021-01-17 08:17:46 +00:00
|
|
|
if session_commit() == "":
|
|
|
|
print("Password for user '{}' changed".format(username))
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print("Failed changing password")
|
|
|
|
sys.exit(3)
|
|
|
|
else:
|
|
|
|
print("Username '{}' not valid, can't change password".format(username))
|
|
|
|
sys.exit(3)
|
|
|
|
|
2019-07-07 12:53:38 +00:00
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
def get_new_session_instance():
|
2023-01-21 14:23:18 +00:00
|
|
|
new_engine = create_engine('sqlite:///{0}'.format(app_DB_path), echo=False)
|
2021-03-20 10:32:50 +00:00
|
|
|
new_session = scoped_session(sessionmaker())
|
|
|
|
new_session.configure(bind=new_engine)
|
|
|
|
|
|
|
|
atexit.register(lambda: new_session.remove() if new_session else True)
|
|
|
|
|
|
|
|
return new_session
|
|
|
|
|
|
|
|
|
2019-07-07 12:53:38 +00:00
|
|
|
def dispose():
|
|
|
|
global session
|
|
|
|
|
2019-07-14 11:43:40 +00:00
|
|
|
old_session = session
|
|
|
|
session = None
|
|
|
|
if old_session:
|
2020-04-19 17:08:58 +00:00
|
|
|
try:
|
|
|
|
old_session.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2019-07-14 11:43:40 +00:00
|
|
|
if old_session.bind:
|
2020-04-19 17:08:58 +00:00
|
|
|
try:
|
|
|
|
old_session.bind.dispose()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def session_commit(success=None, _session=None):
|
|
|
|
s = _session if _session else session
|
2021-01-03 08:53:34 +00:00
|
|
|
try:
|
2022-01-23 12:11:02 +00:00
|
|
|
s.commit()
|
2021-01-03 08:53:34 +00:00
|
|
|
if success:
|
|
|
|
log.info(success)
|
|
|
|
except (exc.OperationalError, exc.InvalidRequestError) as e:
|
2022-01-23 12:11:02 +00:00
|
|
|
s.rollback()
|
2022-03-12 16:14:54 +00:00
|
|
|
log.error_or_exception(e)
|
2021-01-03 08:53:34 +00:00
|
|
|
return ""
|