2015-08-02 18:59:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from sqlalchemy import *
|
2016-11-09 18:24:33 +00:00
|
|
|
from sqlalchemy import exc
|
2015-08-02 18:59:11 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import *
|
2017-01-12 19:43:36 +00:00
|
|
|
from flask_login import AnonymousUserMixin
|
2017-10-01 15:14:20 +00:00
|
|
|
import sys
|
2015-08-02 18:59:11 +00:00
|
|
|
import os
|
2017-01-28 19:16:40 +00:00
|
|
|
import logging
|
2015-08-02 19:23:24 +00:00
|
|
|
from werkzeug.security import generate_password_hash
|
2017-01-12 19:43:36 +00:00
|
|
|
from flask_babel import gettext as _
|
2017-02-20 18:34:37 +00:00
|
|
|
import json
|
2017-07-08 01:18:03 +00:00
|
|
|
import datetime
|
|
|
|
from binascii import hexlify
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-03-25 20:05:44 +00:00
|
|
|
dbpath = os.path.join(os.path.normpath(os.getenv("CALIBRE_DBPATH", os.path.dirname(os.path.realpath(__file__)) + os.sep + ".." + os.sep)), "app.db")
|
2015-08-02 18:59:11 +00:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
ROLE_USER = 0
|
|
|
|
ROLE_ADMIN = 1
|
2016-04-27 08:35:23 +00:00
|
|
|
ROLE_DOWNLOAD = 2
|
2017-01-28 19:16:40 +00:00
|
|
|
ROLE_UPLOAD = 4
|
2016-04-27 08:35:23 +00:00
|
|
|
ROLE_EDIT = 8
|
2016-04-27 14:00:58 +00:00
|
|
|
ROLE_PASSWD = 16
|
2017-01-12 19:43:36 +00:00
|
|
|
ROLE_ANONYMOUS = 32
|
2017-03-19 19:29:35 +00:00
|
|
|
ROLE_EDIT_SHELFS = 64
|
2017-04-14 18:29:11 +00:00
|
|
|
ROLE_DELETE_BOOKS = 128
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
DETAIL_RANDOM = 1
|
|
|
|
SIDEBAR_LANGUAGE = 2
|
|
|
|
SIDEBAR_SERIES = 4
|
|
|
|
SIDEBAR_CATEGORY = 8
|
|
|
|
SIDEBAR_HOT = 16
|
|
|
|
SIDEBAR_RANDOM = 32
|
|
|
|
SIDEBAR_AUTHOR = 64
|
2017-02-04 13:28:18 +00:00
|
|
|
SIDEBAR_BEST_RATED = 128
|
2017-02-19 20:08:22 +00:00
|
|
|
SIDEBAR_READ_AND_UNREAD = 256
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2015-08-02 19:23:24 +00:00
|
|
|
DEFAULT_PASS = "admin123"
|
2017-02-06 15:00:29 +00:00
|
|
|
DEFAULT_PORT = int(os.environ.get("CALIBRE_PORT", 8083))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
DEVELOPMENT = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserBase:
|
2017-10-09 20:36:47 +00:00
|
|
|
@classmethod
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_admin(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_ADMIN == ROLE_ADMIN else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_download(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_DOWNLOAD == ROLE_DOWNLOAD else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_upload(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.role is not None)and(self.role & ROLE_UPLOAD == ROLE_UPLOAD))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def role_edit(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_EDIT == ROLE_EDIT else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 14:00:58 +00:00
|
|
|
def role_passwd(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_PASSWD == ROLE_PASSWD else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-04-27 08:35:23 +00:00
|
|
|
|
2017-01-12 19:43:36 +00:00
|
|
|
def role_anonymous(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_ANONYMOUS == ROLE_ANONYMOUS else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-03-19 19:29:35 +00:00
|
|
|
def role_edit_shelfs(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_EDIT_SHELFS == ROLE_EDIT_SHELFS else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
def role_delete_books(self):
|
|
|
|
return bool((self.role is not None)and(self.role & ROLE_DELETE_BOOKS == ROLE_DELETE_BOOKS))
|
|
|
|
|
2017-04-02 08:27:37 +00:00
|
|
|
@classmethod
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
2017-04-02 08:27:37 +00:00
|
|
|
@classmethod
|
2016-04-27 08:35:23 +00:00
|
|
|
def is_anonymous(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def show_random_books(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_RANDOM == SIDEBAR_RANDOM))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
def show_language(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_LANGUAGE == SIDEBAR_LANGUAGE))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
def show_hot_books(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_HOT == SIDEBAR_HOT))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
def show_series(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_SERIES == SIDEBAR_SERIES))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
def show_category(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_CATEGORY == SIDEBAR_CATEGORY))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def show_author(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_AUTHOR == SIDEBAR_AUTHOR))
|
2017-01-22 15:44:37 +00:00
|
|
|
|
2017-02-04 13:28:18 +00:00
|
|
|
def show_best_rated_books(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_BEST_RATED == SIDEBAR_BEST_RATED))
|
2017-02-04 13:28:18 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
def show_read_and_unread(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & SIDEBAR_READ_AND_UNREAD == SIDEBAR_READ_AND_UNREAD))
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def show_detail_random(self):
|
2017-04-02 08:27:37 +00:00
|
|
|
return bool((self.sidebar_view is not None)and(self.sidebar_view & DETAIL_RANDOM == DETAIL_RANDOM))
|
2017-01-22 20:30:36 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<User %r>' % self.nickname
|
2017-01-22 15:44:37 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Baseclass for Users in Calibre-web, settings which are depending on certain users are stored here. It is derived from
|
|
|
|
# User Base (all access methods are declared there)
|
|
|
|
class User(UserBase, Base):
|
2017-01-12 19:43:36 +00:00
|
|
|
__tablename__ = 'user'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
nickname = Column(String(64), unique=True)
|
|
|
|
email = Column(String(120), unique=True, default="")
|
|
|
|
role = Column(SmallInteger, default=ROLE_USER)
|
|
|
|
password = Column(String)
|
|
|
|
kindle_mail = Column(String(120), default="")
|
|
|
|
shelf = relationship('Shelf', backref='user', lazy='dynamic')
|
|
|
|
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")
|
2017-08-08 16:52:00 +00:00
|
|
|
mature_content = Column(Boolean, default=True)
|
2017-01-12 19:43:36 +00:00
|
|
|
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Class for anonymous user is derived from User base and complets overrides methods and properties for the
|
|
|
|
# anonymous user
|
|
|
|
class Anonymous(AnonymousUserMixin, UserBase):
|
2017-01-12 19:43:36 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
2017-08-13 14:05:45 +00:00
|
|
|
data = session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first() # type: User
|
2017-01-28 19:16:40 +00:00
|
|
|
settings = session.query(Settings).first()
|
2017-01-12 19:43:36 +00:00
|
|
|
self.nickname = data.nickname
|
|
|
|
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
|
2017-08-13 14:05:45 +00:00
|
|
|
self.mature_content = data.mature_content
|
2017-01-22 15:44:37 +00:00
|
|
|
self.anon_browse = settings.config_anonbrowse
|
2017-01-12 19:43:36 +00:00
|
|
|
|
|
|
|
def role_admin(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_anonymous(self):
|
2017-01-22 15:44:37 +00:00
|
|
|
return self.anon_browse
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2017-10-09 20:36:47 +00:00
|
|
|
def is_authenticated(self):
|
|
|
|
return False
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Baseclass representing Shelfs in calibre-web inapp.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)
|
2016-04-27 08:35:23 +00:00
|
|
|
name = Column(String)
|
|
|
|
is_public = Column(Integer, default=0)
|
|
|
|
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 '<Shelf %r>' % self.name
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +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'))
|
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
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
class ReadBook(Base):
|
|
|
|
__tablename__ = 'book_read_link'
|
|
|
|
|
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)
|
2017-02-19 20:08:22 +00:00
|
|
|
is_read = Column(Boolean, unique=False)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
# Baseclass for representing settings in app.db with email server settings and Calibre database settings
|
|
|
|
# (application settings)
|
2015-08-02 19:23:24 +00:00
|
|
|
class Settings(Base):
|
2016-04-27 08:35:23 +00:00
|
|
|
__tablename__ = 'settings'
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
mail_server = Column(String)
|
2017-01-28 19:16:40 +00:00
|
|
|
mail_port = Column(Integer, default=25)
|
|
|
|
mail_use_ssl = Column(SmallInteger, default=0)
|
2016-04-27 08:35:23 +00:00
|
|
|
mail_login = Column(String)
|
|
|
|
mail_password = Column(String)
|
|
|
|
mail_from = Column(String)
|
2017-01-22 15:44:37 +00:00
|
|
|
config_calibre_dir = Column(String)
|
2017-02-06 15:00:29 +00:00
|
|
|
config_port = Column(Integer, default=DEFAULT_PORT)
|
2017-01-28 19:16:40 +00:00
|
|
|
config_calibre_web_title = Column(String, default=u'Calibre-web')
|
|
|
|
config_books_per_page = Column(Integer, default=60)
|
|
|
|
config_random_books = Column(Integer, default=4)
|
|
|
|
config_title_regex = Column(String, default=u'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+')
|
|
|
|
config_log_level = Column(SmallInteger, default=logging.INFO)
|
|
|
|
config_uploading = Column(SmallInteger, default=0)
|
|
|
|
config_anonbrowse = Column(SmallInteger, default=0)
|
|
|
|
config_public_reg = Column(SmallInteger, default=0)
|
2017-02-09 05:46:07 +00:00
|
|
|
config_default_role = Column(SmallInteger, default=0)
|
2017-02-28 23:42:46 +00:00
|
|
|
config_columns_to_ignore = Column(String)
|
2017-02-20 18:34:37 +00:00
|
|
|
config_use_google_drive = Column(Boolean)
|
|
|
|
config_google_drive_client_id = Column(String)
|
|
|
|
config_google_drive_client_secret = Column(String)
|
|
|
|
config_google_drive_folder = Column(String)
|
|
|
|
config_google_drive_calibre_url_base = Column(String)
|
|
|
|
config_google_drive_watch_changes_response = Column(String)
|
2017-02-28 23:53:32 +00:00
|
|
|
config_columns_to_ignore = Column(String)
|
2017-07-08 01:18:03 +00:00
|
|
|
config_remote_login = Column(Boolean)
|
2017-07-08 23:05:20 +00:00
|
|
|
config_use_goodreads = Column(Boolean)
|
|
|
|
config_goodreads_api_key = Column(String)
|
|
|
|
config_goodreads_api_secret = Column(String)
|
2017-08-08 16:52:00 +00:00
|
|
|
config_mature_content_tags = Column(String) # type: str
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def __repr__(self):
|
|
|
|
pass
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-07-08 01:18:03 +00: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):
|
|
|
|
self.auth_token = hexlify(os.urandom(4))
|
|
|
|
self.expiration = datetime.datetime.now() + datetime.timedelta(minutes=10) # 10 min from now
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Token %r>' % self.id
|
|
|
|
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Class holds all application specific settings in calibre-web
|
|
|
|
class Config:
|
|
|
|
def __init__(self):
|
|
|
|
self.config_main_dir = os.path.join(os.path.normpath(os.path.dirname(
|
|
|
|
os.path.realpath(__file__)) + os.sep + ".." + os.sep))
|
|
|
|
self.db_configured = None
|
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
2017-08-08 16:52:00 +00:00
|
|
|
data = session.query(Settings).first() # type: Settings
|
2017-01-28 19:16:40 +00:00
|
|
|
self.config_calibre_dir = data.config_calibre_dir
|
|
|
|
self.config_port = data.config_port
|
|
|
|
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
|
|
|
|
self.config_title_regex = data.config_title_regex
|
|
|
|
self.config_log_level = data.config_log_level
|
|
|
|
self.config_uploading = data.config_uploading
|
|
|
|
self.config_anonbrowse = data.config_anonbrowse
|
|
|
|
self.config_public_reg = data.config_public_reg
|
2017-02-09 05:46:07 +00:00
|
|
|
self.config_default_role = data.config_default_role
|
2017-02-28 23:42:46 +00:00
|
|
|
self.config_columns_to_ignore = data.config_columns_to_ignore
|
2017-02-20 18:34:37 +00:00
|
|
|
self.config_use_google_drive = data.config_use_google_drive
|
|
|
|
self.config_google_drive_client_id = data.config_google_drive_client_id
|
|
|
|
self.config_google_drive_client_secret = data.config_google_drive_client_secret
|
|
|
|
self.config_google_drive_calibre_url_base = data.config_google_drive_calibre_url_base
|
|
|
|
self.config_google_drive_folder = data.config_google_drive_folder
|
|
|
|
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-02-28 23:53:32 +00:00
|
|
|
self.config_columns_to_ignore = data.config_columns_to_ignore
|
2017-04-02 08:42:33 +00: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 01:18:03 +00:00
|
|
|
self.config_remote_login = data.config_remote_login
|
2017-07-08 23:05:20 +00: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
|
2017-08-08 16:52:00 +00:00
|
|
|
self.config_mature_content_tags = data.config_mature_content_tags
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def get_main_dir(self):
|
|
|
|
return self.config_main_dir
|
|
|
|
|
2017-02-09 05:46:07 +00:00
|
|
|
def role_admin(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_ADMIN == ROLE_ADMIN else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def role_download(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_DOWNLOAD == ROLE_DOWNLOAD else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def role_upload(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_UPLOAD == ROLE_UPLOAD else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def role_edit(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_EDIT == ROLE_EDIT else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def role_passwd(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_PASSWD == ROLE_PASSWD else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-03-19 19:29:35 +00:00
|
|
|
def role_edit_shelfs(self):
|
|
|
|
if self.config_default_role is not None:
|
|
|
|
return True if self.config_default_role & ROLE_EDIT_SHELFS == ROLE_EDIT_SHELFS else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
def role_delete_books(self):
|
|
|
|
return bool((self.config_default_role is not None) and
|
|
|
|
(self.config_default_role & ROLE_DELETE_BOOKS == ROLE_DELETE_BOOKS))
|
|
|
|
|
2017-08-08 16:52:00 +00:00
|
|
|
def mature_content_tags(self):
|
2017-10-01 15:14:20 +00:00
|
|
|
if (sys.version_info > (3, 0)): #Python3 str, Python2 unicode
|
|
|
|
lstrip = str.lstrip
|
|
|
|
else:
|
|
|
|
lstrip = unicode.lstrip
|
|
|
|
return list(map(lstrip, self.config_mature_content_tags.split(",")))
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-01-28 19:54:31 +00:00
|
|
|
def get_Log_Level(self):
|
|
|
|
ret_value=""
|
|
|
|
if self.config_log_level == logging.INFO:
|
|
|
|
ret_value='INFO'
|
|
|
|
elif self.config_log_level == logging.DEBUG:
|
|
|
|
ret_value='DEBUG'
|
|
|
|
elif self.config_log_level == logging.WARNING:
|
|
|
|
ret_value='WARNING'
|
|
|
|
elif self.config_log_level == logging.ERROR:
|
|
|
|
ret_value='ERROR'
|
|
|
|
return ret_value
|
2017-01-28 19:16:40 +00: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 18:24:33 +00:00
|
|
|
def migrate_Database():
|
2017-02-19 20:08:22 +00:00
|
|
|
if not engine.dialect.has_table(engine.connect(), "book_read_link"):
|
2017-08-23 15:52:52 +00:00
|
|
|
ReadBook.__table__.create(bind=engine)
|
|
|
|
if not engine.dialect.has_table(engine.connect(), "bookmark"):
|
|
|
|
Bookmark.__table__.create(bind=engine)
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
try:
|
2017-01-28 19:16:40 +00:00
|
|
|
session.query(exists().where(User.locale)).scalar()
|
2016-11-09 18:24:33 +00:00
|
|
|
session.commit()
|
2016-12-23 08:53:39 +00:00
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2016-11-09 18:24:33 +00:00
|
|
|
conn.execute("ALTER TABLE user ADD column locale String(2) DEFAULT 'en'")
|
|
|
|
conn.execute("ALTER TABLE user ADD column default_language String(3) DEFAULT 'all'")
|
|
|
|
session.commit()
|
2016-12-27 15:07:25 +00:00
|
|
|
try:
|
2017-01-22 15:44:37 +00:00
|
|
|
session.query(exists().where(Settings.config_calibre_dir)).scalar()
|
2016-12-27 15:07:25 +00:00
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2017-01-22 15:44:37 +00:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_calibre_dir` String")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_port` INTEGER DEFAULT 8083")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_calibre_web_title` String DEFAULT 'Calibre-web'")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_books_per_page` INTEGER DEFAULT 60")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_random_books` INTEGER DEFAULT 4")
|
2017-01-28 19:16:40 +00:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_title_regex` String DEFAULT "
|
|
|
|
"'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+'")
|
2017-01-31 19:48:01 +00:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_log_level` SmallInteger DEFAULT " + str(logging.INFO))
|
2017-01-22 15:44:37 +00:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_uploading` SmallInteger DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_anonbrowse` SmallInteger DEFAULT 0")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_public_reg` SmallInteger DEFAULT 0")
|
2016-12-27 15:07:25 +00:00
|
|
|
session.commit()
|
2017-02-20 18:34:37 +00: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_client_id` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_google_drive_client_secret` String DEFAULT ''")
|
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_google_drive_calibre_url_base` 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 ''")
|
2017-02-28 23:53:32 +00: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 19:18:37 +00:00
|
|
|
try:
|
|
|
|
session.query(exists().where(Settings.config_default_role)).scalar()
|
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2017-02-09 05:46:07 +00:00
|
|
|
conn.execute("ALTER TABLE Settings ADD column `config_default_role` SmallInteger DEFAULT 0")
|
2016-12-27 15:07:25 +00:00
|
|
|
session.commit()
|
2017-02-02 18:36:31 +00:00
|
|
|
try:
|
|
|
|
session.query(exists().where(BookShelf.order)).scalar()
|
|
|
|
session.commit()
|
|
|
|
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-01-28 19:16:40 +00:00
|
|
|
try:
|
|
|
|
create = False
|
|
|
|
session.query(exists().where(User.sidebar_view)).scalar()
|
|
|
|
session.commit()
|
|
|
|
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()
|
|
|
|
create=True
|
|
|
|
try:
|
|
|
|
if create:
|
2017-07-09 18:15:15 +00:00
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("SELECT language_books FROM user")
|
2017-01-28 19:16:40 +00:00
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError:
|
|
|
|
conn = engine.connect()
|
2017-07-09 18:15:15 +00: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 * "
|
|
|
|
":side_hot + :side_autor + :detail_random)",{'side_random': SIDEBAR_RANDOM,
|
|
|
|
'side_lang': SIDEBAR_LANGUAGE, 'side_series': SIDEBAR_SERIES, 'side_category': SIDEBAR_CATEGORY,
|
|
|
|
'side_hot': SIDEBAR_HOT, 'side_autor': SIDEBAR_AUTHOR, 'detail_random': DETAIL_RANDOM})
|
2017-01-28 19:16:40 +00:00
|
|
|
session.commit()
|
2017-08-08 16:52:00 +00: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")
|
2017-01-28 19:16:40 +00:00
|
|
|
if session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first() is None:
|
|
|
|
create_anonymous_user()
|
2017-07-08 01:18:03 +00: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-08 23:05:20 +00: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 16:52:00 +00: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-07-08 01:18:03 +00:00
|
|
|
|
|
|
|
def clean_database():
|
|
|
|
# Remove expired remote login tokens
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
session.query(RemoteAuthToken).filter(now > RemoteAuthToken.expiration).delete()
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2015-08-02 19:23:24 +00:00
|
|
|
def create_default_config():
|
2016-04-27 08:35:23 +00: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 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
session.add(settings)
|
|
|
|
session.commit()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 19:23:24 +00:00
|
|
|
def get_mail_settings():
|
2016-04-27 08:35:23 +00:00
|
|
|
settings = session.query(Settings).first()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
if not settings:
|
2016-12-23 08:53:39 +00:00
|
|
|
return {}
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
data = {
|
2016-12-23 08:53:39 +00: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 08:35:23 +00:00
|
|
|
}
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
return data
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
# Generate user Guest (translated text), as anoymous user, no rights
|
2017-01-12 19:43:36 +00:00
|
|
|
def create_anonymous_user():
|
|
|
|
user = User()
|
|
|
|
user.nickname = _("Guest")
|
2017-01-28 19:16:40 +00:00
|
|
|
user.email = 'no@email'
|
2017-01-12 19:43:36 +00:00
|
|
|
user.role = ROLE_ANONYMOUS
|
|
|
|
user.password = generate_password_hash('1')
|
|
|
|
|
|
|
|
session.add(user)
|
|
|
|
try:
|
|
|
|
session.commit()
|
2017-03-29 19:43:55 +00:00
|
|
|
except Exception:
|
2017-01-12 19:43:36 +00:00
|
|
|
session.rollback()
|
|
|
|
|
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
|
2015-08-02 19:23:24 +00:00
|
|
|
def create_admin_user():
|
2016-04-27 08:35:23 +00:00
|
|
|
user = User()
|
|
|
|
user.nickname = "admin"
|
2017-04-14 18:29:11 +00:00
|
|
|
user.role = ROLE_USER + ROLE_ADMIN + ROLE_DOWNLOAD + ROLE_UPLOAD + ROLE_EDIT + ROLE_DELETE_BOOKS + ROLE_PASSWD
|
2017-01-28 19:16:40 +00:00
|
|
|
user.sidebar_view = DETAIL_RANDOM + SIDEBAR_LANGUAGE + SIDEBAR_SERIES + SIDEBAR_CATEGORY + SIDEBAR_HOT + \
|
2017-03-03 21:09:53 +00:00
|
|
|
SIDEBAR_RANDOM + SIDEBAR_AUTHOR + SIDEBAR_BEST_RATED + SIDEBAR_READ_AND_UNREAD
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
user.password = generate_password_hash(DEFAULT_PASS)
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
session.add(user)
|
2016-04-29 20:54:38 +00:00
|
|
|
try:
|
|
|
|
session.commit()
|
2017-03-30 19:17:18 +00:00
|
|
|
except Exception:
|
2016-04-29 20:54:38 +00:00
|
|
|
session.rollback()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
# Open session for database connection
|
2015-08-02 18:59:11 +00:00
|
|
|
Session = sessionmaker()
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
session = Session()
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# generate database and admin and guest user, if no database is existing
|
2015-08-02 19:23:24 +00:00
|
|
|
if not os.path.exists(dbpath):
|
2016-04-27 08:35:23 +00:00
|
|
|
try:
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
create_default_config()
|
|
|
|
create_admin_user()
|
2017-01-12 19:43:36 +00:00
|
|
|
create_anonymous_user()
|
2016-04-27 08:35:23 +00:00
|
|
|
except Exception:
|
2017-02-06 14:28:40 +00:00
|
|
|
raise
|
2016-11-09 18:24:33 +00:00
|
|
|
else:
|
2017-07-08 01:18:03 +00:00
|
|
|
Base.metadata.create_all(engine)
|
2016-11-09 18:24:33 +00:00
|
|
|
migrate_Database()
|
2017-07-08 01:18:03 +00:00
|
|
|
clean_database()
|
2017-01-28 19:16:40 +00:00
|
|
|
|
|
|
|
# Generate global Settings Object accecable from every file
|
|
|
|
config = Config()
|