Finalize graphical setup for calibre-web

This commit is contained in:
OzzieIsaacs 2017-01-28 20:16:40 +01:00
parent 75c89c28e1
commit 2c615fdf05
11 changed files with 508 additions and 325 deletions

18
cps.py
View File

@ -3,27 +3,25 @@ import sys
import time
base_path = os.path.dirname(os.path.abspath(__file__))
# Insert local directories into path
sys.path.insert(0, os.path.join(base_path, 'vendor'))
from cps import web
# from cps import config
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
if __name__ == '__main__':
'''if config.DEVELOPMENT:
web.app.run(host="0.0.0.0", port=web.config.config_port, debug=True)
else:'''
http_server = HTTPServer(WSGIContainer(web.app))
http_server.listen(web.config.config_port)
IOLoop.instance().start()
if web.ub.DEVELOPMENT:
web.app.run(host="0.0.0.0", port=web.ub.config.config_port, debug=True)
else:
http_server = HTTPServer(WSGIContainer(web.app))
http_server.listen(web.ub.config.config_port)
IOLoop.instance().start()
if web.global_task == 0:
print("Performing restart of Calibre-web")
os.execl(sys.executable,sys.executable, *sys.argv)
os.execl(sys.executable, sys.executable, *sys.argv)
else:
print("Performing shutdown of Calibre-web")
os._exit(0)
sys.exit(0)

View File

@ -7,12 +7,21 @@ from sqlalchemy.orm import *
import os
import re
import ast
from ub import Config
from ub import config
import ub
session = None
cc_exceptions = None
cc_classes = None
cc_ids = None
books_custom_column_links = None
engine = None
# user defined sort function for calibre databases (Series, etc.)
def title_sort(title):
# calibre sort stuff
config=Config()
# config=Config()
title_pat = re.compile(config.config_title_regex, re.IGNORECASE)
match = title_pat.search(title)
if match:
@ -216,9 +225,10 @@ class Books(Base):
series = relationship('Series', secondary=books_series_link, backref='books')
ratings = relationship('Ratings', secondary=books_ratings_link, backref='books')
languages = relationship('Languages', secondary=books_languages_link, backref='books')
identifiers=relationship('Identifiers', backref='books')
identifiers = relationship('Identifiers', backref='books')
def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover, authors, tags):
def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover,
authors, tags): # ToDO check Authors and tags necessary
self.title = title
self.sort = sort
self.author_sort = author_sort
@ -253,19 +263,33 @@ class Custom_Columns(Base):
return display_dict
def setup_db(config):
def setup_db():
global session
global cc_exceptions
global cc_classes
global cc_ids
global books_custom_column_links
global engine
if config.config_calibre_dir is None:
return
if config.config_calibre_dir is None or config.config_calibre_dir == u'':
return False
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
engine = create_engine('sqlite:///{0}'.format(dbpath.encode('utf-8')), echo=False)
conn = engine.connect()
try:
conn = engine.connect()
except:
content = ub.session.query(ub.Settings).first()
content.config_calibre_dir = None
content.db_configured = False
ub.session.commit()
config.loadSettings()
return False
content = ub.session.query(ub.Settings).first()
content.db_configured = True
ub.session.commit()
config.loadSettings()
conn.connection.create_function('title_sort', 1, title_sort)
cc = conn.execute("SELECT id, datatype FROM custom_columns")
@ -310,3 +334,4 @@ def setup_db(config):
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
return True

View File

