2015-08-02 20:59:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 19:37:45 +01: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/>.
|
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
2017-10-01 17:14:20 +02:00
|
|
|
import sys
|
2015-08-02 20:59:11 +02:00
|
|
|
import os
|
2017-07-08 03:18:03 +02:00
|
|
|
import datetime
|
2019-06-06 17:10:22 +02:00
|
|
|
import json
|
2017-07-08 03:18:03 +02:00
|
|
|
from binascii import hexlify
|
2019-06-06 17:10:22 +02:00
|
|
|
|
2019-03-04 19:34:29 +01:00
|
|
|
from flask import g
|
|
|
|
from flask_babel import gettext as _
|
2019-06-06 17:10:22 +02:00
|
|
|
from flask_login import AnonymousUserMixin
|
2019-02-09 21:26:17 +01:00
|
|
|
try:
|
2019-02-16 07:23:08 +01:00
|
|
|
from flask_dance.consumer.backend.sqla import OAuthConsumerMixin
|
|
|
|
oauth_support = True
|
2019-02-09 21:26:17 +01:00
|
|
|
except ImportError:
|
2019-02-16 07:23:08 +01:00
|
|
|
oauth_support = False
|
2019-06-06 17:10:22 +02:00
|
|
|
from sqlalchemy import create_engine, exc, exists
|
|
|
|
from sqlalchemy import Column, ForeignKey
|
|
|
|
from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime
|
|
|
|
from sqlalchemy.orm import relationship, sessionmaker
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from werkzeug.security import generate_password_hash
|
2019-02-09 21:26:17 +01:00
|
|
|
|
2019-06-16 08:59:25 +02:00
|
|
|
'''try:
|
2019-02-17 09:09:20 +01:00
|
|
|
import ldap
|
|
|
|
except ImportError:
|
2019-06-16 08:59:25 +02:00
|
|
|
pass'''
|
2019-02-08 20:11:44 +01:00
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
from . import constants, logger, cli
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2019-02-17 09:09:20 +01:00
|
|
|
session = None
|
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(cli.settingspath), echo=False)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
2019-03-16 17:33:15 +01:00
|
|
|
def get_sidebar_config(kwargs=None):
|
|
|
|
kwargs = kwargs or []
|
2019-03-05 17:34:51 +01:00
|
|
|
if 'content' in kwargs:
|
2019-04-22 19:11:25 +02:00
|
|
|
if not isinstance(kwargs['content'], Settings):
|
|
|
|
content = not kwargs['content'].role_anonymous()
|
|
|
|
else:
|
|
|
|
content = False
|
2019-03-05 17:34:51 +01:00
|
|
|
else:
|
2019-05-11 20:32:34 +02:00
|
|
|
content = 'conf' in kwargs
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar = list()
|
|
|
|
sidebar.append({"glyph": "glyphicon-book", "text": _('Recently Added'), "link": 'web.index', "id": "new",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_RECENT, 'public': True, "page": "root",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show recent books'), "config_show":True})
|
2019-03-16 15:48:09 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-fire", "text": _('Hot Books'), "link": 'web.books_list', "id": "hot",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_HOT, 'public': True, "page": "hot", "show_text": _('Show hot books'),
|
2019-03-05 17:34:51 +01:00
|
|
|
"config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append(
|
2019-03-16 15:48:09 +01:00
|
|
|
{"glyph": "glyphicon-star", "text": _('Best rated Books'), "link": 'web.books_list', "id": "rated",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_BEST_RATED, 'public': True, "page": "rated",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show best rated books'), "config_show":True})
|
2019-03-16 15:48:09 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-eye-open", "text": _('Read Books'), "link": 'web.books_list', "id": "read",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous), "page": "read",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show read and unread'), "config_show": content})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append(
|
2019-03-16 15:48:09 +01:00
|
|
|
{"glyph": "glyphicon-eye-close", "text": _('Unread Books'), "link": 'web.books_list', "id": "unread",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_READ_AND_UNREAD, 'public': (not g.user.is_anonymous), "page": "unread",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show unread'), "config_show":False})
|
2019-03-16 15:48:09 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-random", "text": _('Discover'), "link": 'web.books_list', "id": "rand",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_RANDOM, 'public': True, "page": "discover",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show random books'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-inbox", "text": _('Categories'), "link": 'web.category_list', "id": "cat",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_CATEGORY, 'public': True, "page": "category",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show category selection'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-bookmark", "text": _('Series'), "link": 'web.series_list', "id": "serie",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_SERIES, 'public': True, "page": "series",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show series selection'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-user", "text": _('Authors'), "link": 'web.author_list', "id": "author",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_AUTHOR, 'public': True, "page": "author",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show author selection'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append(
|
|
|
|
{"glyph": "glyphicon-text-size", "text": _('Publishers'), "link": 'web.publisher_list', "id": "publisher",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_PUBLISHER, 'public': True, "page": "publisher",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show publisher selection'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-flag", "text": _('Languages'), "link": 'web.language_overview', "id": "lang",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_LANGUAGE, 'public': (g.user.filter_language() == 'all'),
|
2019-03-04 19:34:29 +01:00
|
|
|
"page": "language",
|
2019-03-05 17:34:51 +01:00
|
|
|
"show_text": _('Show language selection'), "config_show":True})
|
2019-03-08 16:29:12 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-star-empty", "text": _('Ratings'), "link": 'web.ratings_list', "id": "rate",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_RATING, 'public': True,
|
2019-03-08 16:29:12 +01:00
|
|
|
"page": "rating", "show_text": _('Show ratings selection'), "config_show":True})
|
2019-03-11 18:42:30 +01:00
|
|
|
sidebar.append({"glyph": "glyphicon-file", "text": _('File formats'), "link": 'web.formats_list', "id": "format",
|
2019-06-06 17:10:22 +02:00
|
|
|
"visibility": constants.SIDEBAR_FORMAT, 'public': True,
|
2019-03-11 18:42:30 +01:00
|
|
|
"page": "format", "show_text": _('Show file formats selection'), "config_show":True})
|
2019-03-04 19:34:29 +01:00
|
|
|
return sidebar
|
|
|
|
|
|
|
|
|
2019-03-11 18:42:30 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
class UserBase:
|
2018-08-19 10:14:20 +02:00
|
|
|
|
2017-10-11 00:14:19 +02:00
|
|
|
@property
|
2016-04-27 10:35:23 +02:00
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
def _has_role(self, role_flag):
|
|
|
|
return constants.has_flag(self.role, role_flag)
|
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_admin(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_ADMIN)
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_download(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_DOWNLOAD)
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_upload(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_UPLOAD)
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_edit(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_EDIT)
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 16:00:58 +02:00
|
|
|
def role_passwd(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_PASSWD)
|
2016-04-27 10:35:23 +02:00
|
|
|
|
2017-01-12 20:43:36 +01:00
|
|
|
def role_anonymous(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_ANONYMOUS)
|
2017-01-12 20:43:36 +01:00
|
|
|
|
2017-03-19 20:29:35 +01:00
|
|
|
def role_edit_shelfs(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_EDIT_SHELFS)
|
2017-03-19 20:29:35 +01:00
|
|
|
|
2017-04-14 20:29:11 +02:00
|
|
|
def role_delete_books(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_DELETE_BOOKS)
|
2019-05-19 18:39:34 +02:00
|
|
|
|
|
|
|
def role_viewer(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_VIEWER)
|
2019-05-19 18:39:34 +02:00
|
|
|
|
2017-10-11 00:14:19 +02:00
|
|
|
@property
|
2016-04-27 10:35:23 +02:00
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
2017-10-11 00:14:19 +02:00
|
|
|
@property
|
2016-04-27 10:35:23 +02:00
|
|
|
def is_anonymous(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_id(self):
|
2017-03-05 10:40:39 +01:00
|
|
|
return str(self.id)
|
2016-04-27 10:35:23 +02:00
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
def filter_language(self):
|
|
|
|
return self.default_language
|
|
|
|
|
2019-03-03 19:42:17 +01:00
|
|
|
def check_visibility(self, value):
|
2019-06-06 17:10:22 +02:00
|
|
|
return constants.has_flag(self.sidebar_view, value)
|
2019-03-03 19:42:17 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
def show_detail_random(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self.check_visibility(constants.DETAIL_RANDOM)
|
2017-01-22 21:30:36 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return '<User %r>' % self.nickname
|
2017-01-22 16:44:37 +01:00
|
|
|
|
2019-02-17 09:09:20 +01:00
|
|
|
# Login via LDAP method
|
2019-06-16 08:59:25 +02:00
|
|
|
'''@staticmethod
|
2019-02-17 09:09:20 +01:00
|
|
|
def try_login(username, password,config_dn, ldap_provider_url):
|
|
|
|
conn = get_ldap_connection(ldap_provider_url)
|
2019-01-10 23:51:01 +01:00
|
|
|
conn.simple_bind_s(
|
2019-02-17 09:09:20 +01:00
|
|
|
config_dn.replace("%s", username),
|
2019-06-16 08:59:25 +02:00
|
|
|
password)'''
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2018-08-24 15:48:09 +02:00
|
|
|
# Baseclass for Users in Calibre-Web, settings which are depending on certain users are stored here. It is derived from
|
2017-01-28 20:16:40 +01:00
|
|
|
# User Base (all access methods are declared there)
|
|
|
|
class User(UserBase, Base):
|
2017-01-12 20:43:36 +01:00
|
|
|
__tablename__ = 'user'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
nickname = Column(String(64), unique=True)
|
|
|
|
email = Column(String(120), unique=True, default="")
|
2019-06-06 17:10:22 +02:00
|
|
|
role = Column(SmallInteger, default=constants.ROLE_USER)
|
2017-01-12 20:43:36 +01:00
|
|
|
password = Column(String)
|
|
|
|
kindle_mail = Column(String(120), default="")
|
2017-11-01 16:55:51 +01:00
|
|
|
shelf = relationship('Shelf', backref='user', lazy='dynamic', order_by='Shelf.name')
|
2017-01-12 20:43:36 +01:00
|
|
|
downloads = relationship('Downloads', backref='user', lazy='dynamic')
|
|
|
|
locale = Column(String(2), default="en")
|
2017-01-28 20:16:40 +01:00
|
|
|
sidebar_view = Column(Integer, default=1)
|
2017-01-12 20:43:36 +01:00
|
|
|
default_language = Column(String(3), default="all")
|
2017-08-08 18:52:00 +02:00
|
|
|
mature_content = Column(Boolean, default=True)
|
2017-01-12 20:43:36 +01:00
|
|
|
|
2019-02-16 07:23:08 +01: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 13:52:30 +02:00
|
|
|
|
|
|
|
|
2018-08-19 10:14:20 +02:00
|
|
|
# Class for anonymous user is derived from User base and completly overrides methods and properties for the
|
2017-01-28 20:16:40 +01:00
|
|
|
# anonymous user
|
|
|
|
class Anonymous(AnonymousUserMixin, UserBase):
|
2017-01-12 20:43:36 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
data = session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() # type: User
|
2017-01-28 20:16:40 +01:00
|
|
|
settings = session.query(Settings).first()
|
2017-01-12 20:43:36 +01:00
|
|
|
self.nickname = data.nickname
|
|
|
|
self.role = data.role
|
2017-10-09 22:36:47 +02:00
|
|
|
self.id=data.id
|
2017-01-28 20:16:40 +01:00
|
|
|
self.sidebar_view = data.sidebar_view
|
2017-01-12 20:43:36 +01:00
|
|
|
self.default_language = data.default_language
|
2017-01-14 15:05:49 +01:00
|
|
|
self.locale = data.locale
|
2017-08-13 16:05:45 +02:00
|
|
|
self.mature_content = data.mature_content
|
2017-01-22 16:44:37 +01:00
|
|
|
self.anon_browse = settings.config_anonbrowse
|
2019-04-14 16:37:57 +02:00
|
|
|
self.kindle_mail = data.kindle_mail
|
2017-01-12 20:43:36 +01:00
|
|
|
|
|
|
|
def role_admin(self):
|
|
|
|
return False
|
|
|
|
|
2017-10-10 16:18:28 +02:00
|
|
|
@property
|
2017-01-12 20:43:36 +01:00
|
|
|
def is_active(self):
|
|
|
|
return False
|
|
|
|
|
2017-10-10 16:18:28 +02:00
|
|
|
@property
|
2017-01-12 20:43:36 +01:00
|
|
|
def is_anonymous(self):
|
2017-01-22 16:44:37 +01:00
|
|
|
return self.anon_browse
|
2017-01-12 20:43:36 +01:00
|
|
|
|
2017-10-10 16:18:28 +02:00
|
|
|
@property
|
2017-10-09 22:36:47 +02:00
|
|
|
def is_authenticated(self):
|
|
|
|
return False
|
2017-01-12 20:43:36 +01:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2018-08-19 10:14:20 +02:00
|
|
|
# Baseclass representing Shelfs in calibre-web in app.db
|
2015-08-02 20:59:11 +02:00
|
|
|
class Shelf(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'shelf'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-27 10:35:23 +02:00
|
|
|
name = Column(String)
|
|
|
|
is_public = Column(Integer, default=0)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return '<Shelf %d:%r>' % (self.id, self.name)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
|
2018-08-24 15:48:09 +02:00
|
|
|
# Baseclass representing Relationship between books and Shelfs in Calibre-Web in app.db (N:M)
|
2015-08-02 20:59:11 +02:00
|
|
|
class BookShelf(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'book_shelf_link'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
2016-12-26 11:33:32 +01:00
|
|
|
order = Column(Integer)
|
2016-04-27 10:35:23 +02:00
|
|
|
shelf = Column(Integer, ForeignKey('shelf.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<Book %r>' % self.id
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-08-23 17:52:52 +02:00
|
|
|
|
2017-02-19 21:08:22 +01:00
|
|
|
class ReadBook(Base):
|
|
|
|
__tablename__ = 'book_read_link'
|
|
|
|
|
2017-08-23 17:52:52 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2017-02-19 21:08:22 +01:00
|
|
|
book_id = Column(Integer, unique=False)
|
2017-08-23 17:52:52 +02:00
|
|
|
user_id = Column(Integer, ForeignKey('user.id'), unique=False)
|
2017-02-19 21:08:22 +01:00
|
|
|
is_read = Column(Boolean, unique=False)
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-08-23 17:52:52 +02: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)
|
|
|
|
|
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
# Baseclass representing Downloads from calibre-web in app.db
|
2015-08-02 20:59:11 +02:00
|
|
|
class Downloads(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'downloads'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<Download %r' % self.book_id
|
|
|
|
|
2019-01-20 17:45:42 +01:00
|
|
|
|
2018-08-24 15:48:09 +02:00
|
|
|
# Baseclass representing allowed domains for registration
|
|
|
|
class Registration(Base):
|
|
|
|
__tablename__ = 'registration'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
domain = Column(String)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2019-01-14 20:27:53 +01:00
|
|
|
return u"<Registration('{0}')>".format(self.domain)
|
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
|
|
|
|
# Baseclass for representing settings in app.db with email server settings and Calibre database settings
|
|
|
|
# (application settings)
|
2015-08-02 21:23:24 +02:00
|
|
|
class Settings(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'settings'
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
mail_server = Column(String)
|
2017-01-28 20:16:40 +01:00
|
|
|
mail_port = Column(Integer, default=25)
|
|
|
|
mail_use_ssl = Column(SmallInteger, default=0)
|
2016-04-27 10:35:23 +02:00
|
|
|
mail_login = Column(String)
|
|
|
|
mail_password = Column(String)
|
|
|
|
mail_from = Column(String)
|
2017-01-22 16:44:37 +01:00
|
|
|
config_calibre_dir = Column(String)
|
2019-06-06 17:10:22 +02:00
|
|
|
config_port = Column(Integer, default=constants.DEFAULT_PORT)
|
2018-03-30 21:20:47 +02:00
|
|
|
config_certfile = Column(String)
|
|
|
|
config_keyfile = Column(String)
|
2018-08-24 15:48:09 +02:00
|
|
|
config_calibre_web_title = Column(String, default=u'Calibre-Web')
|
2017-01-28 20:16:40 +01:00
|
|
|
config_books_per_page = Column(Integer, default=60)
|
|
|
|
config_random_books = Column(Integer, default=4)
|
2019-03-09 20:14:16 +01:00
|
|
|
config_authors_max = Column(Integer, default=0)
|
2018-07-15 15:08:06 +02:00
|
|
|
config_read_column = Column(Integer, default=0)
|
2017-01-28 20:16:40 +01:00
|
|
|
config_title_regex = Column(String, default=u'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+')
|
2019-06-06 17:10:22 +02:00
|
|
|
config_log_level = Column(SmallInteger, default=logger.DEFAULT_LOG_LEVEL)
|
2019-06-10 12:18:11 +02:00
|
|
|
config_access_log = Column(SmallInteger, default=0)
|
2017-01-28 20:16:40 +01:00
|
|
|
config_uploading = Column(SmallInteger, default=0)
|
|
|
|
config_anonbrowse = Column(SmallInteger, default=0)
|
|
|
|
config_public_reg = Column(SmallInteger, default=0)
|
2017-02-09 06:46:07 +01:00
|
|
|
config_default_role = Column(SmallInteger, default=0)
|
2018-10-28 14:40:31 +01:00
|
|
|
config_default_show = Column(SmallInteger, default=6143)
|
2017-03-01 00:42:46 +01:00
|
|
|
config_columns_to_ignore = Column(String)
|
2017-02-20 19:34:37 +01:00
|
|
|
config_use_google_drive = Column(Boolean)
|
|
|
|
config_google_drive_folder = Column(String)
|
|
|
|
config_google_drive_watch_changes_response = Column(String)
|
2017-07-08 03:18:03 +02:00
|
|
|
config_remote_login = Column(Boolean)
|
2017-07-09 01:05:20 +02:00
|
|
|
config_use_goodreads = Column(Boolean)
|
|
|
|
config_goodreads_api_key = Column(String)
|
|
|
|
config_goodreads_api_secret = Column(String)
|
2019-02-17 09:09:20 +01:00
|
|
|
config_login_type = Column(Integer, default=0)
|
|
|
|
# config_use_ldap = Column(Boolean)
|
2019-01-12 12:52:27 +01:00
|
|
|
config_ldap_provider_url = Column(String)
|
|
|
|
config_ldap_dn = Column(String)
|
2019-02-17 09:09:20 +01:00
|
|
|
# config_use_github_oauth = Column(Boolean)
|
2018-10-11 13:52:30 +02:00
|
|
|
config_github_oauth_client_id = Column(String)
|
|
|
|
config_github_oauth_client_secret = Column(String)
|
2019-02-17 09:09:20 +01:00
|
|
|
# config_use_google_oauth = Column(Boolean)
|
2018-10-11 13:52:30 +02:00
|
|
|
config_google_oauth_client_id = Column(String)
|
|
|
|
config_google_oauth_client_secret = Column(String)
|
2019-06-02 22:07:22 +02:00
|
|
|
config_ldap_provider_url = Column(String, default='localhost')
|
|
|
|
config_ldap_port = Column(SmallInteger, default=389)
|
|
|
|
config_ldap_schema = Column(String, default='ldap')
|
|
|
|
config_ldap_serv_username = Column(String)
|
|
|
|
config_ldap_serv_password = Column(String)
|
|
|
|
config_ldap_use_ssl = Column(Boolean, default=False)
|
|
|
|
config_ldap_use_tls = Column(Boolean, default=False)
|
|
|
|
config_ldap_require_cert = Column(Boolean, default=False)
|
|
|
|
config_ldap_cert_path = Column(String)
|
2019-01-12 12:52:27 +01:00
|
|
|
config_ldap_dn = Column(String)
|
2019-06-02 22:07:22 +02:00
|
|
|
config_ldap_user_object = Column(String)
|
|
|
|
config_ldap_openldap = Column(Boolean)
|
2018-07-18 20:21:44 +02:00
|
|
|
config_mature_content_tags = Column(String)
|
2017-12-02 11:15:51 +01:00
|
|
|
config_logfile = Column(String)
|
2019-06-10 12:18:11 +02:00
|
|
|
config_access_logfile = Column(String)
|
2018-07-18 20:21:44 +02:00
|
|
|
config_ebookconverter = Column(Integer, default=0)
|
|
|
|
config_converterpath = Column(String)
|
|
|
|
config_calibre = Column(String)
|
2017-11-18 10:34:21 +01:00
|
|
|
config_rarfile_location = Column(String)
|
2019-01-11 08:36:28 +01:00
|
|
|
config_theme = Column(Integer, default=0)
|
2019-01-20 17:45:42 +01:00
|
|
|
config_updatechannel = Column(Integer, default=0)
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
|
|
|
pass
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2017-07-08 03:18:03 +02:00
|
|
|
class RemoteAuthToken(Base):
|
|
|
|
__tablename__ = 'remote_auth_token'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
auth_token = Column(String(8), unique=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
|
|
|
verified = Column(Boolean, default=False)
|
|
|
|
expiration = Column(DateTime)
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-05-26 11:31:09 +02:00
|
|
|
self.auth_token = (hexlify(os.urandom(4))).decode('utf-8')
|
2017-07-08 03:18:03 +02:00
|
|
|
self.expiration = datetime.datetime.now() + datetime.timedelta(minutes=10) # 10 min from now
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Token %r>' % self.id
|
|
|
|
|
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
# Class holds all application specific settings in calibre-web
|
|
|
|
class Config:
|
|
|
|
def __init__(self):
|
|
|
|
self.db_configured = None
|
2017-12-02 11:15:51 +01:00
|
|
|
self.config_logfile = None
|
2017-01-28 20:16:40 +01:00
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
2017-08-08 18:52:00 +02:00
|
|
|
data = session.query(Settings).first() # type: Settings
|
2018-03-30 21:20:47 +02:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
self.config_calibre_dir = data.config_calibre_dir
|
|
|
|
self.config_port = data.config_port
|
2018-03-30 21:20:47 +02:00
|
|
|
self.config_certfile = data.config_certfile
|
|
|
|
self.config_keyfile = data.config_keyfile
|
2017-01-28 20:16:40 +01:00
|
|
|
self.config_calibre_web_title = data.config_calibre_web_title
|
|
|
|
self.config_books_per_page = data.config_books_per_page
|
|
|
|
self.config_random_books = data.config_random_books
|
2019-02-25 19:21:49 +01:00
|
|
|
self.config_authors_max = data.config_authors_max
|
2017-01-28 20:16:40 +01:00
|
|
|
self.config_title_regex = data.config_title_regex
|
2018-07-15 15:08:06 +02:00
|
|
|
self.config_read_column = data.config_read_column
|
2017-01-28 20:16:40 +01:00
|
|
|
self.config_log_level = data.config_log_level
|
2019-06-10 12:18:11 +02:00
|
|
|
self.config_access_log = data.config_access_log
|
2017-01-28 20:16:40 +01:00
|
|
|
self.config_uploading = data.config_uploading
|
|
|
|
self.config_anonbrowse = data.config_anonbrowse
|
|
|
|
self.config_public_reg = data.config_public_reg
|
2017-02-09 06:46:07 +01:00
|
|
|
self.config_default_role = data.config_default_role
|
2017-11-30 20:12:13 +01:00
|
|
|
self.config_default_show = data.config_default_show
|
2017-03-01 00:42:46 +01:00
|
|
|
self.config_columns_to_ignore = data.config_columns_to_ignore
|
2017-02-20 19:34:37 +01:00
|
|
|
self.config_use_google_drive = data.config_use_google_drive
|
|
|
|
self.config_google_drive_folder = data.config_google_drive_folder
|
2018-07-18 20:21:44 +02:00
|
|
|
self.config_ebookconverter = data.config_ebookconverter
|
|
|
|
self.config_converterpath = data.config_converterpath
|
|
|
|
self.config_calibre = data.config_calibre
|
2017-02-20 19:34:37 +01:00
|
|
|
if data.config_google_drive_watch_changes_response:
|
|
|
|
self.config_google_drive_watch_changes_response = json.loads(data.config_google_drive_watch_changes_response)
|
|
|
|
else:
|
|
|
|
self.config_google_drive_watch_changes_response=None
|
2017-03-01 00:53:32 +01:00
|
|
|
self.config_columns_to_ignore = data.config_columns_to_ignore
|
2017-04-02 10:42:33 +02:00
|
|
|
self.db_configured = bool(self.config_calibre_dir is not None and
|
|
|
|
(not self.config_use_google_drive or os.path.exists(self.config_calibre_dir + '/metadata.db')))
|
2017-07-08 03:18:03 +02:00
|
|
|
self.config_remote_login = data.config_remote_login
|
2017-07-09 01:05:20 +02:00
|
|
|
self.config_use_goodreads = data.config_use_goodreads
|
|
|
|
self.config_goodreads_api_key = data.config_goodreads_api_key
|
|
|
|
self.config_goodreads_api_secret = data.config_goodreads_api_secret
|
2019-02-17 09:09:20 +01:00
|
|
|
self.config_login_type = data.config_login_type
|
|
|
|
# self.config_use_ldap = data.config_use_ldap
|
2019-06-16 08:20:01 +02:00
|
|
|
self.config_ldap_user_object = data.config_ldap_user_object
|
|
|
|
self.config_ldap_openldap = data.config_ldap_openldap
|
2019-01-12 12:52:27 +01:00
|
|
|
self.config_ldap_provider_url = data.config_ldap_provider_url
|
2019-06-02 22:07:22 +02:00
|
|
|
self.config_ldap_port = data.config_ldap_port
|
|
|
|
self.config_ldap_schema = data.config_ldap_schema
|
|
|
|
self.config_ldap_serv_username = data.config_ldap_serv_username
|
|
|
|
self.config_ldap_serv_password = data.config_ldap_serv_password
|
|
|
|
self.config_ldap_use_ssl = data.config_ldap_use_ssl
|
|
|
|
self.config_ldap_use_tls = data.config_ldap_use_ssl
|
|
|
|
self.config_ldap_require_cert = data.config_ldap_require_cert
|
|
|
|
self.config_ldap_cert_path = data.config_ldap_cert_path
|
2019-01-12 12:52:27 +01:00
|
|
|
self.config_ldap_dn = data.config_ldap_dn
|
2019-02-17 09:09:20 +01:00
|
|
|
# self.config_use_github_oauth = data.config_use_github_oauth
|
2018-10-11 13:52:30 +02:00
|
|
|
self.config_github_oauth_client_id = data.config_github_oauth_client_id
|
|
|
|
self.config_github_oauth_client_secret = data.config_github_oauth_client_secret
|
2019-02-17 09:09:20 +01:00
|
|
|
# self.config_use_google_oauth = data.config_use_google_oauth
|
2018-10-11 13:52:30 +02:00
|
|
|
self.config_google_oauth_client_id = data.config_google_oauth_client_id
|
|
|
|
self.config_google_oauth_client_secret = data.config_google_oauth_client_secret
|
2019-06-06 17:10:22 +02:00
|
|
|
self.config_mature_content_tags = data.config_mature_content_tags or u''
|
|
|
|
self.config_logfile = data.config_logfile or u''
|
2019-06-10 12:18:11 +02:00
|
|
|
self.config_access_logfile = data.config_access_logfile or u''
|
2017-11-18 10:34:21 +01:00
|
|
|
self.config_rarfile_location = data.config_rarfile_location
|
2019-01-11 08:36:28 +01:00
|
|
|
self.config_theme = data.config_theme
|
2019-01-20 17:45:42 +01:00
|
|
|
self.config_updatechannel = data.config_updatechannel
|
2019-06-11 18:35:20 +02:00
|
|
|
logger.setup(self.config_logfile, self.config_log_level)
|
2017-01-28 20:16:40 +01:00
|
|
|
|
2019-01-20 17:45:42 +01:00
|
|
|
@property
|
|
|
|
def get_update_channel(self):
|
|
|
|
return self.config_updatechannel
|
|
|
|
|
2018-03-30 21:20:47 +02:00
|
|
|
def get_config_certfile(self):
|
|
|
|
if cli.certfilepath:
|
|
|
|
return cli.certfilepath
|
|
|
|
else:
|
|
|
|
if cli.certfilepath is "":
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self.config_certfile
|
|
|
|
|
|
|
|
def get_config_keyfile(self):
|
|
|
|
if cli.keyfilepath:
|
|
|
|
return cli.keyfilepath
|
|
|
|
else:
|
|
|
|
if cli.certfilepath is "":
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self.config_keyfile
|
|
|
|
|
2019-06-10 12:18:11 +02:00
|
|
|
def get_config_ipaddress(self, readable=False):
|
|
|
|
if not readable:
|
|
|
|
if cli.ipadress:
|
|
|
|
return cli.ipadress
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
answer="0.0.0.0"
|
|
|
|
if cli.ipadress:
|
|
|
|
if cli.ipv6:
|
|
|
|
answer = "["+cli.ipadress+"]"
|
|
|
|
else:
|
|
|
|
answer = cli.ipadress
|
|
|
|
return answer
|
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
def _has_role(self, role_flag):
|
|
|
|
return constants.has_flag(self.config_default_role, role_flag)
|
2017-12-02 11:15:51 +01:00
|
|
|
|
2017-02-09 06:46:07 +01:00
|
|
|
def role_admin(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_ADMIN)
|
2017-02-09 06:46:07 +01:00
|
|
|
|
|
|
|
def role_download(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_DOWNLOAD)
|
2017-02-09 06:46:07 +01:00
|
|
|
|
2019-05-19 18:39:34 +02:00
|
|
|
def role_viewer(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_VIEWER)
|
2019-05-19 18:39:34 +02:00
|
|
|
|
2017-02-09 06:46:07 +01:00
|
|
|
def role_upload(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_UPLOAD)
|
2017-02-09 06:46:07 +01:00
|
|
|
|
|
|
|
def role_edit(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_EDIT)
|
2017-02-09 06:46:07 +01:00
|
|
|
|
|
|
|
def role_passwd(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_PASSWD)
|
2017-02-09 06:46:07 +01:00
|
|
|
|
2017-03-19 20:29:35 +01:00
|
|
|
def role_edit_shelfs(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_EDIT_SHELFS)
|
2017-03-19 20:29:35 +01:00
|
|
|
|
2017-04-14 20:29:11 +02:00
|
|
|
def role_delete_books(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self._has_role(constants.ROLE_DELETE_BOOKS)
|
2017-11-30 20:12:13 +01:00
|
|
|
|
2019-03-04 19:34:29 +01:00
|
|
|
def show_element_new_user(self, value):
|
2019-06-06 17:10:22 +02:00
|
|
|
return constants.has_flag(self.config_default_show, value)
|
|
|
|
|
|
|
|
def show_detail_random(self):
|
|
|
|
return self.show_element_new_user(constants.DETAIL_RANDOM)
|
2019-03-03 19:42:17 +01:00
|
|
|
|
2019-03-05 17:34:51 +01:00
|
|
|
def show_mature_content(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return self.show_element_new_user(constants.MATURE_CONTENT)
|
2018-03-27 20:18:11 +02:00
|
|
|
|
2017-08-08 18:52:00 +02:00
|
|
|
def mature_content_tags(self):
|
2017-11-30 16:49:46 +01:00
|
|
|
if sys.version_info > (3, 0): # Python3 str, Python2 unicode
|
2017-10-01 17:14:20 +02:00
|
|
|
lstrip = str.lstrip
|
|
|
|
else:
|
|
|
|
lstrip = unicode.lstrip
|
|
|
|
return list(map(lstrip, self.config_mature_content_tags.split(",")))
|
2017-04-14 20:29:11 +02:00
|
|
|
|
2017-01-28 20:54:31 +01:00
|
|
|
def get_Log_Level(self):
|
2019-06-06 17:10:22 +02:00
|
|
|
return logger.get_level_name(self.config_log_level)
|
2017-01-28 20:16:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Migrate database to current version, has to be updated after every database change. Currently migration from
|
|
|
|
# everywhere to curent should work. Migration is done by checking if relevant coloums are existing, and than adding
|
|
|
|
# rows with SQL commands
|
2016-11-09 19:24:33 +01:00
|
|
|
def migrate_Database():
|
2019-02-09 18:46:36 +01:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "book_read_link"):
|
|
|
|
ReadBook.__table__.create(bind=engine)
|
2017-08-23 17:52:52 +02:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "bookmark"):
|
|
|
|
Bookmark.__table__.create(bind=engine)
|
2018-08-24 15:48:09 +02:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "registration"):
|
|
|
|
ReadBook.__table__.create(bind=engine)
|
2016-12-23 09:53:39 +01:00
|
|
|
conn = engine.connect()
|
2018-08-24 15:48:09 +02:00
|
|
|
conn.execute("insert into registration (domain) values('%.%')")
|
2016-12-27 16:07:25 +01:00
|
|
|
session.commit()
|
2018-08-24 15:48:09 +02:00
|
|
|
# Handle table exists, but no content
|
|
|
|
cnt = session.query(Registration).count()
|
|
|
|
if not cnt:
|
2016-12-27 16:07:25 +01:00
|
|
|
conn = engine.connect()
|
2018-08-24 15:48:09 +02:00
|
|
|
conn.execute("insert into registration (domain) values('%.%')")
|
2016-12-27 16:07:25 +01:00
|
|
|
session.commit()
|
2017-02-20 19:34:37 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_use_google_drive)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_use_google_drive` INTEGER DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_google_drive_folder` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_google_drive_watch_changes_response` String DEFAULT ''")
|
2019-01-14 20:27:53 +01:00
|
|
|
session.commit()
|
2017-03-01 00:53:32 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_columns_to_ignore)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_columns_to_ignore` String DEFAULT ''")
|
|
|
|
session.commit()
|
2017-02-10 20:18:37 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_default_role)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2017-02-09 06:46:07 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_default_role` SmallInteger DEFAULT 0")
|
2016-12-27 16:07:25 +01:00
|
|
|
session.commit()
|
2019-02-25 19:21:49 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_authors_max)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2019-03-09 20:14:16 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_authors_max` INTEGER DEFAULT 0")
|
2019-02-25 19:21:49 +01:00
|
|
|
session.commit()
|
2017-02-02 19:36:31 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(BookShelf.order)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE book_shelf_link ADD column 'order' INTEGER DEFAULT 1")
|
|
|
|
session.commit()
|
2017-11-18 10:34:21 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_rarfile_location)).scalar()
|
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_rarfile_location` String DEFAULT ''")
|
|
|
|
session.commit()
|
2017-01-28 20:16:40 +01:00
|
|
|
try:
|
|
|
|
create = False
|
|
|
|
session.query(exists().where(User.sidebar_view)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE user ADD column `sidebar_view` Integer DEFAULT 1")
|
|
|
|
session.commit()
|
2017-11-30 16:49:46 +01:00
|
|
|
create = True
|
2017-01-28 20:16:40 +01:00
|
|
|
try:
|
|
|
|
if create:
|
2017-07-09 20:15:15 +02:00
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("SELECT language_books FROM user")
|
2017-01-28 20:16:40 +01:00
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2017-07-09 20:15:15 +02:00
|
|
|
conn.execute("UPDATE user SET 'sidebar_view' = (random_books* :side_random + language_books * :side_lang "
|
|
|
|
"+ series_books * :side_series + category_books * :side_category + hot_books * "
|
2017-11-12 14:06:33 +01:00
|
|
|
":side_hot + :side_autor + :detail_random)"
|
2019-06-10 12:18:11 +02: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,
|
2019-06-06 17:10:22 +02:00
|
|
|
'detail_random': constants.DETAIL_RANDOM})
|
2017-01-28 20:16:40 +01:00
|
|
|
session.commit()
|
2017-08-08 18:52:00 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(User.mature_content)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE user ADD column `mature_content` INTEGER DEFAULT 1")
|
2019-01-11 08:36:28 +01:00
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() is None:
|
2017-01-28 20:16:40 +01:00
|
|
|
create_anonymous_user()
|
2017-07-08 03:18:03 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_remote_login)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_remote_login` INTEGER DEFAULT 0")
|
2017-07-09 01:05:20 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_use_goodreads)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_use_goodreads` INTEGER DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_goodreads_api_key` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_goodreads_api_secret` String DEFAULT ''")
|
2017-08-08 18:52:00 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_mature_content_tags)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_mature_content_tags` String DEFAULT ''")
|
2017-11-30 20:12:13 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_default_show)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_default_show` SmallInteger DEFAULT 2047")
|
|
|
|
session.commit()
|
2017-12-02 11:15:51 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_logfile)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_logfile` String DEFAULT ''")
|
|
|
|
session.commit()
|
2018-03-30 21:20:47 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_certfile)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_certfile` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_keyfile` String DEFAULT ''")
|
|
|
|
session.commit()
|
2018-07-15 15:08:06 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_read_column)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_read_column` INTEGER DEFAULT 0")
|
|
|
|
session.commit()
|
2018-07-18 20:21:44 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ebookconverter)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ebookconverter` INTEGER DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_converterpath` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_calibre` String DEFAULT ''")
|
|
|
|
session.commit()
|
2019-01-12 12:52:27 +01:00
|
|
|
try:
|
2019-02-17 09:09:20 +01:00
|
|
|
session.query(exists().where(Settings.config_login_type)).scalar()
|
2019-01-12 12:52:27 +01:00
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-02-17 09:09:20 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_login_type` INTEGER DEFAULT 0")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_provider_url)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-01-12 12:52:27 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_provider_url` String DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_port)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_port` INTEGER DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_schema)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-14 22:55:17 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_schema` String DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_serv_username)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_serv_username` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_serv_password` String DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_use_ssl)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_use_ssl` INTEGER DEFAULT 0")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_use_tls)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-14 22:55:17 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_use_tls` INTEGER DEFAULT 0")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_require_cert)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_require_cert` INTEGER DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_cert_path` String DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_dn)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-01-12 12:52:27 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_dn` String DEFAULT ''")
|
2019-02-17 09:09:20 +01:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_github_oauth_client_id` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_github_oauth_client_secret` String DEFAULT ''")
|
2019-01-12 12:52:27 +01:00
|
|
|
session.commit()
|
2019-06-04 00:47:49 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_user_object)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_user_object` String DEFAULT ''")
|
2019-06-04 00:47:49 +02:00
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_ldap_openldap)).scalar()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2019-06-02 22:07:22 +02:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_ldap_openldap` INTEGER DEFAULT 0")
|
2019-01-12 12:52:27 +01:00
|
|
|
session.commit()
|
2019-01-11 08:36:28 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_theme)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_theme` INTEGER DEFAULT 0")
|
|
|
|
session.commit()
|
2019-01-20 17:45:42 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_updatechannel)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_updatechannel` INTEGER DEFAULT 0")
|
|
|
|
session.commit()
|
2019-06-10 12:18:11 +02:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_access_log)).scalar()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_access_log` INTEGER DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_access_logfile` String DEFAULT ''")
|
|
|
|
session.commit()
|
2019-01-11 08:36:28 +01:00
|
|
|
|
2018-07-03 19:34:29 +02:00
|
|
|
# Remove login capability of user Guest
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("UPDATE user SET password='' where nickname = 'Guest' and password !=''")
|
|
|
|
session.commit()
|
2017-07-08 03:18:03 +02:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2017-07-08 03:18:03 +02:00
|
|
|
def clean_database():
|
|
|
|
# Remove expired remote login tokens
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
session.query(RemoteAuthToken).filter(now > RemoteAuthToken.expiration).delete()
|
2016-11-09 19:24:33 +01:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2019-06-16 08:59:25 +02:00
|
|
|
'''#get LDAP connection
|
2019-02-17 09:09:20 +01:00
|
|
|
def get_ldap_connection(ldap_provider_url):
|
|
|
|
conn = ldap.initialize('ldap://{}'.format(ldap_provider_url))
|
2019-06-16 08:59:25 +02:00
|
|
|
return conn'''
|
2019-02-09 21:26:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
def create_default_config():
|
2016-04-27 10:35:23 +02:00
|
|
|
settings = Settings()
|
|
|
|
settings.mail_server = "mail.example.com"
|
|
|
|
settings.mail_port = 25
|
|
|
|
settings.mail_use_ssl = 0
|
|
|
|
settings.mail_login = "mail@example.com"
|
|
|
|
settings.mail_password = "mypassword"
|
|
|
|
settings.mail_from = "automailer <mail@example.com>"
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
session.add(settings)
|
|
|
|
session.commit()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
def get_mail_settings():
|
2016-04-27 10:35:23 +02:00
|
|
|
settings = session.query(Settings).first()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
if not settings:
|
2016-12-23 09:53:39 +01:00
|
|
|
return {}
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
data = {
|
2016-12-23 09:53:39 +01:00
|
|
|
'mail_server': settings.mail_server,
|
|
|
|
'mail_port': settings.mail_port,
|
|
|
|
'mail_use_ssl': settings.mail_use_ssl,
|
|
|
|
'mail_login': settings.mail_login,
|
|
|
|
'mail_password': settings.mail_password,
|
|
|
|
'mail_from': settings.mail_from
|
2016-04-27 10:35:23 +02:00
|
|
|
}
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
return data
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2018-07-14 08:31:52 +02:00
|
|
|
# Save downloaded books per user in calibre-web's own database
|
|
|
|
def update_download(book_id, user_id):
|
|
|
|
check = session.query(Downloads).filter(Downloads.user_id == user_id).filter(Downloads.book_id ==
|
|
|
|
book_id).first()
|
|
|
|
|
|
|
|
if not check:
|
|
|
|
new_download = Downloads(user_id=user_id, book_id=book_id)
|
|
|
|
session.add(new_download)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# Delete non exisiting downloaded books in calibre-web's own database
|
|
|
|
def delete_download(book_id):
|
|
|
|
session.query(Downloads).filter(book_id == Downloads.book_id).delete()
|
|
|
|
session.commit()
|
2017-01-28 20:16:40 +01:00
|
|
|
|
|
|
|
# Generate user Guest (translated text), as anoymous user, no rights
|
2019-02-16 07:23:08 +01:00
|
|
|
def create_anonymous_user():
|
2017-01-12 20:43:36 +01:00
|
|
|
user = User()
|
2018-07-03 19:34:29 +02:00
|
|
|
user.nickname = "Guest"
|
2017-01-28 20:16:40 +01:00
|
|
|
user.email = 'no@email'
|
2019-06-06 17:10:22 +02:00
|
|
|
user.role = constants.ROLE_ANONYMOUS
|
2018-07-03 19:34:29 +02:00
|
|
|
user.password = ''
|
2017-01-12 20:43:36 +01:00
|
|
|
|
|
|
|
session.add(user)
|
|
|
|
try:
|
|
|
|
session.commit()
|
2017-03-29 21:43:55 +02:00
|
|
|
except Exception:
|
2017-01-12 20:43:36 +01:00
|
|
|
session.rollback()
|
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
# Generate User admin with admin123 password, and access to everything
|
2015-08-02 21:23:24 +02:00
|
|
|
def create_admin_user():
|
2016-04-27 10:35:23 +02:00
|
|
|
user = User()
|
|
|
|
user.nickname = "admin"
|
2019-06-06 17:10:22 +02:00
|
|
|
user.role = constants.ADMIN_USER_ROLES
|
|
|
|
user.sidebar_view = constants.ADMIN_USER_SIDEBAR
|
2017-01-28 20:16:40 +01:00
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
user.password = generate_password_hash(constants.DEFAULT_PASSWORD)
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
session.add(user)
|
2016-04-29 22:54:38 +02:00
|
|
|
try:
|
|
|
|
session.commit()
|
2017-03-30 21:17:18 +02:00
|
|
|
except Exception:
|
2016-04-29 22:54:38 +02:00
|
|
|
session.rollback()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2019-02-08 20:11:44 +01:00
|
|
|
def init_db():
|
|
|
|
# Open session for database connection
|
|
|
|
global session
|
|
|
|
Session = sessionmaker()
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
session = Session()
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(cli.settingspath):
|
|
|
|
try:
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
create_default_config()
|
|
|
|
create_admin_user()
|
|
|
|
create_anonymous_user()
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
else:
|
2016-04-27 10:35:23 +02:00
|
|
|
Base.metadata.create_all(engine)
|
2019-02-08 20:11:44 +01:00
|
|
|
migrate_Database()
|
|
|
|
clean_database()
|