@ -3,7 +3,7 @@ from lxml import etree
import os
import uploader
# ToDo: Check usage of original_file_name
def get_fb2_info(tmp_file_path, original_file_name, original_file_extension):
ns = {

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import db, ub
# import config
import db
import ub
from flask import current_app as app
import logging
import smtplib
@ -33,8 +33,9 @@ def update_download(book_id, user_id):
ub.session.commit()
def make_mobi(book_id,calibrepath):
vendorpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) + os.sep + "../vendor" + os.sep))
def make_mobi(book_id, calibrepath):
vendorpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) +
os.sep + "../vendor" + os.sep))
if sys.platform == "win32":
kindlegen = os.path.join(vendorpath, u"kindlegen.exe")
else:
@ -80,18 +81,20 @@ def make_mobi(book_id,calibrepath):
class StderrLogger(object):
buffer=''
buffer = ''
def __init__(self):
self.logger = logging.getLogger('cps.web')
def write(self, message):
if message=='\n':
if message == '\n':
self.logger.debug(self.buffer)
self.buffer=''
self.buffer = ''
else:
self.buffer=self.buffer+message
self.buffer += message
def send_raw_email(kindle_mail,msg):
def send_raw_email(kindle_mail, msg):
settings = ub.get_mail_settings()
msg['From'] = settings["mail_from"]
@ -107,7 +110,7 @@ def send_raw_email(kindle_mail,msg):
# send email
try:
timeout=600 # set timeout to 5mins
timeout = 600 # set timeout to 5mins
org_stderr = smtplib.stderr
smtplib.stderr = StderrLogger()
@ -140,18 +143,11 @@ def send_test_mail(kindle_mail):
msg['Subject'] = _(u'Calibre-web test email')
text = _(u'This email has been sent via calibre web.')
msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8'))
return send_raw_email(kindle_mail,msg)
return send_raw_email(kindle_mail, msg)
def send_mail(book_id, kindle_mail,calibrepath):
def send_mail(book_id, kindle_mail, calibrepath):
"""Send email with attachments"""
is_mobi = False
is_azw = False
is_azw3 = False
is_epub = False
is_pdf = False
file_path = None
settings = ub.get_mail_settings()
# create MIME message
msg = MIMEMultipart()
msg['Subject'] = _(u'Send to Kindle')
@ -177,7 +173,7 @@ def send_mail(book_id, kindle_mail,calibrepath):
if 'mobi' in formats:
msg.attach(get_attachment(formats['mobi']))
elif 'epub' in formats:
filepath = make_mobi(book.id,calibrepath)
filepath = make_mobi(book.id, calibrepath)
if filepath is not None:
msg.attach(get_attachment(filepath))
elif filepath is None:
@ -207,8 +203,7 @@ def get_attachment(file_path):
return attachment
except IOError:
traceback.print_exc()
message = (_('The requested file could not be read. Maybe wrong '\
'permissions?'))
message = (_('The requested file could not be read. Maybe wrong permissions?')) # ToDo: What is this?
return None
@ -218,7 +213,7 @@ def get_valid_filename(value, replace_whitespace=True):
filename. Limits num characters to 128 max.
"""
value = value[:128]
re_slugify = re.compile('[^\w\s-]', re.UNICODE)
# re_slugify = re.compile('[^\w\s-]', re.UNICODE)
value = unicodedata.normalize('NFKD', value)
re_slugify = re.compile('[^\w\s-]', re.UNICODE)
value = unicode(re_slugify.sub('', value).strip())
@ -238,7 +233,7 @@ def get_normalized_author(value):
return value
def update_dir_stucture(book_id,calibrepath):
def update_dir_stucture(book_id, calibrepath):
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
path = os.path.join(calibrepath, book.path)

View File

@ -75,7 +75,7 @@
</table>
<div class="btn btn-default"><a href="{{url_for('configuration')}}">{{_('Configuration')}}</a></div>
<h2>{{_('Administration')}}</h2>
{% if not config.DEVELOPMENT %}
{% if not development %}
<div class="btn btn-default" data-toggle="modal" data-target="#RestartDialog">{{_('Restart Calibre-web')}}</a></div>
<div class="btn btn-default" data-toggle="modal" data-target="#ShutdownDialog">{{_('Stop Calibre-web')}}</a></div>
{% endif %}

View File

@ -9,7 +9,7 @@
</div>
<div class="form-group">
<label for="config_port">{{_('Server Port')}}</label>
<input type="text" class="form-control" name="config_port" id="config_port" value="{% if content.config_port != None %}{{ content.config_port }}{% endif %}" autocomplete="off" required>
<input type="number" min="1" max="65535" class="form-control" name="config_port" id="config_port" value="{% if content.config_port != None %}{{ content.config_port }}{% endif %}" autocomplete="off" required>
</div>
<div class="form-group">
<label for="config_calibre_web_title">{{_('Title')}}</label>
@ -17,11 +17,11 @@
</div>
<div class="form-group">
<label for="config_books_per_page">{{_('Books per page')}}</label>
<input type="text" class="form-control" name="config_books_per_page" id="config_books_per_page" value="{% if content.config_books_per_page != None %}{{ content.config_books_per_page }}{% endif %}" autocomplete="off">
<input type="number" min="1" max="200" class="form-control" name="config_books_per_page" id="config_books_per_page" value="{% if content.config_books_per_page != None %}{{ content.config_books_per_page }}{% endif %}" autocomplete="off">
</div>
<div class="form-group">
<label for="config_random_books">{{_('No. of random books to show')}}</label>
<input type="text" class="form-control" name="config_random_books" id="config_random_books" value="{% if content.config_random_books != None %}{{ content.config_random_books }}{% endif %}" autocomplete="off">
<input type="number" min="1" max="30" class="form-control" name="config_random_books" id="config_random_books" value="{% if content.config_random_books != None %}{{ content.config_random_books }}{% endif %}" autocomplete="off">
</div>
<div class="form-group">
@ -31,10 +31,10 @@
<div class="form-group">
<label for="config_log_level">{{_('Log Level')}}</label>
<select name="config_log_level" id="config_log_level" class="form-control">
<option value="DEBUG" {% if content.config_log_level == 'DEBUG' %}selected{% endif %}>DEBUG</option>
<option value="INFO" {% if content.config_log_level == 'INFO' or content.config_log_level == None %}selected{% endif %}>INFO</option>
<option value="WARNING" {% if content.config_log_level == 'WARNING' %}selected{% endif %}>WARNING</option>
<option value="ERROR" {% if content.config_log_level == 'ERROR' %}selected{% endif %}>ERROR</option>
<option value="10" {% if content.config_log_level == 10 %}selected{% endif %}>DEBUG</option>
<option value="20" {% if content.config_log_level == 20 or content.config_log_level == None %}selected{% endif %}>INFO</option>
<option value="30" {% if content.config_log_level == 30 %}selected{% endif %}>WARNING</option>
<option value="40" {% if content.config_log_level == 40 %}selected{% endif %}>ERROR</option>
</select>
</div>
<div class="form-group">
@ -48,8 +48,14 @@
<div class="form-group">
<input type="checkbox" id="config_public_reg" name="config_public_reg" {% if content.config_public_reg %}checked{% endif %}>
<label for="config_public_reg">{{_('Enable public registration')}}</label>
</div>
</div>
<button type="submit" class="btn btn-default">{{_('Submit')}}</button>
{% if not origin %}
<a href="{{ url_for('admin') }}" class="btn btn-default">{{_('Back')}}</a>
{% endif %}
{% if success %}
<a href="{{ url_for('login') }}" class="btn btn-default">{{_('Login')}}</a>
{% endif %}
</form>
</div>
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends "layout.html" %}
{% block body %}
{% if g.user.show_random_books() %}
{% if g.user.show_detail_random() %}
<div class="discover">
<h2>{{_('Discover (Random Books)')}}</h2>
<div class="row">

View File

@ -12,19 +12,19 @@
<tbody>
<tr>
<th>Python</th>
<td>{{Versions['PythonVersion']}}</td>
<td>{{versions['PythonVersion']}}</td>
</tr>
<tr>
<th>Kindlegen</th>
<td>{{Versions['KindlegenVersion']}}</td>
<td>{{versions['KindlegenVersion']}}</td>
</tr>
<tr>
<th>ImageMagick</th>
<td>{{Versions['ImageVersion']}}</td>
<td>{{versions['ImageVersion']}}</td>
</tr>
<tr>
<th>PyPDF2</th>
<td>{{Versions['PyPdfVersion']}}</td>
<td>{{versions['PyPdfVersion']}}</td>
</tr>
</tbody>
</table>

View File

@ -41,25 +41,33 @@
</select>
</div>
<div class="form-group">
<input type="checkbox" name="show_random" id="show_random" {% if content.random_books %}checked{% endif %}>
<input type="checkbox" name="show_random" id="show_random" {% if content.show_random_books() %}checked{% endif %}>
<label for="show_random">{{_('Show random books')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_hot" id="show_hot" {% if content.hot_books %}checked{% endif %}>
<input type="checkbox" name="show_hot" id="show_hot" {% if content.show_hot_books() %}checked{% endif %}>
<label for="show_hot">{{_('Show hot books')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_language" id="show_language" {% if content.language_books %}checked{% endif %}>
<input type="checkbox" name="show_language" id="show_language" {% if content.show_language() %}checked{% endif %}>
<label for="show_language">{{_('Show language selection')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_series" id="show_series" {% if content.series_books %}checked{% endif %}>
<input type="checkbox" name="show_series" id="show_series" {% if content.show_series() %}checked{% endif %}>
<label for="show_series">{{_('Show series selection')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_category" id="show_category" {% if content.category_books %}checked{% endif %}>
<input type="checkbox" name="show_category" id="show_category" {% if content.show_category() %}checked{% endif %}>
<label for="show_category">{{_('Show category selection')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_author" id="show_author" {% if content.show_author() %}checked{% endif %}>
<label for="show_author">{{_('Show author selection')}}</label>
</div>
<div class="form-group">
<input type="checkbox" name="show_detail_random" id="show_detail_random" {% if content.show_detail_random() %}checked{% endif %}>
<label for="show_detail_random">{{_('Show random books in detail view')}}</label>
</div>
{% if g.user and g.user.role_admin() and not profile %}
{% if not content.role_anonymous() %}

248
cps/ub.py
View File

@ -8,24 +8,41 @@ from sqlalchemy.orm import *
from flask_login import AnonymousUserMixin
import os
import traceback
import logging
from werkzeug.security import generate_password_hash
from flask_babel import gettext as _
dbpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__))+os.sep+".."+os.sep), "app.db")
dbpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) + os.sep + ".." + os.sep), "app.db")
engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False)
Base = declarative_base()
ROLE_USER = 0
ROLE_ADMIN = 1
ROLE_DOWNLOAD = 2
ROLE_UPLOAD = 4
ROLE_UPLOAD = 4
ROLE_EDIT = 8
ROLE_PASSWD = 16
ROLE_ANONYMOUS = 32
DETAIL_RANDOM = 1
SIDEBAR_LANGUAGE = 2
SIDEBAR_SERIES = 4
SIDEBAR_CATEGORY = 8
SIDEBAR_HOT = 16
SIDEBAR_RANDOM = 32
SIDEBAR_AUTHOR = 64
DEFAULT_PASS = "admin123"
class UserBase():
DEVELOPMENT = False
class UserBase:
@staticmethod
def is_authenticated(self):
return True
@ -78,57 +95,55 @@ class UserBase():
return self.default_language
def show_random_books(self):
return self.random_books
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_RANDOM == SIDEBAR_RANDOM else False
else:
return False
def show_language(self):
return self.language_books
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_LANGUAGE == SIDEBAR_LANGUAGE else False
else:
return False
def show_hot_books(self):
return self.hot_books
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_HOT == SIDEBAR_HOT else False
else:
return False
def show_series(self):
return self.series_books
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_SERIES == SIDEBAR_SERIES else False
else:
return False
def show_category(self):
return self.category_books
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_CATEGORY == SIDEBAR_CATEGORY else False
else:
return False
def show_author(self):
if self.sidebar_view is not None:
return True if self.sidebar_view & SIDEBAR_AUTHOR == SIDEBAR_AUTHOR else False
else:
return False
def show_detail_random(self):
if self.sidebar_view is not None:
return True if self.sidebar_view & DETAIL_RANDOM == DETAIL_RANDOM else False
else:
return False
def __repr__(self):
return '<User %r>' % self.nickname
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):
data=session.query(Settings).first()
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
if self.config_calibre_dir is not None and (self.db_configured is None or self.db_configured is True):
self.db_configured=True
else:
self.db_configured = False
@property
def get_main_dir(self):
return self.config_main_dir
@property
def is_Calibre_Configured(self):
return self.db_configured
class User(UserBase,Base):
# 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):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
@ -140,31 +155,31 @@ class User(UserBase,Base):
shelf = relationship('Shelf', backref='user', lazy='dynamic')
downloads = relationship('Downloads', backref='user', lazy='dynamic')
locale = Column(String(2), default="en")
random_books = Column(Integer, default=1)
language_books = Column(Integer, default=1)
series_books = Column(Integer, default=1)
category_books = Column(Integer, default=1)
hot_books = Column(Integer, default=1)
sidebar_view = Column(Integer, default=1)
#language_books = Column(Integer, default=1)
#series_books = Column(Integer, default=1)
#category_books = Column(Integer, default=1)
#hot_books = Column(Integer, default=1)
default_language = Column(String(3), default="all")
class Anonymous(AnonymousUserMixin,UserBase):
# anon_browse = None
# Class for anonymous user is derived from User base and complets overrides methods and properties for the
# anonymous user
class Anonymous(AnonymousUserMixin, UserBase):
def __init__(self):
self.loadSettings()
def loadSettings(self):
data=session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first()
settings=session.query(Settings).first()
data = session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first()
settings = session.query(Settings).first()
self.nickname = data.nickname
self.role = data.role
self.random_books = data.random_books
self.sidebar_view = data.sidebar_view
self.default_language = data.default_language
self.language_books = data.language_books
self.series_books = data.series_books
self.category_books = data.category_books
self.hot_books = data.hot_books
#self.language_books = data.language_books
#self.series_books = data.series_books
#self.category_books = data.category_books
#self.hot_books = data.hot_books
self.default_language = data.default_language
self.locale = data.locale
self.anon_browse = settings.config_anonbrowse
@ -179,6 +194,7 @@ class Anonymous(AnonymousUserMixin,UserBase):
return self.anon_browse
# Baseclass representing Shelfs in calibre-web inapp.db
class Shelf(Base):
__tablename__ = 'shelf'
@ -190,6 +206,8 @@ class Shelf(Base):
def __repr__(self):
return '<Shelf %r>' % self.name
# Baseclass representing Relationship between books and Shelfs in Calibre-web in app.db (N:M)
class BookShelf(Base):
__tablename__ = 'book_shelf_link'
@ -202,6 +220,7 @@ class BookShelf(Base):
return '<Book %r>' % self.id
# Baseclass representing Downloads from calibre-web in app.db
class Downloads(Base):
__tablename__ = 'downloads'
@ -212,54 +231,79 @@ class Downloads(Base):
def __repr__(self):
return '<Download %r' % self.book_id
# Baseclass for representing settings in app.db with email server settings and Calibre database settings
# (application settings)
class Settings(Base):
__tablename__ = 'settings'
id = Column(Integer, primary_key=True)
mail_server = Column(String)
mail_port = Column(Integer, default = 25)
mail_use_ssl = Column(SmallInteger, default = 0)
mail_port = Column(Integer, default=25)
mail_use_ssl = Column(SmallInteger, default=0)
mail_login = Column(String)
mail_password = Column(String)
mail_from = Column(String)
config_calibre_dir = Column(String)
config_port = Column(Integer, default = 8083)
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(String, default=u'INFO')
config_uploading = Column(SmallInteger, default = 0)
config_anonbrowse = Column(SmallInteger, default = 0)
config_public_reg = Column(SmallInteger, default = 0)
config_port = Column(Integer, default=8083)
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)
def __repr__(self):
#return '<Smtp %r>' % (self.mail_server)
pass
# 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):
data = session.query(Settings).first()
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
if self.config_calibre_dir is not None: # and (self.db_configured is None or self.db_configured is True):
self.db_configured = True
else:
self.db_configured = False
@property
def get_main_dir(self):
return self.config_main_dir
#def is_Calibre_Configured(self):
# return self.db_configured
# 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
def migrate_Database():
if session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first() is None:
create_anonymous_user()
try:
session.query(exists().where(User.random_books)).scalar()
session.query(exists().where(User.locale)).scalar()
session.commit()
except exc.OperationalError: # Database is not compatible, some rows are missing
conn = engine.connect()
conn.execute("ALTER TABLE user ADD column random_books INTEGER DEFAULT 1")
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()
try:
session.query(exists().where(User.language_books)).scalar()
session.commit()
except exc.OperationalError: # Database is not compatible, some rows are missing
conn = engine.connect()
conn.execute("ALTER TABLE user ADD column language_books INTEGER DEFAULT 1")
conn.execute("ALTER TABLE user ADD column series_books INTEGER DEFAULT 1")
conn.execute("ALTER TABLE user ADD column category_books INTEGER DEFAULT 1")
conn.execute("ALTER TABLE user ADD column hot_books INTEGER DEFAULT 1")
session.commit()
try:
session.query(exists().where(Settings.config_calibre_dir)).scalar()
session.commit()
@ -270,12 +314,34 @@ def migrate_Database():
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")
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+'")
conn.execute("ALTER TABLE Settings ADD column `config_log_level` String DEFAULT 'INFO'")
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+'")
conn.execute("ALTER TABLE Settings ADD column `config_log_level` SmallInteger DEFAULT '" + logging.INFO + "'")
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")
session.commit()
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:
conn.execute("SELET language_books FROM user")
session.commit()
except exc.OperationalError:
conn = engine.connect()
conn.execute("UPDATE user SET 'sidebar_view' = (random_books*"+str(SIDEBAR_RANDOM)+"+ language_books *"+
str(SIDEBAR_LANGUAGE)+"+ series_books *"+str(SIDEBAR_SERIES)+"+ category_books *"+str(SIDEBAR_CATEGORY)+
"+ hot_books *"+str(SIDEBAR_HOT)+"+"+str(SIDEBAR_AUTHOR)+"+"+str(DETAIL_RANDOM)+")")
session.commit()
if session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first() is None:
create_anonymous_user()
def create_default_config():
settings = Settings()
@ -307,10 +373,12 @@ def get_mail_settings():
return data
# Generate user Guest (translated text), as anoymous user, no rights
def create_anonymous_user():
user = User()
user.nickname = _("Guest")
user.email='no@email'
user.email = 'no@email'
user.role = ROLE_ANONYMOUS
user.password = generate_password_hash('1')
@ -322,10 +390,14 @@ def create_anonymous_user():
pass
# Generate User admin with admin123 password, and access to everything
def create_admin_user():
user = User()
user.nickname = "admin"
user.role = ROLE_USER + ROLE_ADMIN + ROLE_DOWNLOAD + ROLE_UPLOAD + ROLE_EDIT + ROLE_PASSWD
user.sidebar_view = DETAIL_RANDOM + SIDEBAR_LANGUAGE + SIDEBAR_SERIES + SIDEBAR_CATEGORY + SIDEBAR_HOT + \
SIDEBAR_RANDOM + SIDEBAR_AUTHOR
user.password = generate_password_hash(DEFAULT_PASS)
session.add(user)
@ -335,10 +407,13 @@ def create_admin_user():
session.rollback()
pass
# Open session for database connection
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
# generate database and admin and guest user, if no database is existing
if not os.path.exists(dbpath):
try:
Base.metadata.create_all(engine)
@ -349,3 +424,6 @@ if not os.path.exists(dbpath):
pass
else:
migrate_Database()
# Generate global Settings Object accecable from every file
config = Config()

File diff suppressed because it is too large Load Diff