2015-08-02 18:59:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-14 15:39:25 +00:00
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
import mimetypes
|
2016-03-28 19:07:13 +00:00
|
|
|
import logging
|
2016-04-15 16:23:00 +00:00
|
|
|
from logging.handlers import RotatingFileHandler
|
2017-04-14 13:57:27 +00:00
|
|
|
from flask import (Flask, render_template, request, Response, redirect,
|
|
|
|
url_for, send_from_directory, make_response, g, flash,
|
2018-08-12 07:29:57 +00:00
|
|
|
abort, Markup)
|
2017-02-23 18:58:56 +00:00
|
|
|
from flask import __version__ as flaskVersion
|
2018-09-29 17:21:04 +00:00
|
|
|
from werkzeug import __version__ as werkzeugVersion
|
2018-10-01 08:35:13 +00:00
|
|
|
from werkzeug.exceptions import default_exceptions
|
|
|
|
|
2018-09-29 17:21:04 +00:00
|
|
|
from jinja2 import __version__ as jinja2Version
|
2017-08-13 16:21:19 +00:00
|
|
|
import cache_buster
|
2017-01-28 19:16:40 +00:00
|
|
|
import ub
|
|
|
|
from ub import config
|
|
|
|
import helper
|
2015-08-02 18:59:11 +00:00
|
|
|
import os
|
|
|
|
from sqlalchemy.sql.expression import func
|
2016-03-26 15:12:29 +00:00
|
|
|
from sqlalchemy.sql.expression import false
|
2015-10-13 16:07:17 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2017-02-23 18:58:56 +00:00
|
|
|
from sqlalchemy import __version__ as sqlalchemyVersion
|
2015-08-02 18:59:11 +00:00
|
|
|
from math import ceil
|
2017-04-14 13:57:27 +00:00
|
|
|
from flask_login import (LoginManager, login_user, logout_user,
|
|
|
|
login_required, current_user)
|
2017-03-19 16:14:16 +00:00
|
|
|
from flask_principal import Principal
|
2017-03-07 18:46:23 +00:00
|
|
|
from flask_principal import __version__ as flask_principalVersion
|
2016-11-09 18:24:33 +00:00
|
|
|
from flask_babel import Babel
|
|
|
|
from flask_babel import gettext as _
|
2017-01-28 19:16:40 +00:00
|
|
|
import requests
|
2015-08-02 18:59:11 +00:00
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
2017-03-29 18:59:37 +00:00
|
|
|
from werkzeug.datastructures import Headers
|
2016-11-09 18:24:33 +00:00
|
|
|
from babel import Locale as LC
|
|
|
|
from babel import negotiate_locale
|
2017-02-23 18:58:56 +00:00
|
|
|
from babel import __version__ as babelVersion
|
2017-12-02 17:42:21 +00:00
|
|
|
from babel.dates import format_date, format_datetime
|
2018-09-02 09:48:58 +00:00
|
|
|
from babel.core import UnknownLocaleError
|
2015-10-13 16:07:17 +00:00
|
|
|
from functools import wraps
|
2015-10-13 17:06:37 +00:00
|
|
|
import base64
|
2016-03-26 15:12:29 +00:00
|
|
|
from sqlalchemy.sql import *
|
2016-03-28 22:09:11 +00:00
|
|
|
import json
|
2016-04-03 21:52:32 +00:00
|
|
|
import datetime
|
2016-11-09 18:24:33 +00:00
|
|
|
from iso639 import languages as isoLanguages
|
2017-02-23 18:58:56 +00:00
|
|
|
from iso639 import __version__ as iso639Version
|
2018-09-30 07:43:20 +00:00
|
|
|
from pytz import __version__ as pytzVersion
|
2016-04-03 21:52:32 +00:00
|
|
|
from uuid import uuid4
|
2016-07-16 08:44:47 +00:00
|
|
|
import os.path
|
2016-12-23 08:53:39 +00:00
|
|
|
import sys
|
2016-07-16 08:44:47 +00:00
|
|
|
import re
|
2017-01-22 15:44:37 +00:00
|
|
|
import db
|
|
|
|
from shutil import move, copyfile
|
2017-02-20 18:34:37 +00:00
|
|
|
import gdriveutils
|
2018-07-18 18:21:44 +00:00
|
|
|
import converter
|
2017-03-02 00:28:30 +00:00
|
|
|
import tempfile
|
2018-07-18 18:21:44 +00:00
|
|
|
from redirect import redirect_back
|
2018-09-07 18:04:35 +00:00
|
|
|
import time
|
|
|
|
import server
|
2018-09-30 07:43:20 +00:00
|
|
|
from reverseproxy import ReverseProxied
|
2019-01-20 16:45:42 +00:00
|
|
|
from updater import updater_thread
|
2019-02-03 15:38:27 +00:00
|
|
|
import hashlib
|
2018-11-03 12:43:38 +00:00
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
try:
|
|
|
|
from googleapiclient.errors import HttpError
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
from goodreads.client import GoodreadsClient
|
|
|
|
goodreads_support = True
|
|
|
|
except ImportError:
|
|
|
|
goodreads_support = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
import Levenshtein
|
|
|
|
levenshtein_support = True
|
|
|
|
except ImportError:
|
|
|
|
levenshtein_support = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from functools import reduce
|
|
|
|
except ImportError:
|
|
|
|
pass # We're not using Python 3
|
|
|
|
|
|
|
|
try:
|
|
|
|
import rarfile
|
|
|
|
rar_support=True
|
|
|
|
except ImportError:
|
|
|
|
rar_support=False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from natsort import natsorted as sort
|
|
|
|
except ImportError:
|
|
|
|
sort=sorted # Just use regular sort then
|
|
|
|
# may cause issues with badly named pages in cbz/cbr files
|
2018-09-09 07:41:46 +00:00
|
|
|
try:
|
|
|
|
import cPickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle as cPickle
|
2017-02-20 18:34:37 +00:00
|
|
|
|
2017-03-05 09:40:39 +00:00
|
|
|
try:
|
2017-03-07 22:03:10 +00:00
|
|
|
from urllib.parse import quote
|
2017-03-05 09:40:39 +00:00
|
|
|
from imp import reload
|
2017-03-31 16:31:16 +00:00
|
|
|
except ImportError:
|
2017-03-05 10:48:59 +00:00
|
|
|
from urllib import quote
|
2017-03-05 09:40:39 +00:00
|
|
|
|
2017-03-07 18:46:23 +00:00
|
|
|
try:
|
|
|
|
from flask_login import __version__ as flask_loginVersion
|
2017-03-31 16:31:16 +00:00
|
|
|
except ImportError:
|
2017-03-07 18:46:23 +00:00
|
|
|
from flask_login.__about__ import __version__ as flask_loginVersion
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Global variables
|
2018-09-30 07:43:20 +00:00
|
|
|
current_milli_time = lambda: int(round(time.time() * 1000))
|
2017-04-14 13:57:27 +00:00
|
|
|
gdrive_watch_callback_token = 'target=calibreweb-watch_files'
|
2019-01-23 17:14:45 +00:00
|
|
|
# ToDo: Somehow caused by circular import under python3 refactor
|
2019-01-21 20:01:16 +00:00
|
|
|
py3_gevent_link = None
|
2019-01-23 17:14:45 +00:00
|
|
|
py3_restart_Typ = False
|
2018-09-09 16:17:15 +00:00
|
|
|
EXTENSIONS_UPLOAD = {'txt', 'pdf', 'epub', 'mobi', 'azw', 'azw3', 'cbr', 'cbz', 'cbt', 'djvu', 'prc', 'doc', 'docx',
|
2018-09-17 19:55:41 +00:00
|
|
|
'fb2', 'html', 'rtf', 'odt'}
|
2018-12-27 09:53:19 +00:00
|
|
|
EXTENSIONS_CONVERT = {'pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2', 'lit', 'lrf', 'txt', 'htmlz'}
|
2018-09-09 16:17:15 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Main code
|
2017-01-22 15:44:37 +00:00
|
|
|
mimetypes.init()
|
|
|
|
mimetypes.add_type('application/xhtml+xml', '.xhtml')
|
|
|
|
mimetypes.add_type('application/epub+zip', '.epub')
|
2017-10-01 15:15:44 +00:00
|
|
|
mimetypes.add_type('application/fb2+zip', '.fb2')
|
2017-01-22 15:44:37 +00:00
|
|
|
mimetypes.add_type('application/x-mobipocket-ebook', '.mobi')
|
|
|
|
mimetypes.add_type('application/x-mobipocket-ebook', '.prc')
|
|
|
|
mimetypes.add_type('application/vnd.amazon.ebook', '.azw')
|
|
|
|
mimetypes.add_type('application/x-cbr', '.cbr')
|
|
|
|
mimetypes.add_type('application/x-cbz', '.cbz')
|
|
|
|
mimetypes.add_type('application/x-cbt', '.cbt')
|
|
|
|
mimetypes.add_type('image/vnd.djvu', '.djvu')
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
app = (Flask(__name__))
|
2018-10-01 08:35:13 +00:00
|
|
|
|
2018-11-03 16:38:52 +00:00
|
|
|
# custom error page
|
2018-10-01 08:35:13 +00:00
|
|
|
def error_http(error):
|
|
|
|
return render_template('http_error.html',
|
2018-11-03 16:38:52 +00:00
|
|
|
error_code=error.code,
|
|
|
|
error_name=error.name,
|
|
|
|
instance=config.config_calibre_web_title
|
|
|
|
), error.code
|
2018-10-01 08:35:13 +00:00
|
|
|
|
|
|
|
# http error handling
|
|
|
|
for ex in default_exceptions:
|
2018-11-03 16:38:52 +00:00
|
|
|
# new routine for all client errors, server errors stay
|
|
|
|
if ex < 500:
|
|
|
|
app.register_error_handler(ex, error_http)
|
2018-10-01 08:35:13 +00:00
|
|
|
|
2016-07-14 00:12:21 +00:00
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2017-08-13 16:21:19 +00:00
|
|
|
cache_buster.init_cache_busting(app)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-29 20:06:08 +00:00
|
|
|
formatter = logging.Formatter(
|
2016-04-15 16:23:00 +00:00
|
|
|
"[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
|
2018-10-03 12:03:08 +00:00
|
|
|
try:
|
|
|
|
file_handler = RotatingFileHandler(config.get_config_logfile(), maxBytes=50000, backupCount=2)
|
|
|
|
except IOError:
|
|
|
|
file_handler = RotatingFileHandler(os.path.join(config.get_main_dir, "calibre-web.log"),
|
|
|
|
maxBytes=50000, backupCount=2)
|
|
|
|
# ToDo: reset logfile value in config class
|
2016-04-15 16:23:00 +00:00
|
|
|
file_handler.setFormatter(formatter)
|
2016-03-28 19:07:13 +00:00
|
|
|
app.logger.addHandler(file_handler)
|
2017-01-28 19:16:40 +00:00
|
|
|
app.logger.setLevel(config.config_log_level)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-03-28 19:07:13 +00:00
|
|
|
app.logger.info('Starting Calibre Web...')
|
2016-06-05 16:42:18 +00:00
|
|
|
logging.getLogger("book_formats").addHandler(file_handler)
|
2017-01-29 20:06:08 +00:00
|
|
|
logging.getLogger("book_formats").setLevel(config.config_log_level)
|
2016-06-05 16:42:18 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
Principal(app)
|
2016-11-09 18:24:33 +00:00
|
|
|
babel = Babel(app)
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
import uploader
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
lm = LoginManager(app)
|
|
|
|
lm.init_app(app)
|
|
|
|
lm.login_view = 'login'
|
2017-01-12 19:43:36 +00:00
|
|
|
lm.anonymous_user = ub.Anonymous
|
2017-07-11 16:35:55 +00:00
|
|
|
app.secret_key = os.getenv('SECRET_KEY', 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT')
|
2017-01-28 19:16:40 +00:00
|
|
|
db.setup_db()
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-09-08 19:16:56 +00:00
|
|
|
with open(os.path.join(config.get_main_dir, 'cps/translations/iso639.pickle'), 'rb') as f:
|
|
|
|
language_table = cPickle.load(f)
|
|
|
|
|
2017-03-07 18:10:17 +00:00
|
|
|
|
2017-02-22 22:06:59 +00:00
|
|
|
def is_gdrive_ready():
|
2018-08-24 13:48:09 +00:00
|
|
|
return os.path.exists(os.path.join(config.get_main_dir, 'settings.yaml')) and \
|
|
|
|
os.path.exists(os.path.join(config.get_main_dir, 'gdrive_credentials'))
|
2017-02-22 22:06:59 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2018-11-03 16:38:52 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
|
|
|
# if a user is logged in, use the locale from the user settings
|
|
|
|
user = getattr(g, 'user', None)
|
|
|
|
if user is not None and hasattr(user, "locale"):
|
2018-09-17 17:03:29 +00:00
|
|
|
if user.nickname != 'Guest': # if the account is the guest account bypass the config lang settings
|
|
|
|
return user.locale
|
2018-11-25 06:19:00 +00:00
|
|
|
translations = [str(item) for item in babel.list_translations()] + ['en']
|
2019-01-03 19:47:46 +00:00
|
|
|
preferred = list()
|
|
|
|
for x in request.accept_languages.values():
|
|
|
|
try:
|
|
|
|
preferred.append(str(LC.parse(x.replace('-', '_'))))
|
|
|
|
except (UnknownLocaleError, ValueError) as e:
|
|
|
|
app.logger.debug("Could not parse locale: %s", e)
|
|
|
|
preferred.append('en')
|
2017-01-14 14:05:49 +00:00
|
|
|
return negotiate_locale(preferred, translations)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@babel.timezoneselector
|
|
|
|
def get_timezone():
|
|
|
|
user = getattr(g, 'user', None)
|
|
|
|
if user is not None:
|
|
|
|
return user.timezone
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@lm.user_loader
|
2017-01-28 19:16:40 +00:00
|
|
|
def load_user(user_id):
|
|
|
|
return ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@lm.header_loader
|
|
|
|
def load_user_from_header(header_val):
|
|
|
|
if header_val.startswith('Basic '):
|
|
|
|
header_val = header_val.replace('Basic ', '', 1)
|
2017-01-28 19:16:40 +00:00
|
|
|
basic_username = basic_password = ''
|
2015-08-02 18:59:11 +00:00
|
|
|
try:
|
|
|
|
header_val = base64.b64decode(header_val)
|
2015-10-13 17:06:37 +00:00
|
|
|
basic_username = header_val.split(':')[0]
|
|
|
|
basic_password = header_val.split(':')[1]
|
2015-08-02 18:59:11 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
2017-11-12 13:10:08 +00:00
|
|
|
user = ub.session.query(ub.User).filter(func.lower(ub.User.nickname) == basic_username.lower()).first()
|
2015-10-13 17:06:37 +00:00
|
|
|
if user and check_password_hash(user.password, basic_password):
|
|
|
|
return user
|
|
|
|
return
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2015-10-13 17:06:37 +00:00
|
|
|
def check_auth(username, password):
|
2017-11-12 13:10:08 +00:00
|
|
|
user = ub.session.query(ub.User).filter(func.lower(ub.User.nickname) == username.lower()).first()
|
2017-04-04 17:05:09 +00:00
|
|
|
return bool(user and check_password_hash(user.password, password))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2015-10-13 17:06:37 +00:00
|
|
|
def authenticate():
|
|
|
|
return Response(
|
2016-12-23 08:53:39 +00:00
|
|
|
'Could not verify your access level for that URL.\n'
|
|
|
|
'You have to login with proper credentials', 401,
|
|
|
|
{'WWW-Authenticate': 'Basic realm="Login Required"'})
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2016-04-27 16:29:45 +00:00
|
|
|
def requires_basic_auth_if_no_ano(f):
|
2015-10-13 17:06:37 +00:00
|
|
|
@wraps(f)
|
|
|
|
def decorated(*args, **kwargs):
|
|
|
|
auth = request.authorization
|
2017-01-22 15:44:37 +00:00
|
|
|
if config.config_anonbrowse != 1:
|
2016-04-27 16:29:45 +00:00
|
|
|
if not auth or not check_auth(auth.username, auth.password):
|
|
|
|
return authenticate()
|
2015-10-13 17:06:37 +00:00
|
|
|
return f(*args, **kwargs)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-10-13 17:06:37 +00:00
|
|
|
return decorated
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
# simple pagination for the feed
|
|
|
|
class Pagination(object):
|
2015-08-02 18:59:11 +00:00
|
|
|
def __init__(self, page, per_page, total_count):
|
2017-01-12 19:43:36 +00:00
|
|
|
self.page = int(page)
|
|
|
|
self.per_page = int(per_page)
|
|
|
|
self.total_count = int(total_count)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-09 18:10:39 +00:00
|
|
|
@property
|
|
|
|
def next_offset(self):
|
|
|
|
return int(self.page * self.per_page)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def previous_offset(self):
|
2017-01-28 19:16:40 +00:00
|
|
|
return int((self.page - 2) * self.per_page)
|
2017-01-09 18:10:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_offset(self):
|
|
|
|
last = int(self.total_count) - int(self.per_page)
|
|
|
|
if last < 0:
|
|
|
|
last = 0
|
|
|
|
return int(last)
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@property
|
|
|
|
def pages(self):
|
|
|
|
return int(ceil(self.total_count / float(self.per_page)))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_prev(self):
|
|
|
|
return self.page > 1
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_next(self):
|
|
|
|
return self.page < self.pages
|
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
# right_edge: last right_edges count of all pages are shown as number, means, if 10 pages are paginated -> 9,10 shwn
|
|
|
|
# left_edge: first left_edges count of all pages are shown as number -> 1,2 shwn
|
|
|
|
# left_current: left_current count below current page are shown as number, means if current page 5 -> 3,4 shwn
|
|
|
|
# left_current: right_current count above current page are shown as number, means if current page 5 -> 6,7 shwn
|
2015-08-02 18:59:11 +00:00
|
|
|
def iter_pages(self, left_edge=2, left_current=2,
|
2018-08-19 11:36:04 +00:00
|
|
|
right_current=4, right_edge=2):
|
2015-08-02 18:59:11 +00:00
|
|
|
last = 0
|
2018-08-19 11:36:04 +00:00
|
|
|
left_current = self.page - left_current - 1
|
|
|
|
right_current = self.page + right_current + 1
|
|
|
|
right_edge = self.pages - right_edge
|
|
|
|
for num in range(1, (self.pages + 1)):
|
|
|
|
if num <= left_edge or (left_current < num < right_current) or num > right_edge:
|
2015-08-02 18:59:11 +00:00
|
|
|
if last + 1 != num:
|
|
|
|
yield None
|
|
|
|
yield num
|
|
|
|
last = num
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def login_required_if_no_ano(func):
|
2017-10-10 18:13:28 +00:00
|
|
|
@wraps(func)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if config.config_anonbrowse == 1:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return login_required(func)(*args, **kwargs)
|
|
|
|
return decorated_view
|
2016-04-27 08:35:23 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-07-08 01:18:03 +00:00
|
|
|
def remote_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if config.config_remote_login:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
if request.is_xhr:
|
|
|
|
data = {'status': 'error', 'message': 'Forbidden'}
|
2018-08-24 13:48:09 +00:00
|
|
|
response = make_response(json.dumps(data, ensure_ascii=False))
|
2017-07-08 01:18:03 +00:00
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response, 403
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
# custom jinja filters
|
2018-08-16 19:17:26 +00:00
|
|
|
|
|
|
|
# pagination links in jinja
|
|
|
|
@app.template_filter('url_for_other_page')
|
|
|
|
def url_for_other_page(page):
|
|
|
|
args = request.view_args.copy()
|
|
|
|
args['page'] = page
|
|
|
|
return url_for(request.endpoint, **args)
|
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
|
2018-08-16 19:17:26 +00:00
|
|
|
# shortentitles to at longest nchar, shorten longer words if necessary
|
2016-04-03 21:33:29 +00:00
|
|
|
@app.template_filter('shortentitle')
|
2018-08-24 13:48:09 +00:00
|
|
|
def shortentitle_filter(s, nchar=20):
|
2018-07-17 16:24:12 +00:00
|
|
|
text = s.split()
|
|
|
|
res = "" # result
|
2018-08-12 07:29:57 +00:00
|
|
|
suml = 0 # overall length
|
2018-07-17 16:24:12 +00:00
|
|
|
for line in text:
|
2018-08-12 07:29:57 +00:00
|
|
|
if suml >= 60:
|
2018-07-17 16:24:12 +00:00
|
|
|
res += '...'
|
|
|
|
break
|
|
|
|
# if word longer than 20 chars truncate line and append '...', otherwise add whole word to result
|
2018-08-16 19:17:26 +00:00
|
|
|
# string, and summarize total length to stop at chars given by nchar
|
2018-07-17 16:24:12 +00:00
|
|
|
if len(line) > nchar:
|
|
|
|
res += line[:(nchar-3)] + '[..] '
|
2018-08-12 07:29:57 +00:00
|
|
|
suml += nchar+3
|
2018-07-17 16:24:12 +00:00
|
|
|
else:
|
|
|
|
res += line + ' '
|
2018-08-12 07:29:57 +00:00
|
|
|
suml += len(line) + 1
|
2018-07-17 16:24:12 +00:00
|
|
|
return res.strip()
|
2015-10-13 17:06:37 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-15 11:23:08 +00:00
|
|
|
@app.template_filter('mimetype')
|
|
|
|
def mimetype_filter(val):
|
|
|
|
try:
|
2017-01-28 19:16:40 +00:00
|
|
|
s = mimetypes.types_map['.' + val]
|
2017-03-29 19:43:55 +00:00
|
|
|
except Exception:
|
2017-01-28 19:16:40 +00:00
|
|
|
s = 'application/octet-stream'
|
2017-01-15 11:23:08 +00:00
|
|
|
return s
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-15 17:09:17 +00:00
|
|
|
@app.template_filter('formatdate')
|
2017-12-02 17:42:21 +00:00
|
|
|
def formatdate_filter(val):
|
2017-02-15 17:09:17 +00:00
|
|
|
conformed_timestamp = re.sub(r"[:]|([-](?!((\d{2}[:]\d{2})|(\d{4}))$))", '', val)
|
2017-02-17 17:22:08 +00:00
|
|
|
formatdate = datetime.datetime.strptime(conformed_timestamp[:15], "%Y%m%d %H%M%S")
|
2017-04-14 18:29:11 +00:00
|
|
|
return format_date(formatdate, format='medium', locale=get_locale())
|
|
|
|
|
2017-02-15 17:09:17 +00:00
|
|
|
|
2017-07-09 23:27:46 +00:00
|
|
|
@app.template_filter('formatdateinput')
|
|
|
|
def format_date_input(val):
|
|
|
|
conformed_timestamp = re.sub(r"[:]|([-](?!((\d{2}[:]\d{2})|(\d{4}))$))", '', val)
|
|
|
|
date_obj = datetime.datetime.strptime(conformed_timestamp[:15], "%Y%m%d %H%M%S")
|
2017-11-30 15:49:46 +00:00
|
|
|
input_date = date_obj.isoformat().split('T', 1)[0] # Hack to support dates <1900
|
2017-07-09 23:27:46 +00:00
|
|
|
return '' if input_date == "0101-01-01" else input_date
|
|
|
|
|
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.template_filter('strftime')
|
|
|
|
def timestamptodate(date, fmt=None):
|
2017-04-14 18:29:11 +00:00
|
|
|
date = datetime.datetime.fromtimestamp(
|
2017-02-20 18:34:37 +00:00
|
|
|
int(date)/1000
|
|
|
|
)
|
|
|
|
native = date.replace(tzinfo=None)
|
|
|
|
if fmt:
|
2017-04-14 18:29:11 +00:00
|
|
|
time_format = fmt
|
2017-02-20 18:34:37 +00:00
|
|
|
else:
|
2017-04-14 18:29:11 +00:00
|
|
|
time_format = '%d %m %Y - %H:%S'
|
2017-04-02 08:05:07 +00:00
|
|
|
return native.strftime(time_format)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-07-06 16:21:35 +00:00
|
|
|
@app.template_filter('yesno')
|
2017-07-08 01:22:05 +00:00
|
|
|
def yesno(value, yes, no):
|
|
|
|
return yes if value else no
|
2017-07-06 16:21:35 +00:00
|
|
|
|
|
|
|
|
2018-08-31 08:47:58 +00:00
|
|
|
'''@app.template_filter('canread')
|
2018-01-11 13:43:39 +00:00
|
|
|
def canread(ext):
|
2018-01-16 14:30:26 +00:00
|
|
|
if isinstance(ext, db.Data):
|
2018-01-11 13:43:39 +00:00
|
|
|
ext = ext.format
|
2018-09-09 16:17:15 +00:00
|
|
|
return ext.lower() in EXTENSIONS_READER'''
|
2018-01-11 13:43:39 +00:00
|
|
|
|
|
|
|
|
2015-10-13 16:07:17 +00:00
|
|
|
def admin_required(f):
|
|
|
|
"""
|
|
|
|
Checks if current_user.role == 1
|
|
|
|
"""
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2015-10-13 16:07:17 +00:00
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
2016-04-27 08:35:23 +00:00
|
|
|
if current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
return inner
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
def unconfigured(f):
|
|
|
|
"""
|
|
|
|
Checks if current_user.role == 1
|
|
|
|
"""
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
2017-01-28 19:16:40 +00:00
|
|
|
if not config.db_configured:
|
2017-01-22 15:44:37 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
return inner
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def download_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_download() or current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
return inner
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def upload_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_upload() or current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
return inner
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
def edit_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_edit() or current_user.role_admin():
|
2015-10-13 16:07:17 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2015-10-13 16:07:17 +00:00
|
|
|
return inner
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# Language and content filters for displaying in the UI
|
2017-08-26 15:12:16 +00:00
|
|
|
def common_filters():
|
2016-11-09 18:24:33 +00:00
|
|
|
if current_user.filter_language() != "all":
|
2017-04-02 08:05:07 +00:00
|
|
|
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
2016-11-09 18:24:33 +00:00
|
|
|
else:
|
2017-08-28 16:28:48 +00:00
|
|
|
lang_filter = true()
|
2017-08-08 16:52:00 +00:00
|
|
|
content_rating_filter = false() if current_user.mature_content else \
|
|
|
|
db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags()))
|
2017-08-26 15:12:16 +00:00
|
|
|
return and_(lang_filter, ~content_rating_filter)
|
|
|
|
|
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# Creates for all stored languages a translated speaking name in the array for the UI
|
2018-08-24 13:48:09 +00:00
|
|
|
def speaking_language(languages=None):
|
2018-08-19 08:14:20 +00:00
|
|
|
if not languages:
|
|
|
|
languages = db.session.query(db.Languages).all()
|
|
|
|
for lang in languages:
|
|
|
|
try:
|
|
|
|
cur_l = LC.parse(lang.lang_code)
|
|
|
|
lang.name = cur_l.get_language_name(get_locale())
|
2018-09-02 09:48:58 +00:00
|
|
|
except UnknownLocaleError:
|
2018-08-19 08:14:20 +00:00
|
|
|
lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
|
|
|
|
return languages
|
|
|
|
|
2019-01-06 13:16:52 +00:00
|
|
|
# Orders all Authors in the list according to authors sort
|
|
|
|
def order_authors(entry):
|
|
|
|
sort_authors = entry.author_sort.split('&')
|
|
|
|
authors_ordered = list()
|
|
|
|
error = False
|
|
|
|
for auth in sort_authors:
|
|
|
|
# ToDo: How to handle not found authorname
|
|
|
|
result = db.session.query(db.Authors).filter(db.Authors.sort == auth.lstrip().strip()).first()
|
|
|
|
if not result:
|
|
|
|
error = True
|
|
|
|
break
|
|
|
|
authors_ordered.append(result)
|
|
|
|
if not error:
|
|
|
|
entry.authors = authors_ordered
|
|
|
|
return entry
|
2018-08-19 08:14:20 +00:00
|
|
|
|
2017-08-26 15:12:16 +00:00
|
|
|
# Fill indexpage with all requested data from database
|
2018-07-16 17:04:18 +00:00
|
|
|
def fill_indexpage(page, database, db_filter, order, *join):
|
2017-01-28 19:16:40 +00:00
|
|
|
if current_user.show_detail_random():
|
2018-08-28 07:42:19 +00:00
|
|
|
randm = db.session.query(db.Books).filter(common_filters())\
|
2017-08-08 16:52:00 +00:00
|
|
|
.order_by(func.random()).limit(config.config_random_books)
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
2018-08-28 07:42:19 +00:00
|
|
|
randm = false()
|
2017-01-22 15:44:37 +00:00
|
|
|
off = int(int(config.config_books_per_page) * (page - 1))
|
|
|
|
pagination = Pagination(page, config.config_books_per_page,
|
2017-08-08 16:52:00 +00:00
|
|
|
len(db.session.query(database)
|
2018-07-16 17:04:18 +00:00
|
|
|
.filter(db_filter).filter(common_filters()).all()))
|
|
|
|
entries = db.session.query(database).join(*join,isouter=True).filter(db_filter)\
|
|
|
|
.filter(common_filters()).order_by(*order).offset(off).limit(config.config_books_per_page).all()
|
2019-01-06 13:16:52 +00:00
|
|
|
for book in entries:
|
|
|
|
book = order_authors(book)
|
2018-08-28 07:42:19 +00:00
|
|
|
return entries, randm, pagination
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# Modifies different Database objects, first check if elements have to be added to database, than check
|
|
|
|
# if elements have to be deleted, because they are no longer used
|
2017-04-03 19:05:28 +00:00
|
|
|
def modify_database_object(input_elements, db_book_object, db_object, db_session, db_type):
|
2018-09-30 12:08:55 +00:00
|
|
|
# passing input_elements not as a list may lead to undesired results
|
2018-10-01 08:45:51 +00:00
|
|
|
if not isinstance(input_elements, list):
|
2018-09-30 12:08:55 +00:00
|
|
|
raise TypeError(str(input_elements) + " should be passed as a list")
|
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
input_elements = [x for x in input_elements if x != '']
|
|
|
|
# we have all input element (authors, series, tags) names now
|
|
|
|
# 1. search for elements to remove
|
|
|
|
del_elements = []
|
|
|
|
for c_elements in db_book_object:
|
|
|
|
found = False
|
2017-04-15 09:35:05 +00:00
|
|
|
if db_type == 'languages':
|
|
|
|
type_elements = c_elements.lang_code
|
|
|
|
elif db_type == 'custom':
|
|
|
|
type_elements = c_elements.value
|
2017-04-14 18:29:11 +00:00
|
|
|
else:
|
2017-04-15 09:35:05 +00:00
|
|
|
type_elements = c_elements.name
|
2016-11-09 18:24:33 +00:00
|
|
|
for inp_element in input_elements:
|
2018-10-28 13:40:31 +00:00
|
|
|
if inp_element.lower() == type_elements.lower():
|
2019-01-05 20:44:29 +00:00
|
|
|
# if inp_element == type_elements:
|
2016-11-09 18:24:33 +00:00
|
|
|
found = True
|
2016-12-23 08:53:39 +00:00
|
|
|
break
|
2016-11-09 18:24:33 +00:00
|
|
|
# if the element was not found in the new list, add it to remove list
|
|
|
|
if not found:
|
|
|
|
del_elements.append(c_elements)
|
2018-09-30 12:08:55 +00:00
|
|
|
# 2. search for elements that need to be added
|
2016-11-09 18:24:33 +00:00
|
|
|
add_elements = []
|
|
|
|
for inp_element in input_elements:
|
|
|
|
found = False
|
|
|
|
for c_elements in db_book_object:
|
2017-04-15 09:35:05 +00:00
|
|
|
if db_type == 'languages':
|
|
|
|
type_elements = c_elements.lang_code
|
|
|
|
elif db_type == 'custom':
|
2017-04-14 18:29:11 +00:00
|
|
|
type_elements = c_elements.value
|
|
|
|
else:
|
|
|
|
type_elements = c_elements.name
|
|
|
|
if inp_element == type_elements:
|
2016-11-09 18:24:33 +00:00
|
|
|
found = True
|
2016-12-23 08:53:39 +00:00
|
|
|
break
|
2016-11-09 18:24:33 +00:00
|
|
|
if not found:
|
|
|
|
add_elements.append(inp_element)
|
|
|
|
# if there are elements to remove, we remove them now
|
|
|
|
if len(del_elements) > 0:
|
|
|
|
for del_element in del_elements:
|
|
|
|
db_book_object.remove(del_element)
|
|
|
|
if len(del_element.books) == 0:
|
|
|
|
db_session.delete(del_element)
|
|
|
|
# if there are elements to add, we add them now!
|
|
|
|
if len(add_elements) > 0:
|
2017-04-03 19:05:28 +00:00
|
|
|
if db_type == 'languages':
|
2016-12-23 08:53:39 +00:00
|
|
|
db_filter = db_object.lang_code
|
2017-04-14 18:29:11 +00:00
|
|
|
elif db_type == 'custom':
|
|
|
|
db_filter = db_object.value
|
2016-11-09 18:24:33 +00:00
|
|
|
else:
|
2016-12-23 08:53:39 +00:00
|
|
|
db_filter = db_object.name
|
2016-11-09 18:24:33 +00:00
|
|
|
for add_element in add_elements:
|
|
|
|
# check if a element with that name exists
|
2018-10-28 13:40:31 +00:00
|
|
|
db_element = db_session.query(db_object).filter(db_filter == add_element).first()
|
2016-11-09 18:24:33 +00:00
|
|
|
# if no element is found add it
|
2018-10-28 13:40:31 +00:00
|
|
|
# if new_element is None:
|
|
|
|
if db_type == 'author':
|
2018-10-30 20:47:33 +00:00
|
|
|
new_element = db_object(add_element, helper.get_sorted_author(add_element.replace('|', ',')), "")
|
2018-10-28 13:40:31 +00:00
|
|
|
elif db_type == 'series':
|
|
|
|
new_element = db_object(add_element, add_element)
|
|
|
|
elif db_type == 'custom':
|
|
|
|
new_element = db_object(value=add_element)
|
|
|
|
elif db_type == 'publisher':
|
|
|
|
new_element = db_object(add_element, None)
|
|
|
|
else: # db_type should be tag or language
|
|
|
|
new_element = db_object(add_element)
|
|
|
|
if db_element is None:
|
2016-11-09 18:24:33 +00:00
|
|
|
db_session.add(new_element)
|
2018-10-28 13:40:31 +00:00
|
|
|
db_book_object.append(new_element)
|
|
|
|
else:
|
2018-12-24 15:32:22 +00:00
|
|
|
if db_type == 'custom':
|
|
|
|
if db_element.value != add_element:
|
|
|
|
new_element.value = add_element
|
|
|
|
# new_element = db_element
|
|
|
|
elif db_type == 'languages':
|
|
|
|
if db_element.lang_code != add_element:
|
|
|
|
db_element.lang_code = add_element
|
|
|
|
# new_element = db_element
|
|
|
|
elif db_type == 'series':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element # = add_element # new_element = db_object(add_element, add_element)
|
|
|
|
db_element.sort = add_element
|
|
|
|
# new_element = db_element
|
|
|
|
elif db_type == 'author':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
db_element.sort = add_element.replace('|', ',')
|
|
|
|
# new_element = db_element
|
|
|
|
elif db_type == 'publisher':
|
|
|
|
if db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
db_element.sort = None
|
|
|
|
# new_element = db_element
|
2018-10-28 13:40:31 +00:00
|
|
|
elif db_element.name != add_element:
|
|
|
|
db_element.name = add_element
|
|
|
|
# new_element = db_element
|
|
|
|
# add element to book
|
|
|
|
db_book_object.append(db_element)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-08-12 11:31:18 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# read search results from calibre-database and return it (function is used for feed and simple search
|
2018-08-12 11:31:18 +00:00
|
|
|
def get_search_results(term):
|
|
|
|
q = list()
|
|
|
|
authorterms = re.split("[, ]+", term)
|
|
|
|
for authorterm in authorterms:
|
|
|
|
q.append(db.Books.authors.any(db.Authors.name.ilike("%" + authorterm + "%")))
|
|
|
|
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
|
|
|
db.Books.authors.any(db.Authors.name.ilike("%" + term + "%"))
|
|
|
|
|
|
|
|
return db.session.query(db.Books).filter(common_filters()).filter(
|
|
|
|
db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")),
|
|
|
|
db.Books.series.any(db.Series.name.ilike("%" + term + "%")),
|
|
|
|
db.Books.authors.any(and_(*q)),
|
|
|
|
db.Books.publishers.any(db.Publishers.name.ilike("%" + term + "%")),
|
|
|
|
db.Books.title.ilike("%" + term + "%"))).all()
|
|
|
|
|
|
|
|
|
|
|
|
def feed_search(term):
|
|
|
|
if term:
|
|
|
|
term = term.strip().lower()
|
|
|
|
entries = get_search_results( term)
|
|
|
|
entriescount = len(entries) if len(entries) > 0 else 1
|
|
|
|
pagination = Pagination(1, entriescount, entriescount)
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', searchterm=term, entries=entries, pagination=pagination)
|
2018-08-12 11:31:18 +00:00
|
|
|
else:
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', searchterm="")
|
|
|
|
|
|
|
|
|
|
|
|
def render_xml_template(*args, **kwargs):
|
|
|
|
#ToDo: return time in current timezone similar to %z
|
|
|
|
currtime = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")
|
|
|
|
xml = render_template(current_time=currtime, *args, **kwargs)
|
2018-08-12 11:31:18 +00:00
|
|
|
response = make_response(xml)
|
|
|
|
response.headers["Content-Type"] = "application/atom+xml; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2018-08-18 15:35:23 +00:00
|
|
|
# Returns the template for redering and includes the instance name
|
2017-01-22 20:30:36 +00:00
|
|
|
def render_title_template(*args, **kwargs):
|
|
|
|
return render_template(instance=config.config_calibre_web_title, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
|
|
|
g.user = current_user
|
2017-01-22 15:44:37 +00:00
|
|
|
g.allow_registration = config.config_public_reg
|
|
|
|
g.allow_upload = config.config_uploading
|
2019-01-11 07:36:28 +00:00
|
|
|
g.current_theme = config.config_theme
|
2017-11-01 15:55:51 +00:00
|
|
|
g.public_shelfes = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1).order_by(ub.Shelf.name).all()
|
2017-01-28 19:16:40 +00:00
|
|
|
if not config.db_configured and request.endpoint not in ('basic_configuration', 'login') and '/static/' not in request.path:
|
|
|
|
return redirect(url_for('basic_configuration'))
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
# Routing functions
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def feed_index():
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('index.xml')
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/osd")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def feed_osd():
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('osd.xml', lang='en-EN')
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-12 19:43:36 +00:00
|
|
|
@app.route("/opds/search/<query>")
|
2017-01-17 16:45:23 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-01-12 19:43:36 +00:00
|
|
|
def feed_cc_search(query):
|
|
|
|
return feed_search(query.strip())
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/search", methods=["GET"])
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-01-12 19:43:36 +00:00
|
|
|
def feed_normal_search():
|
|
|
|
return feed_search(request.args.get("query").strip())
|
|
|
|
|
2017-01-15 11:37:58 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/new")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def feed_new():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-04-14 13:57:27 +00:00
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-07-16 17:04:18 +00:00
|
|
|
db.Books, True, [db.Books.timestamp.desc()])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/discover")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def feed_discover():
|
2017-08-26 15:12:16 +00:00
|
|
|
entries = db.session.query(db.Books).filter(common_filters()).order_by(func.random())\
|
2017-08-08 16:52:00 +00:00
|
|
|
.limit(config.config_books_per_page)
|
2017-01-28 19:16:40 +00:00
|
|
|
pagination = Pagination(1, config.config_books_per_page, int(config.config_books_per_page))
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-02-04 13:28:18 +00:00
|
|
|
@app.route("/opds/rated")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-02-04 13:28:18 +00:00
|
|
|
def feed_best_rated():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-04-14 17:16:39 +00:00
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-07-16 17:04:18 +00:00
|
|
|
db.Books, db.Books.ratings.any(db.Ratings.rating > 9), [db.Books.timestamp.desc()])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-04 13:28:18 +00:00
|
|
|
@app.route("/opds/hot")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_hot():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-02-04 13:28:18 +00:00
|
|
|
all_books = ub.session.query(ub.Downloads, ub.func.count(ub.Downloads.book_id)).order_by(
|
|
|
|
ub.func.count(ub.Downloads.book_id).desc()).group_by(ub.Downloads.book_id)
|
|
|
|
hot_books = all_books.offset(off).limit(config.config_books_per_page)
|
|
|
|
entries = list()
|
|
|
|
for book in hot_books:
|
2017-03-07 18:10:17 +00:00
|
|
|
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first()
|
|
|
|
if downloadBook:
|
|
|
|
entries.append(
|
2017-08-26 15:12:16 +00:00
|
|
|
db.session.query(db.Books).filter(common_filters())
|
|
|
|
.filter(db.Books.id == book.Downloads.book_id).first()
|
2017-08-08 16:52:00 +00:00
|
|
|
)
|
2017-03-07 18:10:17 +00:00
|
|
|
else:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.delete_download(book.Downloads.book_id)
|
|
|
|
# ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete()
|
|
|
|
# ub.session.commit()
|
2017-02-04 13:28:18 +00:00
|
|
|
numBooks = entries.__len__()
|
2018-11-25 10:25:20 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1),
|
|
|
|
config.config_books_per_page, numBooks)
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2017-02-04 13:28:18 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/author")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_authorindex():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-08-26 15:12:16 +00:00
|
|
|
entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books).filter(common_filters())\
|
2017-02-03 12:20:35 +00:00
|
|
|
.group_by('books_authors_link.author').order_by(db.Authors.sort).limit(config.config_books_per_page).offset(off)
|
2017-01-28 19:16:40 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2017-01-07 17:08:12 +00:00
|
|
|
len(db.session.query(db.Authors).all()))
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='feed_author', pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/opds/author/<int:book_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def feed_author(book_id):
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-07-09 18:15:15 +00:00
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-07-16 17:04:18 +00:00
|
|
|
db.Books, db.Books.authors.any(db.Authors.id == book_id), [db.Books.timestamp.desc()])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2018-09-30 16:48:36 +00:00
|
|
|
@app.route("/opds/publisher")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_publisherindex():
|
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
entries = db.session.query(db.Publishers).join(db.books_publishers_link).join(db.Books).filter(common_filters())\
|
|
|
|
.group_by('books_publishers_link.publisher').order_by(db.Publishers.sort).limit(config.config_books_per_page).offset(off)
|
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(db.session.query(db.Publishers).all()))
|
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='feed_publisher', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/opds/publisher/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_publisher(book_id):
|
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-11-25 10:25:20 +00:00
|
|
|
db.Books, db.Books.publishers.any(db.Publishers.id == book_id),
|
|
|
|
[db.Books.timestamp.desc()])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/category")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_categoryindex():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-08-26 15:12:16 +00:00
|
|
|
entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books).filter(common_filters())\
|
2017-08-08 16:52:00 +00:00
|
|
|
.group_by('books_tags_link.tag').order_by(db.Tags.name).offset(off).limit(config.config_books_per_page)
|
2017-01-28 19:16:40 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2017-01-07 17:08:12 +00:00
|
|
|
len(db.session.query(db.Tags).all()))
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='feed_category', pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/opds/category/<int:book_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def feed_category(book_id):
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-09-17 05:15:08 +00:00
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-07-16 17:04:18 +00:00
|
|
|
db.Books, db.Books.tags.any(db.Tags.id == book_id), [db.Books.timestamp.desc()])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-28 14:51:58 +00:00
|
|
|
@app.route("/opds/series")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_seriesindex():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-08-26 15:12:16 +00:00
|
|
|
entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books).filter(common_filters())\
|
2017-08-08 16:52:00 +00:00
|
|
|
.group_by('books_series_link.series').order_by(db.Series.sort).offset(off).all()
|
2017-01-28 19:16:40 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2017-01-07 17:08:12 +00:00
|
|
|
len(db.session.query(db.Series).all()))
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='feed_series', pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/opds/series/<int:book_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def feed_series(book_id):
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2018-08-12 07:29:57 +00:00
|
|
|
entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
2018-07-16 17:04:18 +00:00
|
|
|
db.Books, db.Books.series.any(db.Series.id == book_id), [db.Books.series_index])
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2017-12-01 06:53:52 +00:00
|
|
|
@app.route("/opds/shelfindex/", defaults={'public': 0})
|
|
|
|
@app.route("/opds/shelfindex/<string:public>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_shelfindex(public):
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-12-01 06:53:52 +00:00
|
|
|
if public is not 0:
|
|
|
|
shelf = g.public_shelfes
|
|
|
|
number = len(shelf)
|
|
|
|
else:
|
|
|
|
shelf = g.user.shelf
|
|
|
|
number = shelf.count()
|
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
number)
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=shelf, folder='feed_shelf', pagination=pagination)
|
2017-12-01 06:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/opds/shelf/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_shelf(book_id):
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-12-01 06:53:52 +00:00
|
|
|
if current_user.is_anonymous:
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1, ub.Shelf.id == book_id).first()
|
|
|
|
else:
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id),
|
|
|
|
ub.Shelf.id == book_id),
|
|
|
|
ub.and_(ub.Shelf.is_public == 1,
|
|
|
|
ub.Shelf.id == book_id))).first()
|
|
|
|
result = list()
|
|
|
|
# user is allowed to access shelf
|
|
|
|
if shelf:
|
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == book_id).order_by(
|
|
|
|
ub.BookShelf.order.asc()).all()
|
|
|
|
for book in books_in_shelf:
|
|
|
|
cur_book = db.session.query(db.Books).filter(db.Books.id == book.book_id).first()
|
|
|
|
result.append(cur_book)
|
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(result))
|
2018-08-18 15:35:23 +00:00
|
|
|
return render_xml_template('feed.xml', entries=result, pagination=pagination)
|
2017-12-01 06:53:52 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/opds/download/<book_id>/<book_format>/")
|
2016-04-27 16:29:45 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2016-04-27 08:35:23 +00:00
|
|
|
@download_required
|
2017-04-02 08:05:07 +00:00
|
|
|
def get_opds_download_link(book_id, book_format):
|
|
|
|
book_format = book_format.split(".")[0]
|
2015-10-13 17:06:37 +00:00
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
2017-04-02 08:05:07 +00:00
|
|
|
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first()
|
2017-04-14 18:29:11 +00:00
|
|
|
app.logger.info(data.name)
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user.is_authenticated:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.update_download(book_id, int(current_user.id))
|
2016-03-26 15:12:29 +00:00
|
|
|
file_name = book.title
|
2017-02-15 17:09:17 +00:00
|
|
|
if len(book.authors) > 0:
|
2017-03-14 18:48:17 +00:00
|
|
|
file_name = book.authors[0].name + '_' + file_name
|
2017-03-06 08:35:00 +00:00
|
|
|
file_name = helper.get_valid_filename(file_name)
|
2017-04-14 18:29:11 +00:00
|
|
|
headers = Headers()
|
2018-11-25 10:25:20 +00:00
|
|
|
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf8')),
|
|
|
|
book_format)
|
2017-10-01 18:48:01 +00:00
|
|
|
try:
|
|
|
|
headers["Content-Type"] = mimetypes.types_map['.' + book_format]
|
|
|
|
except KeyError:
|
|
|
|
headers["Content-Type"] = "application/octet-stream"
|
2018-08-04 08:56:42 +00:00
|
|
|
return helper.do_download_file(book, book_format, data, headers)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-09 20:18:49 +00:00
|
|
|
@app.route("/ajax/book/<string:uuid>")
|
2017-01-15 10:05:28 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-01-09 20:18:49 +00:00
|
|
|
def get_metadata_calibre_companion(uuid):
|
2017-01-28 19:16:40 +00:00
|
|
|
entry = db.session.query(db.Books).filter(db.Books.uuid.like("%" + uuid + "%")).first()
|
|
|
|
if entry is not None:
|
|
|
|
js = render_template('json.txt', entry=entry)
|
2017-01-09 20:18:49 +00:00
|
|
|
response = make_response(js)
|
2017-01-14 12:18:35 +00:00
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
2017-01-09 20:18:49 +00:00
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return ""
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-07-30 18:12:41 +00:00
|
|
|
@app.route("/ajax/emailstat")
|
|
|
|
@login_required
|
|
|
|
def get_email_status_json():
|
2018-08-12 07:29:57 +00:00
|
|
|
tasks=helper.global_WorkerThread.get_taskstatus()
|
2018-11-18 16:09:13 +00:00
|
|
|
answer = helper.render_task_status(tasks)
|
|
|
|
js=json.dumps(answer, default=helper.json_serial)
|
2018-07-30 18:12:41 +00:00
|
|
|
response = make_response(js)
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
# checks if domain is in database (including wildcards)
|
2018-10-03 19:58:37 +00:00
|
|
|
# example SELECT * FROM @TABLE WHERE 'abcdefg' LIKE Name;
|
2018-08-24 13:48:09 +00:00
|
|
|
# from https://code.luasoftware.com/tutorials/flask/execute-raw-sql-in-flask-sqlalchemy/
|
|
|
|
def check_valid_domain(domain_text):
|
|
|
|
domain_text = domain_text.split('@',1)[-1].lower()
|
2019-02-03 15:38:27 +00:00
|
|
|
sql = "SELECT * FROM registration WHERE :domain LIKE domain;"
|
|
|
|
result = ub.session.query(ub.Registration).from_statement(text(sql)).params(domain=domain_text).all()
|
2018-08-24 13:48:09 +00:00
|
|
|
return len(result)
|
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
@app.route("/ajax/editdomain", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def edit_domain():
|
2019-01-08 06:58:22 +00:00
|
|
|
''' POST /post
|
|
|
|
name: 'username', //name of field (column in db)
|
|
|
|
pk: 1 //primary key (record id)
|
|
|
|
value: 'superuser!' //new value'''
|
2018-08-24 13:48:09 +00:00
|
|
|
vals = request.form.to_dict()
|
|
|
|
answer = ub.session.query(ub.Registration).filter(ub.Registration.id == vals['pk']).first()
|
2018-10-03 19:58:37 +00:00
|
|
|
# domain_name = request.args.get('domain')
|
2018-08-24 13:48:09 +00:00
|
|
|
answer.domain = vals['value'].replace('*','%').replace('?','_').lower()
|
|
|
|
ub.session.commit()
|
|
|
|
return ""
|
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
@app.route("/ajax/adddomain", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def add_domain():
|
|
|
|
domain_name = request.form.to_dict()['domainname'].replace('*','%').replace('?','_').lower()
|
|
|
|
check = ub.session.query(ub.Registration).filter(ub.Registration.domain == domain_name).first()
|
|
|
|
if not check:
|
|
|
|
new_domain = ub.Registration(domain=domain_name)
|
|
|
|
ub.session.add(new_domain)
|
|
|
|
ub.session.commit()
|
|
|
|
return ""
|
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
@app.route("/ajax/deletedomain", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def delete_domain():
|
|
|
|
domain_id = request.form.to_dict()['domainid'].replace('*','%').replace('?','_').lower()
|
|
|
|
ub.session.query(ub.Registration).filter(ub.Registration.id == domain_id).delete()
|
|
|
|
ub.session.commit()
|
2018-08-28 07:42:19 +00:00
|
|
|
# If last domain was deleted, add all domains by default
|
|
|
|
if not ub.session.query(ub.Registration).count():
|
|
|
|
new_domain = ub.Registration(domain="%.%")
|
|
|
|
ub.session.add(new_domain)
|
|
|
|
ub.session.commit()
|
2018-08-24 13:48:09 +00:00
|
|
|
return ""
|
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
@app.route("/ajax/domainlist")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def list_domain():
|
|
|
|
answer = ub.session.query(ub.Registration).all()
|
|
|
|
json_dumps = json.dumps([{"domain":r.domain.replace('%','*').replace('_','?'),"id":r.id} for r in answer])
|
|
|
|
js=json.dumps(json_dumps.replace('"', "'")).lstrip('"').strip('"')
|
|
|
|
response = make_response(js.replace("'",'"'))
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2018-08-31 08:47:58 +00:00
|
|
|
'''
|
2017-11-18 09:34:21 +00:00
|
|
|
@app.route("/ajax/getcomic/<int:book_id>/<book_format>/<int:page>")
|
|
|
|
@login_required
|
|
|
|
def get_comic_book(book_id, book_format, page):
|
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
|
|
|
if not book:
|
2017-11-19 19:37:43 +00:00
|
|
|
return "", 204
|
2017-11-18 09:34:21 +00:00
|
|
|
else:
|
|
|
|
for bookformat in book.data:
|
|
|
|
if bookformat.format.lower() == book_format.lower():
|
|
|
|
cbr_file = os.path.join(config.config_calibre_dir, book.path, bookformat.name) + "." + book_format
|
2017-12-15 17:14:20 +00:00
|
|
|
if book_format in ("cbr", "rar"):
|
2017-11-18 09:34:21 +00:00
|
|
|
if rar_support == True:
|
|
|
|
rarfile.UNRAR_TOOL = config.config_rarfile_location
|
|
|
|
try:
|
|
|
|
rf = rarfile.RarFile(cbr_file)
|
2018-01-11 13:43:39 +00:00
|
|
|
names = sort(rf.namelist())
|
|
|
|
extract = lambda page: rf.read(names[page])
|
2017-11-18 09:34:21 +00:00
|
|
|
except:
|
|
|
|
# rarfile not valid
|
2017-11-19 19:37:43 +00:00
|
|
|
app.logger.error('Unrar binary not found, or unable to decompress file ' + cbr_file)
|
|
|
|
return "", 204
|
2017-11-18 09:34:21 +00:00
|
|
|
else:
|
2017-11-19 17:08:55 +00:00
|
|
|
app.logger.info('Unrar is not supported please install python rarfile extension')
|
2017-11-18 09:34:21 +00:00
|
|
|
# no support means return nothing
|
2017-11-19 19:37:43 +00:00
|
|
|
return "", 204
|
2018-01-11 13:43:39 +00:00
|
|
|
elif book_format in ("cbz", "zip"):
|
2017-11-18 09:34:21 +00:00
|
|
|
zf = zipfile.ZipFile(cbr_file)
|
2018-01-11 13:43:39 +00:00
|
|
|
names=sort(zf.namelist())
|
|
|
|
extract = lambda page: zf.read(names[page])
|
|
|
|
elif book_format in ("cbt", "tar"):
|
2017-12-15 17:14:20 +00:00
|
|
|
tf = tarfile.TarFile(cbr_file)
|
2018-01-11 13:43:39 +00:00
|
|
|
names=sort(tf.getnames())
|
|
|
|
extract = lambda page: tf.extractfile(names[page]).read()
|
|
|
|
else:
|
|
|
|
app.logger.error('unsupported comic format')
|
|
|
|
return "", 204
|
|
|
|
|
|
|
|
if sys.version_info.major >= 3:
|
|
|
|
b64 = codecs.encode(extract(page), 'base64').decode()
|
|
|
|
else:
|
|
|
|
b64 = extract(page).encode('base64')
|
|
|
|
ext = names[page].rpartition('.')[-1]
|
|
|
|
if ext not in ('png', 'gif', 'jpg', 'jpeg'):
|
|
|
|
ext = 'png'
|
|
|
|
extractedfile="data:image/" + ext + ";base64," + b64
|
|
|
|
fileData={"name": names[page], "page":page, "last":len(names)-1, "content": extractedfile}
|
2017-11-18 09:34:21 +00:00
|
|
|
return make_response(json.dumps(fileData))
|
2017-11-20 06:53:52 +00:00
|
|
|
return "", 204
|
2018-08-31 08:47:58 +00:00
|
|
|
'''
|
2017-11-18 09:34:21 +00:00
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/get_authors_json", methods=['GET', 'POST'])
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2016-12-23 08:53:39 +00:00
|
|
|
def get_authors_json():
|
2016-04-15 21:35:18 +00:00
|
|
|
if request.method == "GET":
|
|
|
|
query = request.args.get('q')
|
2017-10-20 21:23:16 +00:00
|
|
|
entries = db.session.query(db.Authors).filter(db.Authors.name.ilike("%" + query + "%")).all()
|
2018-08-12 11:31:18 +00:00
|
|
|
json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries])
|
2016-04-15 21:35:18 +00:00
|
|
|
return json_dumps
|
|
|
|
|
2019-01-08 06:58:22 +00:00
|
|
|
|
2018-09-30 12:08:55 +00:00
|
|
|
@app.route("/get_publishers_json", methods=['GET', 'POST'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_publishers_json():
|
|
|
|
if request.method == "GET":
|
|
|
|
query = request.args.get('q')
|
|
|
|
entries = db.session.query(db.Publishers).filter(db.Publishers.name.ilike("%" + query + "%")).all()
|
|
|
|
json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries])
|
|
|
|
return json_dumps
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/get_tags_json", methods=['GET', 'POST'])
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2016-12-23 08:53:39 +00:00
|
|
|
def get_tags_json():
|
2016-04-15 21:35:18 +00:00
|
|
|
if request.method == "GET":
|
|
|
|
query = request.args.get('q')
|
2017-11-13 20:33:36 +00:00
|
|
|
entries = db.session.query(db.Tags).filter(db.Tags.name.ilike("%" + query + "%")).all()
|
2017-03-31 16:31:16 +00:00
|
|
|
json_dumps = json.dumps([dict(name=r.name) for r in entries])
|
2016-04-15 21:35:18 +00:00
|
|
|
return json_dumps
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/get_languages_json", methods=['GET', 'POST'])
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_languages_json():
|
|
|
|
if request.method == "GET":
|
|
|
|
query = request.args.get('q').lower()
|
2018-09-08 19:16:56 +00:00
|
|
|
# languages = speaking_language()
|
|
|
|
languages = language_table[get_locale()]
|
2018-09-09 08:13:08 +00:00
|
|
|
entries_start = [s for key, s in languages.items() if s.lower().startswith(query.lower())]
|
|
|
|
if len(entries_start) < 5:
|
|
|
|
entries = [s for key,s in languages.items() if query in s.lower()]
|
|
|
|
entries_start.extend(entries[0:(5-len(entries_start))])
|
|
|
|
entries_start = list(set(entries_start))
|
|
|
|
json_dumps = json.dumps([dict(name=r) for r in entries_start[0:5]])
|
2016-11-09 18:24:33 +00:00
|
|
|
return json_dumps
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
@app.route("/get_series_json", methods=['GET', 'POST'])
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2016-12-23 08:53:39 +00:00
|
|
|
def get_series_json():
|
2016-04-15 21:35:18 +00:00
|
|
|
if request.method == "GET":
|
|
|
|
query = request.args.get('q')
|
2017-10-20 21:23:16 +00:00
|
|
|
entries = db.session.query(db.Series).filter(db.Series.name.ilike("%" + query + "%")).all()
|
2017-03-31 16:31:16 +00:00
|
|
|
# entries = db.session.execute("select name from series where name like '%" + query + "%'")
|
|
|
|
json_dumps = json.dumps([dict(name=r.name) for r in entries])
|
2016-04-15 21:35:18 +00:00
|
|
|
return json_dumps
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/get_matching_tags", methods=['GET', 'POST'])
|
2016-05-02 20:43:50 +00:00
|
|
|
@login_required_if_no_ano
|
2016-12-23 08:53:39 +00:00
|
|
|
def get_matching_tags():
|
2016-05-02 20:43:50 +00:00
|
|
|
tag_dict = {'tags': []}
|
|
|
|
if request.method == "GET":
|
|
|
|
q = db.session.query(db.Books)
|
|
|
|
author_input = request.args.get('author_name')
|
|
|
|
title_input = request.args.get('book_title')
|
2016-05-03 12:27:38 +00:00
|
|
|
include_tag_inputs = request.args.getlist('include_tag')
|
|
|
|
exclude_tag_inputs = request.args.getlist('exclude_tag')
|
2017-10-20 21:23:16 +00:00
|
|
|
q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_input + "%")),
|
|
|
|
db.Books.title.ilike("%" + title_input + "%"))
|
2016-05-03 12:27:38 +00:00
|
|
|
if len(include_tag_inputs) > 0:
|
|
|
|
for tag in include_tag_inputs:
|
2016-05-02 20:43:50 +00:00
|
|
|
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
|
2016-05-03 12:27:38 +00:00
|
|
|
if len(exclude_tag_inputs) > 0:
|
|
|
|
for tag in exclude_tag_inputs:
|
|
|
|
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
|
2016-05-02 20:43:50 +00:00
|
|
|
for book in q:
|
|
|
|
for tag in book.tags:
|
|
|
|
if tag.id not in tag_dict['tags']:
|
|
|
|
tag_dict['tags'].append(tag.id)
|
|
|
|
json_dumps = json.dumps(tag_dict)
|
|
|
|
return json_dumps
|
2015-10-13 17:06:37 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-08-12 11:31:18 +00:00
|
|
|
@app.route("/get_update_status", methods=['GET'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_update_status():
|
2019-01-20 16:45:42 +00:00
|
|
|
return updater_thread.get_available_updates(request.method)
|
2018-08-12 11:31:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/get_updater_status", methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def get_updater_status():
|
|
|
|
status = {}
|
|
|
|
if request.method == "POST":
|
|
|
|
commit = request.form.to_dict()
|
|
|
|
if "start" in commit and commit['start'] == 'True':
|
|
|
|
text = {
|
|
|
|
"1": _(u'Requesting update package'),
|
|
|
|
"2": _(u'Downloading update package'),
|
|
|
|
"3": _(u'Unzipping update package'),
|
2018-11-01 12:44:00 +00:00
|
|
|
"4": _(u'Replacing files'),
|
2018-08-12 11:31:18 +00:00
|
|
|
"5": _(u'Database connections are closed'),
|
2018-11-01 12:44:00 +00:00
|
|
|
"6": _(u'Stopping server'),
|
2018-10-29 19:38:09 +00:00
|
|
|
"7": _(u'Update finished, please press okay and reload page'),
|
|
|
|
"8": _(u'Update failed:') + u' ' + _(u'HTTP Error'),
|
|
|
|
"9": _(u'Update failed:') + u' ' + _(u'Connection error'),
|
|
|
|
"10": _(u'Update failed:') + u' ' + _(u'Timeout while establishing connection'),
|
|
|
|
"11": _(u'Update failed:') + u' ' + _(u'General error')
|
2018-08-12 11:31:18 +00:00
|
|
|
}
|
|
|
|
status['text'] = text
|
2019-01-20 16:45:42 +00:00
|
|
|
# helper.updater_thread = helper.Updater()
|
|
|
|
updater_thread.start()
|
|
|
|
status['status'] = updater_thread.get_update_status()
|
2018-08-12 11:31:18 +00:00
|
|
|
elif request.method == "GET":
|
|
|
|
try:
|
2019-01-20 16:45:42 +00:00
|
|
|
status['status'] = updater_thread.get_update_status()
|
2018-11-01 12:47:05 +00:00
|
|
|
except AttributeError:
|
|
|
|
# thread is not active, occours after restart on update
|
2018-11-01 12:44:00 +00:00
|
|
|
status['status'] = 7
|
2018-11-01 12:47:05 +00:00
|
|
|
except Exception:
|
|
|
|
status['status'] = 11
|
2018-08-12 11:31:18 +00:00
|
|
|
return json.dumps(status)
|
|
|
|
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/", defaults={'page': 1})
|
|
|
|
@app.route('/page/<int:page>')
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def index(page):
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, True, [db.Books.timestamp.desc()])
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Recently Added Books"), page="root")
|
2017-07-06 02:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/books/newest', defaults={'page': 1})
|
|
|
|
@app.route('/books/newest/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def newest_books(page):
|
2017-11-15 06:40:17 +00:00
|
|
|
if current_user.show_sorted():
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, True, [db.Books.pubdate.desc()])
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Newest Books"), page="newest")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2017-07-06 02:09:01 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2017-07-06 02:09:01 +00:00
|
|
|
@app.route('/books/oldest', defaults={'page': 1})
|
|
|
|
@app.route('/books/oldest/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def oldest_books(page):
|
2017-11-15 06:40:17 +00:00
|
|
|
if current_user.show_sorted():
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, True, [db.Books.pubdate])
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Oldest Books"), page="oldest")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2017-07-06 02:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/books/a-z', defaults={'page': 1})
|
|
|
|
@app.route('/books/a-z/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def titles_ascending(page):
|
2017-11-15 06:40:17 +00:00
|
|
|
if current_user.show_sorted():
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, True, [db.Books.sort])
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Books (A-Z)"), page="a-z")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2017-07-06 02:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/books/z-a', defaults={'page': 1})
|
|
|
|
@app.route('/books/z-a/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def titles_descending(page):
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, True, [db.Books.sort.desc()])
|
2017-07-06 02:09:01 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Books (Z-A)"), page="z-a")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
@app.route("/hot", defaults={'page': 1})
|
|
|
|
@app.route('/hot/page/<int:page>')
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def hot_books(page):
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_hot_books():
|
|
|
|
if current_user.show_detail_random():
|
|
|
|
random = db.session.query(db.Books).filter(common_filters())\
|
|
|
|
.order_by(func.random()).limit(config.config_random_books)
|
2017-03-07 18:10:17 +00:00
|
|
|
else:
|
2018-08-28 07:42:19 +00:00
|
|
|
random = false()
|
2017-11-12 13:06:33 +00:00
|
|
|
off = int(int(config.config_books_per_page) * (page - 1))
|
|
|
|
all_books = ub.session.query(ub.Downloads, ub.func.count(ub.Downloads.book_id)).order_by(
|
|
|
|
ub.func.count(ub.Downloads.book_id).desc()).group_by(ub.Downloads.book_id)
|
|
|
|
hot_books = all_books.offset(off).limit(config.config_books_per_page)
|
|
|
|
entries = list()
|
|
|
|
for book in hot_books:
|
|
|
|
downloadBook = db.session.query(db.Books).filter(common_filters()).filter(db.Books.id == book.Downloads.book_id).first()
|
|
|
|
if downloadBook:
|
|
|
|
entries.append(downloadBook)
|
|
|
|
else:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.delete_download(book.Downloads.book_id)
|
|
|
|
# ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete()
|
|
|
|
# ub.session.commit()
|
2017-11-12 13:06:33 +00:00
|
|
|
numBooks = entries.__len__()
|
|
|
|
pagination = Pagination(page, config.config_books_per_page, numBooks)
|
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Hot Books (most downloaded)"), page="hot")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-02-04 13:28:18 +00:00
|
|
|
@app.route("/rated", defaults={'page': 1})
|
|
|
|
@app.route('/rated/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def best_rated_books(page):
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_best_rated_books():
|
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.ratings.any(db.Ratings.rating > 9),
|
2018-07-16 17:04:18 +00:00
|
|
|
[db.Books.timestamp.desc()])
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Best rated books"), page="rated")
|
2018-10-28 13:40:31 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2017-02-04 13:28:18 +00:00
|
|
|
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/discover", defaults={'page': 1})
|
|
|
|
@app.route('/discover/page/<int:page>')
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def discover(page):
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_random_books():
|
2018-07-16 17:04:18 +00:00
|
|
|
entries, __, pagination = fill_indexpage(page, db.Books, True, [func.randomblob(2)])
|
2017-11-30 15:49:46 +00:00
|
|
|
pagination = Pagination(1, config.config_books_per_page, config.config_books_per_page)
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('discover.html', entries=entries, pagination=pagination,
|
|
|
|
title=_(u"Random Books"), page="discover")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route("/author")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def author_list():
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_author():
|
|
|
|
entries = db.session.query(db.Authors, func.count('books_authors_link.book').label('count'))\
|
|
|
|
.join(db.books_authors_link).join(db.Books).filter(common_filters())\
|
|
|
|
.group_by('books_authors_link.author').order_by(db.Authors.sort).all()
|
|
|
|
for entry in entries:
|
2017-11-30 15:49:46 +00:00
|
|
|
entry.Authors.name = entry.Authors.name.replace('|', ',')
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='author',
|
2019-01-11 07:36:28 +00:00
|
|
|
title=u"Author list", page="authorlist")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/author/<int:book_id>", defaults={'page': 1})
|
2019-01-25 20:58:19 +00:00
|
|
|
@app.route("/author/<int:book_id>/<int:page>")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2017-04-14 18:29:11 +00:00
|
|
|
def author(book_id, page):
|
2017-09-17 05:15:08 +00:00
|
|
|
entries, __, pagination = fill_indexpage(page, db.Books, db.Books.authors.any(db.Authors.id == book_id),
|
2018-11-25 10:25:20 +00:00
|
|
|
[db.Series.name, db.Books.series_index],db.books_series_link, db.Series)
|
2017-08-16 16:24:44 +00:00
|
|
|
if entries is None:
|
2017-02-03 12:20:35 +00:00
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
|
|
|
|
return redirect(url_for("index"))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
name = (db.session.query(db.Authors).filter(db.Authors.id == book_id).first().name).replace('|', ',')
|
2017-08-16 16:24:44 +00:00
|
|
|
|
|
|
|
author_info = None
|
2017-08-16 16:24:44 +00:00
|
|
|
other_books = []
|
2017-08-16 16:24:44 +00:00
|
|
|
if goodreads_support and config.config_use_goodreads:
|
2018-05-31 22:28:16 +00:00
|
|
|
try:
|
|
|
|
gc = GoodreadsClient(config.config_goodreads_api_key, config.config_goodreads_api_secret)
|
|
|
|
author_info = gc.find_author(author_name=name)
|
|
|
|
other_books = get_unique_other_books(entries.all(), author_info.books)
|
2018-08-12 07:29:57 +00:00
|
|
|
except Exception:
|
2018-05-31 22:28:16 +00:00
|
|
|
# Skip goodreads, if site is down/inaccessible
|
2018-07-07 10:27:07 +00:00
|
|
|
app.logger.error('Goodreads website is down/inaccessible')
|
2017-08-16 16:24:44 +00:00
|
|
|
|
|
|
|
return render_title_template('author.html', entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=name, author=author_info, other_books=other_books, page="author")
|
2017-08-16 16:24:44 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-09-30 16:30:24 +00:00
|
|
|
@app.route("/publisher")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def publisher_list():
|
|
|
|
if current_user.show_publisher():
|
|
|
|
entries = db.session.query(db.Publishers, func.count('books_publishers_link.book').label('count'))\
|
|
|
|
.join(db.books_publishers_link).join(db.Books).filter(common_filters())\
|
|
|
|
.group_by('books_publishers_link.publisher').order_by(db.Publishers.sort).all()
|
|
|
|
return render_title_template('list.html', entries=entries, folder='publisher',
|
|
|
|
title=_(u"Publisher list"), page="publisherlist")
|
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/publisher/<int:book_id>", defaults={'page': 1})
|
|
|
|
@app.route('/publisher/<int:book_id>/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def publisher(book_id, page):
|
2018-10-28 13:40:31 +00:00
|
|
|
publisher = db.session.query(db.Publishers).filter(db.Publishers.id == book_id).first()
|
|
|
|
if publisher:
|
2018-11-25 10:25:20 +00:00
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books,
|
|
|
|
db.Books.publishers.any(db.Publishers.id == book_id),
|
|
|
|
(db.Series.name, db.Books.series_index), db.books_series_link, db.Series)
|
2018-10-28 13:40:31 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
|
|
|
title=_(u"Publisher: %(name)s", name=publisher.name), page="publisher")
|
|
|
|
else:
|
|
|
|
abort(404)
|
2018-09-30 16:30:24 +00:00
|
|
|
|
|
|
|
|
2017-08-24 15:53:53 +00:00
|
|
|
def get_unique_other_books(library_books, author_books):
|
|
|
|
# Get all identifiers (ISBN, Goodreads, etc) and filter author's books by that list so we show fewer duplicates
|
|
|
|
# Note: Not all images will be shown, even though they're available on Goodreads.com.
|
|
|
|
# See https://www.goodreads.com/topic/show/18213769-goodreads-book-images
|
|
|
|
identifiers = reduce(lambda acc, book: acc + map(lambda identifier: identifier.val, book.identifiers), library_books, [])
|
|
|
|
other_books = filter(lambda book: book.isbn not in identifiers and book.gid["#text"] not in identifiers, author_books)
|
|
|
|
|
|
|
|
# Fuzzy match book titles
|
|
|
|
if levenshtein_support:
|
|
|
|
library_titles = reduce(lambda acc, book: acc + [book.title], library_books, [])
|
|
|
|
other_books = filter(lambda author_book: not filter(
|
|
|
|
lambda library_book:
|
|
|
|
Levenshtein.ratio(re.sub(r"\(.*\)", "", author_book.title), library_book) > 0.7, # Remove items in parentheses before comparing
|
|
|
|
library_titles
|
|
|
|
), other_books)
|
|
|
|
|
|
|
|
return other_books
|
|
|
|
|
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route("/series")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2016-11-09 18:24:33 +00:00
|
|
|
def series_list():
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_series():
|
|
|
|
entries = db.session.query(db.Series, func.count('books_series_link.book').label('count'))\
|
|
|
|
.join(db.books_series_link).join(db.Books).filter(common_filters())\
|
|
|
|
.group_by('books_series_link.series').order_by(db.Series.sort).all()
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='series',
|
|
|
|
title=_(u"Series list"), page="serieslist")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/series/<int:book_id>/", defaults={'page': 1})
|
2018-11-28 17:28:55 +00:00
|
|
|
@app.route("/series/<int:book_id>/<int:page>")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def series(book_id, page):
|
2018-10-28 13:40:31 +00:00
|
|
|
name = db.session.query(db.Series).filter(db.Series.id == book_id).first()
|
|
|
|
if name:
|
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.series.any(db.Series.id == book_id),
|
2018-07-16 17:04:18 +00:00
|
|
|
[db.Books.series_index])
|
2018-11-28 17:28:55 +00:00
|
|
|
return render_title_template('index.html', random=random, pagination=pagination, entries=entries,
|
2018-10-28 13:40:31 +00:00
|
|
|
title=_(u"Series: %(serie)s", serie=name.name), page="series")
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
2018-10-28 13:40:31 +00:00
|
|
|
abort(404)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route("/language")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def language_overview():
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_language():
|
|
|
|
if current_user.filter_language() == u"all":
|
2018-08-19 08:14:20 +00:00
|
|
|
languages = speaking_language()
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
2016-11-09 18:24:33 +00:00
|
|
|
try:
|
2017-11-12 13:06:33 +00:00
|
|
|
cur_l = LC.parse(current_user.filter_language())
|
2018-09-02 09:48:58 +00:00
|
|
|
except UnknownLocaleError:
|
2017-11-12 13:06:33 +00:00
|
|
|
cur_l = None
|
|
|
|
languages = db.session.query(db.Languages).filter(
|
|
|
|
db.Languages.lang_code == current_user.filter_language()).all()
|
|
|
|
if cur_l:
|
|
|
|
languages[0].name = cur_l.get_language_name(get_locale())
|
|
|
|
else:
|
|
|
|
languages[0].name = _(isoLanguages.get(part3=languages[0].lang_code).name)
|
|
|
|
lang_counter = db.session.query(db.books_languages_link,
|
|
|
|
func.count('books_languages_link.book').label('bookcount')).group_by(
|
|
|
|
'books_languages_link.lang_code').all()
|
|
|
|
return render_title_template('languages.html', languages=languages, lang_counter=lang_counter,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Available languages"), page="langlist")
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
2017-11-12 13:06:33 +00:00
|
|
|
abort(404)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/language/<name>", defaults={'page': 1})
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route('/language/<name>/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
2016-12-23 08:53:39 +00:00
|
|
|
def language(name, page):
|
2016-11-09 18:24:33 +00:00
|
|
|
try:
|
|
|
|
cur_l = LC.parse(name)
|
2018-10-28 13:40:31 +00:00
|
|
|
lang_name = cur_l.get_language_name(get_locale())
|
2018-09-02 09:48:58 +00:00
|
|
|
except UnknownLocaleError:
|
2018-10-28 13:40:31 +00:00
|
|
|
try:
|
|
|
|
lang_name = _(isoLanguages.get(part3=name).name)
|
|
|
|
except KeyError:
|
|
|
|
abort(404)
|
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.languages.any(db.Languages.lang_code == name),
|
|
|
|
[db.Books.timestamp.desc()])
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-10-28 13:40:31 +00:00
|
|
|
title=_(u"Language: %(name)s", name=lang_name), page="language")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
@app.route("/category")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def category_list():
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_category():
|
|
|
|
entries = db.session.query(db.Tags, func.count('books_tags_link.book').label('count'))\
|
|
|
|
.join(db.books_tags_link).join(db.Books).order_by(db.Tags.name).filter(common_filters())\
|
|
|
|
.group_by('books_tags_link.tag').all()
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='category',
|
|
|
|
title=_(u"Category list"), page="catlist")
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/category/<int:book_id>", defaults={'page': 1})
|
|
|
|
@app.route('/category/<int:book_id>/<int:page>')
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def category(book_id, page):
|
2018-10-28 13:40:31 +00:00
|
|
|
name = db.session.query(db.Tags).filter(db.Tags.id == book_id).first()
|
|
|
|
if name:
|
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.tags.any(db.Tags.id == book_id),
|
|
|
|
(db.Series.name, db.Books.series_index),db.books_series_link,db.Series)
|
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
|
|
|
title=_(u"Category: %(name)s", name=name.name), page="category")
|
|
|
|
else:
|
|
|
|
abort(404)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/ajax/toggleread/<int:book_id>", methods=['POST'])
|
2017-02-19 20:08:22 +00:00
|
|
|
@login_required
|
2017-04-02 08:05:07 +00:00
|
|
|
def toggle_read(book_id):
|
2018-07-15 13:08:06 +00:00
|
|
|
if not config.config_read_column:
|
|
|
|
book = ub.session.query(ub.ReadBook).filter(ub.and_(ub.ReadBook.user_id == int(current_user.id),
|
2017-04-02 08:05:07 +00:00
|
|
|
ub.ReadBook.book_id == book_id)).first()
|
2018-07-15 13:08:06 +00:00
|
|
|
if book:
|
|
|
|
book.is_read = not book.is_read
|
|
|
|
else:
|
|
|
|
readBook = ub.ReadBook()
|
|
|
|
readBook.user_id = int(current_user.id)
|
|
|
|
readBook.book_id = book_id
|
|
|
|
readBook.is_read = True
|
|
|
|
book = readBook
|
|
|
|
ub.session.merge(book)
|
|
|
|
ub.session.commit()
|
2017-02-19 20:08:22 +00:00
|
|
|
else:
|
2018-07-15 13:08:06 +00:00
|
|
|
try:
|
|
|
|
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).filter(common_filters()).first()
|
|
|
|
read_status = getattr(book, 'custom_column_' + str(config.config_read_column))
|
|
|
|
if len(read_status):
|
|
|
|
read_status[0].value = not read_status[0].value
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
2018-07-15 13:14:26 +00:00
|
|
|
cc_class = db.cc_classes[config.config_read_column]
|
2018-07-15 13:08:06 +00:00
|
|
|
new_cc = cc_class(value=1, book=book_id)
|
|
|
|
db.session.add(new_cc)
|
|
|
|
db.session.commit()
|
|
|
|
except KeyError:
|
|
|
|
app.logger.error(
|
|
|
|
u"Custom Column No.%d is not exisiting in calibre database" % config.config_read_column)
|
|
|
|
return ""
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/book/<int:book_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def show_book(book_id):
|
2017-08-26 15:12:16 +00:00
|
|
|
entries = db.session.query(db.Books).filter(db.Books.id == book_id).filter(common_filters()).first()
|
2016-12-23 08:53:39 +00:00
|
|
|
if entries:
|
|
|
|
for index in range(0, len(entries.languages)):
|
2016-11-09 18:24:33 +00:00
|
|
|
try:
|
2016-12-23 08:53:39 +00:00
|
|
|
entries.languages[index].language_name = LC.parse(entries.languages[index].lang_code).get_language_name(
|
|
|
|
get_locale())
|
2018-09-02 09:48:58 +00:00
|
|
|
except UnknownLocaleError:
|
2016-12-23 08:53:39 +00:00
|
|
|
entries.languages[index].language_name = _(
|
|
|
|
isoLanguages.get(part3=entries.languages[index].lang_code).name)
|
2017-02-28 23:42:46 +00:00
|
|
|
tmpcc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
|
|
|
|
|
|
|
if config.config_columns_to_ignore:
|
2017-04-14 18:29:11 +00:00
|
|
|
cc = []
|
2017-02-28 23:42:46 +00:00
|
|
|
for col in tmpcc:
|
2017-04-14 18:29:11 +00:00
|
|
|
r = re.compile(config.config_columns_to_ignore)
|
2017-02-28 23:42:46 +00:00
|
|
|
if r.match(col.label):
|
2017-03-07 19:36:43 +00:00
|
|
|
cc.append(col)
|
2017-02-28 23:42:46 +00:00
|
|
|
else:
|
2017-04-14 18:29:11 +00:00
|
|
|
cc = tmpcc
|
2016-11-09 18:24:33 +00:00
|
|
|
book_in_shelfs = []
|
2017-04-02 08:05:07 +00:00
|
|
|
shelfs = ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).all()
|
2016-11-09 18:24:33 +00:00
|
|
|
for entry in shelfs:
|
|
|
|
book_in_shelfs.append(entry.shelf)
|
|
|
|
|
2017-10-10 14:18:28 +00:00
|
|
|
if not current_user.is_anonymous:
|
2018-07-15 13:08:06 +00:00
|
|
|
if not config.config_read_column:
|
|
|
|
matching_have_read_book = ub.session.query(ub.ReadBook)\
|
|
|
|
.filter(ub.and_(ub.ReadBook.user_id == int(current_user.id),
|
|
|
|
ub.ReadBook.book_id == book_id)).all()
|
|
|
|
have_read = len(matching_have_read_book) > 0 and matching_have_read_book[0].is_read
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
matching_have_read_book = getattr(entries,'custom_column_'+str(config.config_read_column))
|
|
|
|
have_read = len(matching_have_read_book) > 0 and matching_have_read_book[0].value
|
|
|
|
except KeyError:
|
|
|
|
app.logger.error(
|
|
|
|
u"Custom Column No.%d is not exisiting in calibre database" % config.config_read_column)
|
|
|
|
have_read = None
|
|
|
|
|
2017-03-02 23:56:07 +00:00
|
|
|
else:
|
2017-04-14 18:29:11 +00:00
|
|
|
have_read = None
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2018-04-22 18:43:43 +00:00
|
|
|
entries.tags = sort(entries.tags, key = lambda tag: tag.name)
|
|
|
|
|
2019-01-06 13:16:52 +00:00
|
|
|
entries = order_authors(entries)
|
|
|
|
|
2018-11-18 16:09:13 +00:00
|
|
|
kindle_list = helper.check_send_to_kindle(entries)
|
2018-11-25 10:25:20 +00:00
|
|
|
reader_list = helper.check_read_formats(entries)
|
2018-10-01 18:19:29 +00:00
|
|
|
|
2017-07-06 16:21:35 +00:00
|
|
|
return render_title_template('detail.html', entry=entries, cc=cc, is_xhr=request.is_xhr,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=entries.title, books_shelfs=book_in_shelfs,
|
2018-11-25 10:25:20 +00:00
|
|
|
have_read=have_read, kindle_list=kindle_list, reader_list=reader_list, page="book")
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for("index"))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
@app.route("/ajax/bookmark/<int:book_id>/<book_format>", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def bookmark(book_id, book_format):
|
|
|
|
bookmark_key = request.form["bookmark"]
|
|
|
|
ub.session.query(ub.Bookmark).filter(ub.and_(ub.Bookmark.user_id == int(current_user.id),
|
|
|
|
ub.Bookmark.book_id == book_id,
|
|
|
|
ub.Bookmark.format == book_format)).delete()
|
|
|
|
if not bookmark_key:
|
|
|
|
ub.session.commit()
|
|
|
|
return "", 204
|
|
|
|
|
2018-08-19 11:36:04 +00:00
|
|
|
lbookmark = ub.Bookmark(user_id=current_user.id,
|
2017-08-23 15:52:52 +00:00
|
|
|
book_id=book_id,
|
|
|
|
format=book_format,
|
|
|
|
bookmark_key=bookmark_key)
|
2018-08-19 11:36:04 +00:00
|
|
|
ub.session.merge(lbookmark)
|
2017-08-23 15:52:52 +00:00
|
|
|
ub.session.commit()
|
|
|
|
return "", 201
|
|
|
|
|
2018-08-31 08:47:58 +00:00
|
|
|
|
2018-07-30 18:12:41 +00:00
|
|
|
@app.route("/tasks")
|
|
|
|
@login_required
|
|
|
|
def get_tasks_status():
|
|
|
|
# if current user admin, show all email, otherwise only own emails
|
|
|
|
answer=list()
|
2018-11-03 12:43:38 +00:00
|
|
|
# UIanswer=list()
|
2018-08-12 07:29:57 +00:00
|
|
|
tasks=helper.global_WorkerThread.get_taskstatus()
|
2018-11-03 12:43:38 +00:00
|
|
|
# answer = tasks
|
2018-07-30 18:12:41 +00:00
|
|
|
|
2018-11-03 12:43:38 +00:00
|
|
|
# UIanswer = copy.deepcopy(answer)
|
|
|
|
answer = helper.render_task_status(tasks)
|
2018-07-30 18:12:41 +00:00
|
|
|
# foreach row format row
|
2019-01-11 21:24:48 +00:00
|
|
|
return render_title_template('tasks.html', entries=answer, title=_(u"Tasks"), page="tasks")
|
2018-07-30 18:12:41 +00:00
|
|
|
|
2017-08-23 15:52:52 +00:00
|
|
|
|
2017-01-02 17:52:33 +00:00
|
|
|
@app.route("/admin")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required
|
2017-01-02 17:52:33 +00:00
|
|
|
def admin_forbidden():
|
2015-10-13 16:07:17 +00:00
|
|
|
abort(403)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route("/stats")
|
|
|
|
@login_required
|
|
|
|
def stats():
|
2018-07-18 18:21:44 +00:00
|
|
|
counter = db.session.query(db.Books).count()
|
|
|
|
authors = db.session.query(db.Authors).count()
|
|
|
|
categorys = db.session.query(db.Tags).count()
|
|
|
|
series = db.session.query(db.Series).count()
|
2018-07-15 16:50:35 +00:00
|
|
|
versions = uploader.book_formats.get_versions()
|
2018-09-29 17:21:04 +00:00
|
|
|
versions['Babel'] = 'v' + babelVersion
|
|
|
|
versions['Sqlalchemy'] = 'v' + sqlalchemyVersion
|
|
|
|
versions['Werkzeug'] = 'v' + werkzeugVersion
|
|
|
|
versions['Jinja2'] = 'v' + jinja2Version
|
2018-09-30 07:43:20 +00:00
|
|
|
versions['Flask'] = 'v' + flaskVersion
|
|
|
|
versions['Flask Login'] = 'v' + flask_loginVersion
|
|
|
|
versions['Flask Principal'] = 'v' + flask_principalVersion
|
|
|
|
versions['Iso 639'] = 'v' + iso639Version
|
|
|
|
versions['pytz'] = 'v' + pytzVersion
|
|
|
|
|
|
|
|
versions['Requests'] = 'v' + requests.__version__
|
|
|
|
versions['pySqlite'] = 'v' + db.engine.dialect.dbapi.version
|
|
|
|
versions['Sqlite'] = 'v' + db.engine.dialect.dbapi.sqlite_version
|
2018-07-18 18:21:44 +00:00
|
|
|
versions.update(converter.versioncheck())
|
2018-07-09 16:47:36 +00:00
|
|
|
versions.update(server.Server.getNameVersion())
|
2018-07-18 18:21:44 +00:00
|
|
|
versions['Python'] = sys.version
|
2017-01-28 19:16:40 +00:00
|
|
|
return render_title_template('stats.html', bookcounter=counter, authorcounter=authors, versions=versions,
|
2018-07-15 16:50:35 +00:00
|
|
|
categorycounter=categorys, seriecounter=series, title=_(u"Statistics"), page="stat")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2018-08-04 16:22:43 +00:00
|
|
|
@app.route("/delete/<int:book_id>/", defaults={'book_format': ""})
|
|
|
|
@app.route("/delete/<int:book_id>/<string:book_format>/")
|
2017-04-14 18:29:11 +00:00
|
|
|
@login_required
|
2018-08-04 16:22:43 +00:00
|
|
|
def delete_book(book_id, book_format):
|
2017-04-14 18:29:11 +00:00
|
|
|
if current_user.role_delete_books():
|
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
|
|
|
if book:
|
2018-08-04 16:22:43 +00:00
|
|
|
helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper())
|
|
|
|
if not book_format:
|
|
|
|
# delete book from Shelfs, Downloads, Read list
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete()
|
|
|
|
ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id).delete()
|
|
|
|
ub.delete_download(book_id)
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
# check if only this book links to:
|
|
|
|
# author, language, series, tags, custom columns
|
|
|
|
modify_database_object([u''], book.authors, db.Authors, db.session, 'author')
|
|
|
|
modify_database_object([u''], book.tags, db.Tags, db.session, 'tags')
|
|
|
|
modify_database_object([u''], book.series, db.Series, db.session, 'series')
|
|
|
|
modify_database_object([u''], book.languages, db.Languages, db.session, 'languages')
|
2018-09-19 18:38:40 +00:00
|
|
|
modify_database_object([u''], book.publishers, db.Publishers, db.session, 'publishers')
|
2018-08-04 16:22:43 +00:00
|
|
|
|
|
|
|
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
|
|
|
for c in cc:
|
|
|
|
cc_string = "custom_column_" + str(c.id)
|
|
|
|
if not c.is_multiple:
|
|
|
|
if len(getattr(book, cc_string)) > 0:
|
|
|
|
if c.datatype == 'bool' or c.datatype == 'integer':
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
2017-04-15 16:07:53 +00:00
|
|
|
db.session.delete(del_cc)
|
2018-08-04 16:22:43 +00:00
|
|
|
elif c.datatype == 'rating':
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
if len(del_cc.books) == 0:
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
else:
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
else:
|
2018-11-25 10:25:20 +00:00
|
|
|
modify_database_object([u''], getattr(book, cc_string), db.cc_classes[c.id],
|
|
|
|
db.session, 'custom')
|
2018-08-04 16:22:43 +00:00
|
|
|
db.session.query(db.Books).filter(db.Books.id == book_id).delete()
|
|
|
|
else:
|
|
|
|
db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format).delete()
|
2017-04-15 09:35:05 +00:00
|
|
|
db.session.commit()
|
2017-04-14 18:29:11 +00:00
|
|
|
else:
|
2018-08-04 16:22:43 +00:00
|
|
|
# book not found
|
2017-11-12 17:08:13 +00:00
|
|
|
app.logger.info('Book with id "'+str(book_id)+'" could not be deleted')
|
2018-08-04 16:22:43 +00:00
|
|
|
if book_format:
|
|
|
|
return redirect(url_for('edit_book', book_id=book_id))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.route("/gdrive/authenticate")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def authenticate_google_drive():
|
2019-02-03 15:38:27 +00:00
|
|
|
try:
|
|
|
|
authUrl = gdriveutils.Gauth.Instance().auth.GetAuthUrl()
|
|
|
|
except gdriveutils.InvalidConfigError:
|
|
|
|
flash(_(u'Google Drive setup not completed, try to deactivate and activate Google Drive again'),
|
|
|
|
category="error")
|
|
|
|
return redirect(url_for('index'))
|
2017-02-20 18:34:37 +00:00
|
|
|
return redirect(authUrl)
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.route("/gdrive/callback")
|
|
|
|
def google_drive_callback():
|
|
|
|
auth_code = request.args.get('code')
|
2018-10-03 19:18:08 +00:00
|
|
|
if not auth_code:
|
|
|
|
abort(403)
|
2018-08-16 19:17:26 +00:00
|
|
|
try:
|
|
|
|
credentials = gdriveutils.Gauth.Instance().auth.flow.step2_exchange(auth_code)
|
|
|
|
with open(os.path.join(config.get_main_dir,'gdrive_credentials'), 'w') as f:
|
|
|
|
f.write(credentials.to_json())
|
|
|
|
except ValueError as error:
|
|
|
|
app.logger.error(error)
|
2018-08-19 08:14:20 +00:00
|
|
|
return redirect(url_for('configuration'))
|
2017-02-20 18:34:37 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.route("/gdrive/watch/subscribe")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def watch_gdrive():
|
|
|
|
if not config.config_google_drive_watch_changes_response:
|
2018-07-07 09:41:39 +00:00
|
|
|
with open(os.path.join(config.get_main_dir,'client_secrets.json'), 'r') as settings:
|
2018-06-02 08:59:34 +00:00
|
|
|
filedata = json.load(settings)
|
|
|
|
if filedata['web']['redirect_uris'][0].endswith('/'):
|
|
|
|
filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-((len('/gdrive/callback')+1))]
|
|
|
|
else:
|
|
|
|
filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-(len('/gdrive/callback'))]
|
|
|
|
address = '%s/gdrive/watch/callback' % filedata['web']['redirect_uris'][0]
|
2017-04-14 18:29:11 +00:00
|
|
|
notification_id = str(uuid4())
|
2018-06-02 08:59:34 +00:00
|
|
|
try:
|
2018-07-14 06:31:52 +00:00
|
|
|
result = gdriveutils.watchChange(gdriveutils.Gdrive.Instance().drive, notification_id,
|
2017-02-20 18:34:37 +00:00
|
|
|
'web_hook', address, gdrive_watch_callback_token, current_milli_time() + 604800*1000)
|
2018-06-02 08:59:34 +00:00
|
|
|
settings = ub.session.query(ub.Settings).first()
|
|
|
|
settings.config_google_drive_watch_changes_response = json.dumps(result)
|
|
|
|
ub.session.merge(settings)
|
|
|
|
ub.session.commit()
|
|
|
|
settings = ub.session.query(ub.Settings).first()
|
|
|
|
config.loadSettings()
|
|
|
|
except HttpError as e:
|
|
|
|
reason=json.loads(e.content)['error']['errors'][0]
|
|
|
|
if reason['reason'] == u'push.webhookUrlUnauthorized':
|
|
|
|
flash(_(u'Callback domain is not verified, please follow steps to verify domain in google developer console'), category="error")
|
|
|
|
else:
|
|
|
|
flash(reason['message'], category="error")
|
2017-02-20 18:34:37 +00:00
|
|
|
|
|
|
|
return redirect(url_for('configuration'))
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.route("/gdrive/watch/revoke")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def revoke_watch_gdrive():
|
2017-04-14 18:29:11 +00:00
|
|
|
last_watch_response = config.config_google_drive_watch_changes_response
|
2017-02-20 18:34:37 +00:00
|
|
|
if last_watch_response:
|
2017-03-12 00:24:53 +00:00
|
|
|
try:
|
2018-11-25 10:25:20 +00:00
|
|
|
gdriveutils.stopChannel(gdriveutils.Gdrive.Instance().drive, last_watch_response['id'],
|
|
|
|
last_watch_response['resourceId'])
|
2017-03-19 16:14:16 +00:00
|
|
|
except HttpError:
|
2017-03-12 00:24:53 +00:00
|
|
|
pass
|
2017-02-20 18:34:37 +00:00
|
|
|
settings = ub.session.query(ub.Settings).first()
|
2017-04-14 18:29:11 +00:00
|
|
|
settings.config_google_drive_watch_changes_response = None
|
2017-02-20 18:34:37 +00:00
|
|
|
ub.session.merge(settings)
|
|
|
|
ub.session.commit()
|
|
|
|
config.loadSettings()
|
|
|
|
return redirect(url_for('configuration'))
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-20 18:34:37 +00:00
|
|
|
@app.route("/gdrive/watch/callback", methods=['GET', 'POST'])
|
|
|
|
def on_received_watch_confirmation():
|
2018-07-07 13:48:50 +00:00
|
|
|
app.logger.debug(request.headers)
|
2017-02-20 18:34:37 +00:00
|
|
|
if request.headers.get('X-Goog-Channel-Token') == gdrive_watch_callback_token \
|
2017-04-14 18:29:11 +00:00
|
|
|
and request.headers.get('X-Goog-Resource-State') == 'change' \
|
|
|
|
and request.data:
|
2017-02-20 18:34:37 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
data = request.data
|
2017-02-20 18:34:37 +00:00
|
|
|
|
|
|
|
def updateMetaData():
|
2017-04-14 18:29:11 +00:00
|
|
|
app.logger.info('Change received from gdrive')
|
2018-07-07 13:48:50 +00:00
|
|
|
app.logger.debug(data)
|
2017-02-20 18:34:37 +00:00
|
|
|
try:
|
2017-04-14 18:29:11 +00:00
|
|
|
j = json.loads(data)
|
|
|
|
app.logger.info('Getting change details')
|
2018-07-14 06:31:52 +00:00
|
|
|
response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id'])
|
2018-07-07 13:48:50 +00:00
|
|
|
app.logger.debug(response)
|
2017-02-20 18:34:37 +00:00
|
|
|
if response:
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
|
2019-02-03 15:38:27 +00:00
|
|
|
if not response['deleted'] and response['file']['title'] == 'metadata.db' and response['file']['md5Checksum'] != hashlib.md5(dbpath):
|
2017-04-14 18:29:11 +00:00
|
|
|
tmpDir = tempfile.gettempdir()
|
|
|
|
app.logger.info('Database file updated')
|
|
|
|
copyfile(dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time())))
|
|
|
|
app.logger.info('Backing up existing and downloading updated metadata.db')
|
2018-07-14 06:31:52 +00:00
|
|
|
gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db"))
|
2017-04-14 18:29:11 +00:00
|
|
|
app.logger.info('Setting up new DB')
|
2018-07-07 14:27:21 +00:00
|
|
|
# prevent error on windows, as os.rename does on exisiting files
|
2018-08-04 08:56:42 +00:00
|
|
|
move(os.path.join(tmpDir, "tmp_metadata.db"), dbpath)
|
2017-02-20 18:34:37 +00:00
|
|
|
db.setup_db()
|
2017-04-02 08:05:07 +00:00
|
|
|
except Exception as e:
|
2018-07-14 06:31:52 +00:00
|
|
|
app.logger.info(e.message)
|
2018-07-14 11:48:51 +00:00
|
|
|
app.logger.exception(e)
|
2017-02-20 18:34:37 +00:00
|
|
|
updateMetaData()
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/shutdown")
|
2017-01-17 16:45:23 +00:00
|
|
|
@login_required
|
2017-01-22 15:44:37 +00:00
|
|
|
@admin_required
|
2016-12-23 08:53:39 +00:00
|
|
|
def shutdown():
|
2017-01-22 15:44:37 +00:00
|
|
|
task = int(request.args.get("parameter").strip())
|
2017-01-28 19:16:40 +00:00
|
|
|
if task == 1 or task == 0: # valid commandos received
|
2017-01-22 15:44:37 +00:00
|
|
|
# close all database connections
|
|
|
|
db.session.close()
|
|
|
|
db.engine.dispose()
|
|
|
|
ub.session.close()
|
|
|
|
ub.engine.dispose()
|
2018-07-09 16:47:36 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
showtext = {}
|
2017-01-22 15:44:37 +00:00
|
|
|
if task == 0:
|
2017-02-20 18:52:00 +00:00
|
|
|
showtext['text'] = _(u'Server restarted, please reload page')
|
2018-07-09 16:47:36 +00:00
|
|
|
server.Server.setRestartTyp(True)
|
2017-01-22 15:44:37 +00:00
|
|
|
else:
|
2017-01-28 19:16:40 +00:00
|
|
|
showtext['text'] = _(u'Performing shutdown of server, please close window')
|
2018-07-09 16:47:36 +00:00
|
|
|
server.Server.setRestartTyp(False)
|
2018-09-08 12:58:00 +00:00
|
|
|
# stop gevent/tornado server
|
2018-07-09 16:47:36 +00:00
|
|
|
server.Server.stopServer()
|
2017-01-28 19:16:40 +00:00
|
|
|
return json.dumps(showtext)
|
2017-01-22 15:44:37 +00:00
|
|
|
else:
|
2017-03-07 18:10:17 +00:00
|
|
|
if task == 2:
|
|
|
|
db.session.close()
|
|
|
|
db.engine.dispose()
|
|
|
|
db.setup_db()
|
|
|
|
return json.dumps({})
|
2017-01-22 15:44:37 +00:00
|
|
|
abort(404)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/search", methods=["GET"])
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def search():
|
2017-10-21 19:50:47 +00:00
|
|
|
term = request.args.get("query").strip().lower()
|
2015-08-02 18:59:11 +00:00
|
|
|
if term:
|
2018-08-12 11:31:18 +00:00
|
|
|
entries = get_search_results(term)
|
2018-09-03 17:12:15 +00:00
|
|
|
ids = list()
|
|
|
|
for element in entries:
|
|
|
|
ids.append(element.id)
|
|
|
|
ub.searched_ids[current_user.id] = ids
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('search.html', searchterm=term, entries=entries, page="search")
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('search.html', searchterm="", page="search")
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
@app.route("/advanced_search", methods=['GET'])
|
2016-05-02 20:43:50 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def advanced_search():
|
2018-08-12 16:21:57 +00:00
|
|
|
# Build custom columns names
|
|
|
|
tmpcc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
|
|
|
if config.config_columns_to_ignore:
|
|
|
|
cc = []
|
|
|
|
for col in tmpcc:
|
|
|
|
r = re.compile(config.config_columns_to_ignore)
|
|
|
|
if r.match(col.label):
|
|
|
|
cc.append(col)
|
|
|
|
else:
|
|
|
|
cc = tmpcc
|
|
|
|
|
|
|
|
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
|
|
|
q = db.session.query(db.Books)
|
|
|
|
# postargs = request.form.to_dict()
|
|
|
|
|
|
|
|
include_tag_inputs = request.args.getlist('include_tag')
|
|
|
|
exclude_tag_inputs = request.args.getlist('exclude_tag')
|
|
|
|
include_series_inputs = request.args.getlist('include_serie')
|
|
|
|
exclude_series_inputs = request.args.getlist('exclude_serie')
|
|
|
|
include_languages_inputs = request.args.getlist('include_language')
|
|
|
|
exclude_languages_inputs = request.args.getlist('exclude_language')
|
|
|
|
|
|
|
|
author_name = request.args.get("author_name")
|
|
|
|
book_title = request.args.get("book_title")
|
|
|
|
publisher = request.args.get("publisher")
|
|
|
|
pub_start = request.args.get("Publishstart")
|
|
|
|
pub_end = request.args.get("Publishend")
|
|
|
|
rating_low = request.args.get("ratinghigh")
|
|
|
|
rating_high = request.args.get("ratinglow")
|
|
|
|
description = request.args.get("comment")
|
|
|
|
if author_name: author_name = author_name.strip().lower().replace(',','|')
|
|
|
|
if book_title: book_title = book_title.strip().lower()
|
|
|
|
if publisher: publisher = publisher.strip().lower()
|
|
|
|
|
|
|
|
searchterm = []
|
|
|
|
cc_present = False
|
|
|
|
for c in cc:
|
|
|
|
if request.args.get('custom_column_' + str(c.id)):
|
2018-09-08 10:28:48 +00:00
|
|
|
searchterm.extend([(u"%s: %s" % (c.name, request.args.get('custom_column_' + str(c.id))))])
|
2018-08-12 16:21:57 +00:00
|
|
|
cc_present = True
|
|
|
|
|
|
|
|
if include_tag_inputs or exclude_tag_inputs or include_series_inputs or exclude_series_inputs or \
|
|
|
|
include_languages_inputs or exclude_languages_inputs or author_name or book_title or \
|
|
|
|
publisher or pub_start or pub_end or rating_low or rating_high or description or cc_present:
|
|
|
|
searchterm = []
|
|
|
|
searchterm.extend((author_name.replace('|',','), book_title, publisher))
|
|
|
|
if pub_start:
|
|
|
|
try:
|
2018-09-08 10:28:48 +00:00
|
|
|
searchterm.extend([_(u"Published after ") +
|
2018-08-12 16:21:57 +00:00
|
|
|
format_date(datetime.datetime.strptime(pub_start,"%Y-%m-%d"),
|
2018-09-08 10:28:48 +00:00
|
|
|
format='medium', locale=get_locale())])
|
2018-08-12 16:21:57 +00:00
|
|
|
except ValueError:
|
|
|
|
pub_start = u""
|
|
|
|
if pub_end:
|
|
|
|
try:
|
|
|
|
searchterm.extend([_(u"Published before ") +
|
|
|
|
format_date(datetime.datetime.strptime(pub_end,"%Y-%m-%d"),
|
|
|
|
format='medium', locale=get_locale())])
|
|
|
|
except ValueError:
|
|
|
|
pub_start = u""
|
|
|
|
tag_names = db.session.query(db.Tags).filter(db.Tags.id.in_(include_tag_inputs)).all()
|
|
|
|
searchterm.extend(tag.name for tag in tag_names)
|
|
|
|
serie_names = db.session.query(db.Series).filter(db.Series.id.in_(include_series_inputs)).all()
|
|
|
|
searchterm.extend(serie.name for serie in serie_names)
|
|
|
|
language_names = db.session.query(db.Languages).filter(db.Languages.id.in_(include_languages_inputs)).all()
|
2018-08-19 08:14:20 +00:00
|
|
|
if language_names:
|
|
|
|
language_names = speaking_language(language_names)
|
2018-08-12 16:21:57 +00:00
|
|
|
searchterm.extend(language.name for language in language_names)
|
|
|
|
if rating_high:
|
2018-09-08 10:28:48 +00:00
|
|
|
searchterm.extend([_(u"Rating <= %(rating)s", rating=rating_high)])
|
2018-08-12 16:21:57 +00:00
|
|
|
if rating_low:
|
2018-09-08 10:28:48 +00:00
|
|
|
searchterm.extend([_(u"Rating >= %(rating)s", rating=rating_low)])
|
2018-08-12 16:21:57 +00:00
|
|
|
# handle custom columns
|
|
|
|
for c in cc:
|
|
|
|
if request.args.get('custom_column_' + str(c.id)):
|
2018-09-08 10:28:48 +00:00
|
|
|
searchterm.extend([(u"%s: %s" % (c.name, request.args.get('custom_column_' + str(c.id))))])
|
2018-08-12 16:21:57 +00:00
|
|
|
searchterm = " + ".join(filter(None, searchterm))
|
|
|
|
q = q.filter()
|
|
|
|
if author_name:
|
|
|
|
q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_name + "%")))
|
|
|
|
if book_title:
|
|
|
|
q = q.filter(db.Books.title.ilike("%" + book_title + "%"))
|
|
|
|
if pub_start:
|
|
|
|
q = q.filter(db.Books.pubdate >= pub_start)
|
|
|
|
if pub_end:
|
|
|
|
q = q.filter(db.Books.pubdate <= pub_end)
|
|
|
|
if publisher:
|
|
|
|
q = q.filter(db.Books.publishers.any(db.Publishers.name.ilike("%" + publisher + "%")))
|
|
|
|
for tag in include_tag_inputs:
|
|
|
|
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
|
|
|
|
for tag in exclude_tag_inputs:
|
|
|
|
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
|
|
|
|
for serie in include_series_inputs:
|
|
|
|
q = q.filter(db.Books.series.any(db.Series.id == serie))
|
|
|
|
for serie in exclude_series_inputs:
|
|
|
|
q = q.filter(not_(db.Books.series.any(db.Series.id == serie)))
|
|
|
|
if current_user.filter_language() != "all":
|
|
|
|
q = q.filter(db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()))
|
|
|
|
else:
|
|
|
|
for language in include_languages_inputs:
|
|
|
|
q = q.filter(db.Books.languages.any(db.Languages.id == language))
|
|
|
|
for language in exclude_languages_inputs:
|
|
|
|
q = q.filter(not_(db.Books.series.any(db.Languages.id == language)))
|
|
|
|
if rating_high:
|
2018-12-02 11:48:12 +00:00
|
|
|
rating_high = int(rating_high) * 2
|
|
|
|
q = q.filter(db.Books.ratings.any(db.Ratings.rating <= rating_high))
|
2018-08-12 16:21:57 +00:00
|
|
|
if rating_low:
|
2018-12-02 11:48:12 +00:00
|
|
|
rating_low = int(rating_low) *2
|
|
|
|
q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low))
|
2018-08-12 16:21:57 +00:00
|
|
|
if description:
|
|
|
|
q = q.filter(db.Books.comments.any(db.Comments.text.ilike("%" + description + "%")))
|
|
|
|
|
|
|
|
# search custom culumns
|
|
|
|
for c in cc:
|
|
|
|
custom_query = request.args.get('custom_column_' + str(c.id))
|
|
|
|
if custom_query:
|
|
|
|
if c.datatype == 'bool':
|
|
|
|
getattr(db.Books, 'custom_column_1')
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_'+str(c.id)).any(
|
|
|
|
db.cc_classes[c.id].value == (custom_query== "True") ))
|
|
|
|
elif c.datatype == 'int':
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_'+str(c.id)).any(
|
|
|
|
db.cc_classes[c.id].value == custom_query ))
|
|
|
|
else:
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_'+str(c.id)).any(
|
|
|
|
db.cc_classes[c.id].value.ilike("%" + custom_query + "%")))
|
|
|
|
q = q.all()
|
2018-08-19 08:14:20 +00:00
|
|
|
ids = list()
|
|
|
|
for element in q:
|
|
|
|
ids.append(element.id)
|
|
|
|
ub.searched_ids[current_user.id] = ids
|
2018-08-12 16:21:57 +00:00
|
|
|
return render_title_template('search.html', searchterm=searchterm,
|
|
|
|
entries=q, title=_(u"search"), page="search")
|
2018-08-16 19:17:26 +00:00
|
|
|
# prepare data for search-form
|
2016-05-02 20:43:50 +00:00
|
|
|
tags = db.session.query(db.Tags).order_by(db.Tags.name).all()
|
2016-12-27 15:07:25 +00:00
|
|
|
series = db.session.query(db.Series).order_by(db.Series.name).all()
|
|
|
|
if current_user.filter_language() == u"all":
|
2018-08-19 08:14:20 +00:00
|
|
|
languages = speaking_language()
|
2016-12-27 15:07:25 +00:00
|
|
|
else:
|
2017-01-28 19:16:40 +00:00
|
|
|
languages = None
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('search_form.html', tags=tags, languages=languages,
|
2018-08-12 16:21:57 +00:00
|
|
|
series=series, title=_(u"search"), cc=cc, page="advsearch")
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-02-03 19:29:16 +00:00
|
|
|
@app.route("/cover/<book_id>")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2019-02-03 19:29:16 +00:00
|
|
|
def get_cover(book_id):
|
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
|
|
|
return helper.get_book_cover(book.path)
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2018-08-19 11:36:04 +00:00
|
|
|
|
2017-05-25 06:46:33 +00:00
|
|
|
@app.route("/show/<book_id>/<book_format>")
|
|
|
|
@login_required_if_no_ano
|
2017-11-30 15:49:46 +00:00
|
|
|
def serve_book(book_id, book_format):
|
2017-05-25 06:46:33 +00:00
|
|
|
book_format = book_format.split(".")[0]
|
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
|
|
|
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first()
|
|
|
|
app.logger.info(data.name)
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
headers = Headers()
|
|
|
|
try:
|
|
|
|
headers["Content-Type"] = mimetypes.types_map['.' + book_format]
|
|
|
|
except KeyError:
|
|
|
|
headers["Content-Type"] = "application/octet-stream"
|
2018-07-14 06:31:52 +00:00
|
|
|
df = gdriveutils.getFileFromEbooksFolder(book.path, data.name + "." + book_format)
|
2018-08-19 11:36:04 +00:00
|
|
|
return gdriveutils.do_gdrive_download(df, headers)
|
2017-05-25 06:46:33 +00:00
|
|
|
else:
|
|
|
|
return send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format)
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2019-02-03 19:29:16 +00:00
|
|
|
@app.route("/opds/thumb_240_240/<book_id>")
|
|
|
|
@app.route("/opds/cover_240_240/<book_id>")
|
|
|
|
@app.route("/opds/cover_90_90/<book_id>")
|
|
|
|
@app.route("/opds/cover/<book_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2017-01-07 17:08:12 +00:00
|
|
|
def feed_get_cover(book_id):
|
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
2018-08-04 08:56:42 +00:00
|
|
|
return helper.get_book_cover(book.path)
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
def render_read_books(page, are_read, as_xml=False):
|
2018-07-15 13:08:06 +00:00
|
|
|
if not config.config_read_column:
|
|
|
|
readBooks = ub.session.query(ub.ReadBook).filter(ub.ReadBook.user_id == int(current_user.id))\
|
|
|
|
.filter(ub.ReadBook.is_read == True).all()
|
|
|
|
readBookIds = [x.book_id for x in readBooks]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
readBooks = db.session.query(db.cc_classes[config.config_read_column])\
|
|
|
|
.filter(db.cc_classes[config.config_read_column].value==True).all()
|
|
|
|
readBookIds = [x.book for x in readBooks]
|
|
|
|
except KeyError:
|
2018-07-15 16:50:35 +00:00
|
|
|
app.logger.error(u"Custom Column No.%d is not existing in calibre database" % config.config_read_column)
|
2018-08-19 11:36:04 +00:00
|
|
|
readBookIds = []
|
2018-07-15 13:08:06 +00:00
|
|
|
|
2017-11-17 17:29:29 +00:00
|
|
|
if are_read:
|
|
|
|
db_filter = db.Books.id.in_(readBookIds)
|
2017-02-19 20:08:22 +00:00
|
|
|
else:
|
2017-11-17 17:29:29 +00:00
|
|
|
db_filter = ~db.Books.id.in_(readBookIds)
|
|
|
|
|
|
|
|
entries, random, pagination = fill_indexpage(page, db.Books,
|
2018-08-19 11:36:04 +00:00
|
|
|
db_filter, [db.Books.timestamp.desc()])
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2017-02-22 22:45:19 +00:00
|
|
|
if as_xml:
|
2017-02-19 20:08:22 +00:00
|
|
|
xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
response = make_response(xml)
|
2018-08-07 16:19:08 +00:00
|
|
|
response.headers["Content-Type"] = "application/xml; charset=utf-8"
|
2017-02-19 20:08:22 +00:00
|
|
|
return response
|
|
|
|
else:
|
2017-07-07 05:33:57 +00:00
|
|
|
if are_read:
|
2017-07-08 14:47:07 +00:00
|
|
|
name = _(u'Read Books') + ' (' + str(len(readBookIds)) + ')'
|
2017-07-07 05:33:57 +00:00
|
|
|
else:
|
|
|
|
total_books = db.session.query(func.count(db.Books.id)).scalar()
|
2017-07-08 14:47:07 +00:00
|
|
|
name = _(u'Unread Books') + ' (' + str(total_books - len(readBookIds)) + ')'
|
2017-02-19 20:08:22 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(name, name=name), page="read")
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
|
|
|
|
@app.route("/opds/readbooks/")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def feed_read_books():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-02-19 20:08:22 +00:00
|
|
|
return render_read_books(int(off) / (int(config.config_books_per_page)) + 1, True, True)
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
@app.route("/readbooks/", defaults={'page': 1})
|
|
|
|
@app.route("/readbooks/<int:page>'")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def read_books(page):
|
|
|
|
return render_read_books(page, True)
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
@app.route("/opds/unreadbooks/")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def feed_unread_books():
|
2018-09-02 09:48:58 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2017-02-19 20:08:22 +00:00
|
|
|
return render_read_books(int(off) / (int(config.config_books_per_page)) + 1, False, True)
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-02-19 20:08:22 +00:00
|
|
|
@app.route("/unreadbooks/", defaults={'page': 1})
|
|
|
|
@app.route("/unreadbooks/<int:page>'")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def unread_books(page):
|
|
|
|
return render_read_books(page, False)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/read/<int:book_id>/<book_format>")
|
2017-02-25 10:14:45 +00:00
|
|
|
@login_required_if_no_ano
|
2017-04-02 08:05:07 +00:00
|
|
|
def read_book(book_id, book_format):
|
2015-08-02 18:59:11 +00:00
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
2017-08-23 15:52:52 +00:00
|
|
|
if not book:
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
|
2017-01-02 17:52:33 +00:00
|
|
|
return redirect(url_for("index"))
|
2016-10-30 10:44:02 +00:00
|
|
|
|
2018-07-07 13:48:50 +00:00
|
|
|
# check if book was downloaded before
|
2018-08-19 11:36:04 +00:00
|
|
|
lbookmark = None
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user.is_authenticated:
|
2018-08-19 11:36:04 +00:00
|
|
|
lbookmark = ub.session.query(ub.Bookmark).filter(ub.and_(ub.Bookmark.user_id == int(current_user.id),
|
2017-08-23 15:52:52 +00:00
|
|
|
ub.Bookmark.book_id == book_id,
|
|
|
|
ub.Bookmark.format == book_format.upper())).first()
|
|
|
|
if book_format.lower() == "epub":
|
2018-08-19 11:36:04 +00:00
|
|
|
return render_title_template('read.html', bookid=book_id, title=_(u"Read a Book"), bookmark=lbookmark)
|
2017-08-23 15:52:52 +00:00
|
|
|
elif book_format.lower() == "pdf":
|
|
|
|
return render_title_template('readpdf.html', pdffile=book_id, title=_(u"Read a Book"))
|
|
|
|
elif book_format.lower() == "txt":
|
|
|
|
return render_title_template('readtxt.html', txtfile=book_id, title=_(u"Read a Book"))
|
2017-09-17 10:36:32 +00:00
|
|
|
else:
|
2018-07-07 13:48:50 +00:00
|
|
|
book_dir = os.path.join(config.get_main_dir, "cps", "static", str(book_id))
|
|
|
|
if not os.path.exists(book_dir):
|
|
|
|
os.mkdir(book_dir)
|
2017-11-30 15:49:46 +00:00
|
|
|
for fileext in ["cbr", "cbt", "cbz"]:
|
2017-09-17 13:33:22 +00:00
|
|
|
if book_format.lower() == fileext:
|
2018-07-07 13:48:50 +00:00
|
|
|
all_name = str(book_id) # + "/" + book.data[0].name + "." + fileext
|
|
|
|
#tmp_file = os.path.join(book_dir, book.data[0].name) + "." + fileext
|
|
|
|
#if not os.path.exists(all_name):
|
|
|
|
# cbr_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + "." + fileext
|
|
|
|
# copyfile(cbr_file, tmp_file)
|
|
|
|
return render_title_template('readcbr.html', comicfile=all_name, title=_(u"Read a Book"),
|
|
|
|
extension=fileext)
|
2018-08-31 08:47:58 +00:00
|
|
|
'''if rar_support == True:
|
2017-11-18 09:34:21 +00:00
|
|
|
extensionList = ["cbr","cbt","cbz"]
|
|
|
|
else:
|
|
|
|
extensionList = ["cbt","cbz"]
|
|
|
|
for fileext in extensionList:
|
2017-09-17 13:33:22 +00:00
|
|
|
if book_format.lower() == fileext:
|
2018-11-03 17:37:38 +00:00
|
|
|
return render_title_template('readcbr.html', comicfile=book_id,
|
|
|
|
extension=fileext, title=_(u"Read a Book"), book=book)
|
2017-11-19 19:37:43 +00:00
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible."), category="error")
|
2018-08-31 08:47:58 +00:00
|
|
|
return redirect(url_for("index"))'''
|
2017-08-23 15:52:52 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/download/<int:book_id>/<book_format>")
|
2017-01-12 19:43:36 +00:00
|
|
|
@login_required_if_no_ano
|
2016-04-27 08:35:23 +00:00
|
|
|
@download_required
|
2017-04-02 08:05:07 +00:00
|
|
|
def get_download_link(book_id, book_format):
|
|
|
|
book_format = book_format.split(".")[0]
|
2015-08-02 18:59:11 +00:00
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
2018-11-03 17:37:38 +00:00
|
|
|
data = db.session.query(db.Data).filter(db.Data.book == book.id)\
|
|
|
|
.filter(db.Data.format == book_format.upper()).first()
|
2017-01-15 11:23:08 +00:00
|
|
|
if data:
|
2017-01-28 19:16:40 +00:00
|
|
|
# collect downloaded books only for registered user and not for anonymous user
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user.is_authenticated:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.update_download(book_id, int(current_user.id))
|
2017-01-15 11:23:08 +00:00
|
|
|
file_name = book.title
|
2017-02-15 17:09:17 +00:00
|
|
|
if len(book.authors) > 0:
|
2017-03-14 18:48:17 +00:00
|
|
|
file_name = book.authors[0].name + '_' + file_name
|
2017-01-15 11:23:08 +00:00
|
|
|
file_name = helper.get_valid_filename(file_name)
|
2017-04-14 18:29:11 +00:00
|
|
|
headers = Headers()
|
2017-03-06 23:17:57 +00:00
|
|
|
try:
|
2017-04-02 08:05:07 +00:00
|
|
|
headers["Content-Type"] = mimetypes.types_map['.' + book_format]
|
|
|
|
except KeyError:
|
|
|
|
headers["Content-Type"] = "application/octet-stream"
|
2018-11-03 17:37:38 +00:00
|
|
|
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')),
|
|
|
|
book_format)
|
2018-08-04 08:56:42 +00:00
|
|
|
return helper.do_download_file(book, book_format, data, headers)
|
2017-01-15 11:23:08 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2017-04-02 08:05:07 +00:00
|
|
|
@app.route("/download/<int:book_id>/<book_format>/<anyname>")
|
2017-03-08 05:56:13 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
@download_required
|
2017-04-02 08:05:07 +00:00
|
|
|
def get_download_link_ext(book_id, book_format, anyname):
|
|
|
|
return get_download_link(book_id, book_format)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
2015-10-13 00:30:55 +00:00
|
|
|
def register():
|
2017-01-22 20:30:36 +00:00
|
|
|
if not config.config_public_reg:
|
2015-10-13 00:30:55 +00:00
|
|
|
abort(404)
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('index'))
|
2015-10-13 00:30:55 +00:00
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2018-08-24 13:48:09 +00:00
|
|
|
if not to_save["nickname"] or not to_save["email"]:
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Please fill out all fields!"), category="error")
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('register.html', title=_(u"register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
|
2017-11-12 13:10:08 +00:00
|
|
|
existing_user = ub.session.query(ub.User).filter(func.lower(ub.User.nickname) == to_save["nickname"].lower()).first()
|
2018-08-24 13:48:09 +00:00
|
|
|
existing_email = ub.session.query(ub.User).filter(ub.User.email == to_save["email"].lower()).first()
|
2015-10-13 00:30:55 +00:00
|
|
|
if not existing_user and not existing_email:
|
|
|
|
content = ub.User()
|
2018-08-24 13:48:09 +00:00
|
|
|
# content.password = generate_password_hash(to_save["password"])
|
|
|
|
if check_valid_domain(to_save["email"]):
|
|
|
|
content.nickname = to_save["nickname"]
|
|
|
|
content.email = to_save["email"]
|
|
|
|
password = helper.generate_random_password()
|
2018-10-03 19:58:37 +00:00
|
|
|
content.password = generate_password_hash(password)
|
2018-08-24 13:48:09 +00:00
|
|
|
content.role = config.config_default_role
|
|
|
|
content.sidebar_view = config.config_default_show
|
2019-01-19 15:48:04 +00:00
|
|
|
content.mature_content = bool(config.config_default_show & ub.MATURE_CONTENT)
|
2018-08-24 13:48:09 +00:00
|
|
|
try:
|
|
|
|
ub.session.add(content)
|
|
|
|
ub.session.commit()
|
|
|
|
helper.send_registration_mail(to_save["email"],to_save["nickname"], password)
|
|
|
|
except Exception:
|
|
|
|
ub.session.rollback()
|
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
|
|
|
return render_title_template('register.html', title=_(u"register"), page="register")
|
|
|
|
else:
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Your e-mail is not allowed to register"), category="error")
|
|
|
|
app.logger.info('Registering failed for user "' + to_save['nickname'] + '" e-mail adress: ' + to_save["email"])
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('register.html', title=_(u"register"), page="register")
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Confirmation e-mail was send to your e-mail account."), category="success")
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('login'))
|
2015-10-13 00:30:55 +00:00
|
|
|
else:
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"This username or e-mail address is already in use."), category="error")
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('register.html', title=_(u"register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
|
2018-08-16 19:17:26 +00:00
|
|
|
return render_title_template('register.html', title=_(u"register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
2015-08-02 18:59:11 +00:00
|
|
|
def login():
|
2017-01-28 19:16:40 +00:00
|
|
|
if not config.db_configured:
|
2017-01-22 15:44:37 +00:00
|
|
|
return redirect(url_for('basic_configuration'))
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('index'))
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
form = request.form.to_dict()
|
2017-11-12 13:10:08 +00:00
|
|
|
user = ub.session.query(ub.User).filter(func.lower(ub.User.nickname) == form['username'].strip().lower()).first()
|
2018-07-03 17:34:29 +00:00
|
|
|
if user and check_password_hash(user.password, form['password']) and user.nickname is not "Guest":
|
2016-12-23 08:53:39 +00:00
|
|
|
login_user(user, remember=True)
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"you are now logged in as: '%(nickname)s'", nickname=user.nickname), category="success")
|
2017-07-08 01:18:03 +00:00
|
|
|
return redirect_back(url_for("index"))
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2017-11-30 15:49:46 +00:00
|
|
|
ipAdress = request.headers.get('X-Forwarded-For', request.remote_addr)
|
2017-06-07 16:08:15 +00:00
|
|
|
app.logger.info('Login failed for user "' + form['username'] + '" IP-adress: ' + ipAdress)
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Wrong Username or Password"), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2018-04-01 17:32:19 +00:00
|
|
|
# next_url = request.args.get('next')
|
|
|
|
# if next_url is None or not is_safe_url(next_url):
|
|
|
|
next_url = url_for('index')
|
2017-07-08 01:18:03 +00:00
|
|
|
|
|
|
|
return render_title_template('login.html', title=_(u"login"), next_url=next_url,
|
2018-07-15 16:50:35 +00:00
|
|
|
remote_login=config.config_remote_login, page="login")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
@login_required
|
|
|
|
def logout():
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2015-08-02 18:59:11 +00:00
|
|
|
logout_user()
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('login'))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
2017-07-08 01:18:03 +00:00
|
|
|
@app.route('/remote/login')
|
|
|
|
@remote_login_required
|
|
|
|
def remote_login():
|
|
|
|
auth_token = ub.RemoteAuthToken()
|
|
|
|
ub.session.add(auth_token)
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
verify_url = url_for('verify_token', token=auth_token.auth_token, _external=true)
|
|
|
|
|
|
|
|
return render_title_template('remote_login.html', title=_(u"login"), token=auth_token.auth_token,
|
2018-07-15 16:50:35 +00:00
|
|
|
verify_url=verify_url, page="remotelogin")
|
2017-07-08 01:18:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/verify/<token>')
|
|
|
|
@remote_login_required
|
|
|
|
@login_required
|
|
|
|
def verify_token(token):
|
|
|
|
auth_token = ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.auth_token == token).first()
|
|
|
|
|
|
|
|
# Token not found
|
|
|
|
if auth_token is None:
|
|
|
|
flash(_(u"Token not found"), category="error")
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
# Token expired
|
|
|
|
if datetime.datetime.now() > auth_token.expiration:
|
|
|
|
ub.session.delete(auth_token)
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
flash(_(u"Token has expired"), category="error")
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
# Update token with user information
|
|
|
|
auth_token.user_id = current_user.id
|
|
|
|
auth_token.verified = True
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
flash(_(u"Success! Please return to your device"), category="success")
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/ajax/verify_token', methods=['POST'])
|
|
|
|
@remote_login_required
|
|
|
|
def token_verified():
|
|
|
|
token = request.form['token']
|
|
|
|
auth_token = ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.auth_token == token).first()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
# Token not found
|
|
|
|
if auth_token is None:
|
|
|
|
data['status'] = 'error'
|
|
|
|
data['message'] = _(u"Token not found")
|
|
|
|
|
|
|
|
# Token expired
|
|
|
|
elif datetime.datetime.now() > auth_token.expiration:
|
|
|
|
ub.session.delete(auth_token)
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
data['status'] = 'error'
|
|
|
|
data['message'] = _(u"Token has expired")
|
|
|
|
|
|
|
|
elif not auth_token.verified:
|
|
|
|
data['status'] = 'not_verified'
|
|
|
|
|
|
|
|
else:
|
|
|
|
user = ub.session.query(ub.User).filter(ub.User.id == auth_token.user_id).first()
|
|
|
|
login_user(user)
|
|
|
|
|
|
|
|
ub.session.delete(auth_token)
|
|
|
|
ub.session.commit()
|
|
|
|
|
|
|
|
data['status'] = 'success'
|
|
|
|
flash(_(u"you are now logged in as: '%(nickname)s'", nickname=user.nickname), category="success")
|
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
response = make_response(json.dumps(data, ensure_ascii=False))
|
2017-07-08 01:18:03 +00:00
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2018-11-18 16:09:13 +00:00
|
|
|
@app.route('/send/<int:book_id>/<book_format>/<int:convert>')
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
2016-04-27 08:35:23 +00:00
|
|
|
@download_required
|
2018-11-18 16:09:13 +00:00
|
|
|
def send_to_kindle(book_id, book_format, convert):
|
2015-08-02 19:23:24 +00:00
|
|
|
settings = ub.get_mail_settings()
|
|
|
|
if settings.get("mail_server", "mail.example.com") == "mail.example.com":
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Please configure the SMTP mail settings first..."), category="error")
|
2015-08-02 19:23:24 +00:00
|
|
|
elif current_user.kindle_mail:
|
2018-11-25 10:25:20 +00:00
|
|
|
result = helper.send_mail(book_id, book_format, convert, current_user.kindle_mail, config.config_calibre_dir,
|
|
|
|
current_user.nickname)
|
2016-03-27 21:36:51 +00:00
|
|
|
if result is None:
|
2018-07-30 18:12:41 +00:00
|
|
|
flash(_(u"Book successfully queued for sending to %(kindlemail)s", kindlemail=current_user.kindle_mail),
|
2016-12-23 08:53:39 +00:00
|
|
|
category="success")
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.update_download(book_id, int(current_user.id))
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2016-12-23 08:53:39 +00:00
|
|
|
flash(_(u"There was an error sending this book: %(res)s", res=result), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Please configure your kindle e-mail address first..."), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/shelf/add/<int:shelf_id>/<int:book_id>")
|
|
|
|
@login_required
|
|
|
|
def add_to_shelf(shelf_id, book_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2017-07-22 03:42:01 +00:00
|
|
|
if shelf is None:
|
|
|
|
app.logger.info("Invalid shelf specified")
|
|
|
|
if not request.is_xhr:
|
2018-09-27 20:04:57 +00:00
|
|
|
flash(_(u"Invalid shelf specified"), category="error")
|
2017-07-22 03:42:01 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "Invalid shelf specified", 400
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
if not shelf.is_public and not shelf.user_id == int(current_user.id):
|
2017-03-19 19:29:35 +00:00
|
|
|
app.logger.info("Sorry you are not allowed to add a book to the the shelf: %s" % shelf.name)
|
2017-07-22 03:42:01 +00:00
|
|
|
if not request.is_xhr:
|
2018-09-27 20:04:57 +00:00
|
|
|
flash(_(u"Sorry you are not allowed to add a book to the the shelf: %(shelfname)s", shelfname=shelf.name),
|
|
|
|
category="error")
|
2017-07-22 03:42:01 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "Sorry you are not allowed to add a book to the the shelf: %s" % shelf.name, 403
|
|
|
|
|
|
|
|
if shelf.is_public and not current_user.role_edit_shelfs():
|
|
|
|
app.logger.info("User is not allowed to edit public shelves")
|
|
|
|
if not request.is_xhr:
|
2018-09-27 20:04:57 +00:00
|
|
|
flash(_(u"You are not allowed to edit public shelves"), category="error")
|
2017-07-22 03:42:01 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "User is not allowed to edit public shelves", 403
|
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
book_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id,
|
2017-03-19 18:22:45 +00:00
|
|
|
ub.BookShelf.book_id == book_id).first()
|
|
|
|
if book_in_shelf:
|
2017-03-19 19:29:35 +00:00
|
|
|
app.logger.info("Book is already part of the shelf: %s" % shelf.name)
|
2017-07-22 03:42:01 +00:00
|
|
|
if not request.is_xhr:
|
2018-09-27 20:04:57 +00:00
|
|
|
flash(_(u"Book is already part of the shelf: %(shelfname)s", shelfname=shelf.name), category="error")
|
2017-07-22 03:42:01 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "Book is already part of the shelf: %s" % shelf.name, 400
|
|
|
|
|
|
|
|
maxOrder = ub.session.query(func.max(ub.BookShelf.order)).filter(ub.BookShelf.shelf == shelf_id).first()
|
2017-01-28 19:16:40 +00:00
|
|
|
if maxOrder[0] is None:
|
2016-12-26 10:33:32 +00:00
|
|
|
maxOrder = 0
|
|
|
|
else:
|
2017-01-28 19:16:40 +00:00
|
|
|
maxOrder = maxOrder[0]
|
2017-07-22 03:42:01 +00:00
|
|
|
|
|
|
|
ins = ub.BookShelf(shelf=shelf.id, book_id=book_id, order=maxOrder + 1)
|
|
|
|
ub.session.add(ins)
|
|
|
|
ub.session.commit()
|
|
|
|
if not request.is_xhr:
|
2017-03-19 19:29:35 +00:00
|
|
|
flash(_(u"Book has been added to shelf: %(sname)s", sname=shelf.name), category="success")
|
2018-09-27 20:04:57 +00:00
|
|
|
if "HTTP_REFERER" in request.environ:
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
else:
|
|
|
|
return redirect(url_for('index'))
|
2017-07-22 03:42:01 +00:00
|
|
|
return "", 204
|
2015-10-12 01:09:56 +00:00
|
|
|
|
2018-08-19 11:36:04 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
@app.route("/shelf/massadd/<int:shelf_id>")
|
|
|
|
@login_required
|
|
|
|
def search_to_shelf(shelf_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
if shelf is None:
|
|
|
|
app.logger.info("Invalid shelf specified")
|
2018-08-19 11:36:04 +00:00
|
|
|
flash(_(u"Invalid shelf specified"), category="error")
|
|
|
|
return redirect(url_for('index'))
|
2018-08-19 08:14:20 +00:00
|
|
|
|
|
|
|
if not shelf.is_public and not shelf.user_id == int(current_user.id):
|
2018-08-19 11:36:04 +00:00
|
|
|
app.logger.info("You are not allowed to add a book to the the shelf: %s" % shelf.name)
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"You are not allowed to add a book to the the shelf: %(name)s", name=shelf.name), category="error")
|
2018-08-19 11:36:04 +00:00
|
|
|
return redirect(url_for('index'))
|
2018-08-19 08:14:20 +00:00
|
|
|
|
|
|
|
if shelf.is_public and not current_user.role_edit_shelfs():
|
|
|
|
app.logger.info("User is not allowed to edit public shelves")
|
2018-08-19 11:36:04 +00:00
|
|
|
flash(_(u"User is not allowed to edit public shelves"), category="error")
|
|
|
|
return redirect(url_for('index'))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
if current_user.id in ub.searched_ids and ub.searched_ids[current_user.id]:
|
|
|
|
books_for_shelf = list()
|
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).all()
|
|
|
|
if books_in_shelf:
|
|
|
|
book_ids = list()
|
|
|
|
for book_id in books_in_shelf:
|
|
|
|
book_ids.append(book_id.book_id)
|
|
|
|
for id in ub.searched_ids[current_user.id]:
|
|
|
|
if id not in book_ids:
|
|
|
|
books_for_shelf.append(id)
|
|
|
|
else:
|
|
|
|
books_for_shelf = ub.searched_ids[current_user.id]
|
|
|
|
|
|
|
|
if not books_for_shelf:
|
|
|
|
app.logger.info("Books are already part of the shelf: %s" % shelf.name)
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Books are already part of the shelf: %(name)s", name=shelf.name), category="error")
|
2018-08-19 08:14:20 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
maxOrder = ub.session.query(func.max(ub.BookShelf.order)).filter(ub.BookShelf.shelf == shelf_id).first()
|
|
|
|
if maxOrder[0] is None:
|
|
|
|
maxOrder = 0
|
|
|
|
else:
|
|
|
|
maxOrder = maxOrder[0]
|
|
|
|
|
|
|
|
for book in books_for_shelf:
|
|
|
|
maxOrder = maxOrder + 1
|
|
|
|
ins = ub.BookShelf(shelf=shelf.id, book_id=book, order=maxOrder)
|
|
|
|
ub.session.add(ins)
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u"Books have been added to shelf: %(sname)s", sname=shelf.name), category="success")
|
|
|
|
else:
|
|
|
|
flash(_(u"Could not add books to shelf: %(sname)s", sname=shelf.name), category="error")
|
2018-10-03 19:58:37 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
|
2015-10-12 01:09:56 +00:00
|
|
|
@app.route("/shelf/remove/<int:shelf_id>/<int:book_id>")
|
|
|
|
@login_required
|
|
|
|
def remove_from_shelf(shelf_id, book_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2017-07-22 03:42:01 +00:00
|
|
|
if shelf is None:
|
|
|
|
app.logger.info("Invalid shelf specified")
|
|
|
|
if not request.is_xhr:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "Invalid shelf specified", 400
|
|
|
|
|
2017-11-12 18:08:20 +00:00
|
|
|
# if shelf is public and use is allowed to edit shelfs, or if shelf is private and user is owner
|
|
|
|
# allow editing shelfs
|
2018-01-06 11:08:55 +00:00
|
|
|
# result shelf public user allowed user owner
|
|
|
|
# false 1 0 x
|
|
|
|
# true 1 1 x
|
|
|
|
# true 0 x 1
|
|
|
|
# false 0 x 0
|
|
|
|
|
|
|
|
if (not shelf.is_public and shelf.user_id == int(current_user.id)) \
|
|
|
|
or (shelf.is_public and current_user.role_edit_shelfs()):
|
|
|
|
book_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id,
|
|
|
|
ub.BookShelf.book_id == book_id).first()
|
|
|
|
|
|
|
|
if book_shelf is None:
|
|
|
|
app.logger.info("Book already removed from shelf")
|
|
|
|
if not request.is_xhr:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
return "Book already removed from shelf", 410
|
|
|
|
|
|
|
|
ub.session.delete(book_shelf)
|
|
|
|
ub.session.commit()
|
2017-07-22 03:42:01 +00:00
|
|
|
|
|
|
|
if not request.is_xhr:
|
2018-01-06 11:08:55 +00:00
|
|
|
flash(_(u"Book has been removed from shelf: %(sname)s", sname=shelf.name), category="success")
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
return "", 204
|
|
|
|
else:
|
|
|
|
app.logger.info("Sorry you are not allowed to remove a book from this shelf: %s" % shelf.name)
|
|
|
|
if not request.is_xhr:
|
2018-11-25 10:25:20 +00:00
|
|
|
flash(_(u"Sorry you are not allowed to remove a book from this shelf: %(sname)s", sname=shelf.name),
|
|
|
|
category="error")
|
2017-07-22 03:42:01 +00:00
|
|
|
return redirect(url_for('index'))
|
2018-01-06 11:08:55 +00:00
|
|
|
return "Sorry you are not allowed to remove a book from this shelf: %s" % shelf.name, 403
|
2017-07-22 03:42:01 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/shelf/create", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def create_shelf():
|
|
|
|
shelf = ub.Shelf()
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
if "is_public" in to_save:
|
|
|
|
shelf.is_public = 1
|
|
|
|
shelf.name = to_save["title"]
|
|
|
|
shelf.user_id = int(current_user.id)
|
2017-01-28 19:16:40 +00:00
|
|
|
existing_shelf = ub.session.query(ub.Shelf).filter(
|
|
|
|
or_((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1),
|
|
|
|
(ub.Shelf.name == to_save["title"]) & (ub.Shelf.user_id == int(current_user.id)))).first()
|
2015-10-13 17:12:18 +00:00
|
|
|
if existing_shelf:
|
2017-01-28 19:16:40 +00:00
|
|
|
flash(_(u"A shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error")
|
2015-10-13 17:12:18 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
ub.session.add(shelf)
|
|
|
|
ub.session.commit()
|
2016-12-23 08:53:39 +00:00
|
|
|
flash(_(u"Shelf %(title)s created", title=to_save["title"]), category="success")
|
2017-03-31 16:31:16 +00:00
|
|
|
except Exception:
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"There was an error"), category="error")
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"create a shelf"), page="shelfcreate")
|
2016-12-26 10:33:32 +00:00
|
|
|
else:
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"create a shelf"), page="shelfcreate")
|
2016-12-26 10:33:32 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2016-12-26 10:33:32 +00:00
|
|
|
@app.route("/shelf/edit/<int:shelf_id>", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def edit_shelf(shelf_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2017-01-28 19:16:40 +00:00
|
|
|
existing_shelf = ub.session.query(ub.Shelf).filter(
|
|
|
|
or_((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1),
|
|
|
|
(ub.Shelf.name == to_save["title"]) & (ub.Shelf.user_id == int(current_user.id)))).filter(
|
|
|
|
ub.Shelf.id != shelf_id).first()
|
2017-01-03 18:29:33 +00:00
|
|
|
if existing_shelf:
|
2017-01-28 19:16:40 +00:00
|
|
|
flash(_(u"A shelf with the name '%(title)s' already exists.", title=to_save["title"]), category="error")
|
2016-12-26 10:33:32 +00:00
|
|
|
else:
|
|
|
|
shelf.name = to_save["title"]
|
|
|
|
if "is_public" in to_save:
|
|
|
|
shelf.is_public = 1
|
|
|
|
else:
|
|
|
|
shelf.is_public = 0
|
|
|
|
try:
|
|
|
|
ub.session.commit()
|
2017-01-28 19:16:40 +00:00
|
|
|
flash(_(u"Shelf %(title)s changed", title=to_save["title"]), category="success")
|
2017-03-31 16:31:16 +00:00
|
|
|
except Exception:
|
2016-12-26 10:33:32 +00:00
|
|
|
flash(_(u"There was an error"), category="error")
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"Edit a shelf"), page="shelfedit")
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2018-07-15 16:50:35 +00:00
|
|
|
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"Edit a shelf"), page="shelfedit")
|
2016-12-26 10:33:32 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
@app.route("/shelf/delete/<int:shelf_id>")
|
|
|
|
@login_required
|
|
|
|
def delete_shelf(shelf_id):
|
|
|
|
cur_shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2018-08-28 07:42:19 +00:00
|
|
|
deleted = None
|
2017-03-19 19:29:35 +00:00
|
|
|
if current_user.role_admin():
|
2016-11-09 18:24:33 +00:00
|
|
|
deleted = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).delete()
|
|
|
|
else:
|
2017-09-17 05:15:08 +00:00
|
|
|
if (not cur_shelf.is_public and cur_shelf.user_id == int(current_user.id)) \
|
2017-03-19 19:29:35 +00:00
|
|
|
or (cur_shelf.is_public and current_user.role_edit_shelfs()):
|
|
|
|
deleted = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id),
|
2016-12-23 08:53:39 +00:00
|
|
|
ub.Shelf.id == shelf_id),
|
|
|
|
ub.and_(ub.Shelf.is_public == 1,
|
|
|
|
ub.Shelf.id == shelf_id))).delete()
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
if deleted:
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).delete()
|
|
|
|
ub.session.commit()
|
2017-03-19 19:29:35 +00:00
|
|
|
app.logger.info(_(u"successfully deleted shelf %(name)s", name=cur_shelf.name, category="success"))
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('index'))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
@app.route("/shelf/<int:shelf_id>")
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
2015-08-02 18:59:11 +00:00
|
|
|
def show_shelf(shelf_id):
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user.is_anonymous:
|
2016-11-09 18:24:33 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1, ub.Shelf.id == shelf_id).first()
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id),
|
|
|
|
ub.Shelf.id == shelf_id),
|
|
|
|
ub.and_(ub.Shelf.is_public == 1,
|
|
|
|
ub.Shelf.id == shelf_id))).first()
|
2015-08-02 18:59:11 +00:00
|
|
|
result = list()
|
2017-07-08 11:50:21 +00:00
|
|
|
# user is allowed to access shelf
|
2015-08-02 18:59:11 +00:00
|
|
|
if shelf:
|
2017-01-28 19:16:40 +00:00
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).order_by(
|
|
|
|
ub.BookShelf.order.asc()).all()
|
2015-08-02 18:59:11 +00:00
|
|
|
for book in books_in_shelf:
|
|
|
|
cur_book = db.session.query(db.Books).filter(db.Books.id == book.book_id).first()
|
2018-09-10 17:31:56 +00:00
|
|
|
if cur_book:
|
|
|
|
result.append(cur_book)
|
|
|
|
else:
|
|
|
|
app.logger.info('Not existing book %s in shelf %s deleted' % (book.book_id, shelf.id))
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book.book_id).delete()
|
|
|
|
ub.session.commit()
|
2017-07-08 11:50:21 +00:00
|
|
|
return render_title_template('shelf.html', entries=result, title=_(u"Shelf: '%(name)s'", name=shelf.name),
|
2018-07-15 16:50:35 +00:00
|
|
|
shelf=shelf, page="shelf")
|
2017-07-08 11:50:21 +00:00
|
|
|
else:
|
2017-07-08 17:50:44 +00:00
|
|
|
flash(_(u"Error opening shelf. Shelf does not exist or is not accessible"), category="error")
|
2017-07-08 11:50:21 +00:00
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-12-26 10:33:32 +00:00
|
|
|
@app.route("/shelf/order/<int:shelf_id>", methods=["GET", "POST"])
|
2017-01-12 19:43:36 +00:00
|
|
|
@login_required
|
2016-12-26 10:33:32 +00:00
|
|
|
def order_shelf(shelf_id):
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).order_by(
|
|
|
|
ub.BookShelf.order.asc()).all()
|
2017-01-28 19:16:40 +00:00
|
|
|
counter = 0
|
2016-12-26 10:33:32 +00:00
|
|
|
for book in books_in_shelf:
|
|
|
|
setattr(book, 'order', to_save[str(book.book_id)])
|
2017-01-28 19:16:40 +00:00
|
|
|
counter += 1
|
2016-12-26 10:33:32 +00:00
|
|
|
ub.session.commit()
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user.is_anonymous:
|
2016-12-26 10:33:32 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1, ub.Shelf.id == shelf_id).first()
|
|
|
|
else:
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id),
|
|
|
|
ub.Shelf.id == shelf_id),
|
|
|
|
ub.and_(ub.Shelf.is_public == 1,
|
|
|
|
ub.Shelf.id == shelf_id))).first()
|
|
|
|
result = list()
|
|
|
|
if shelf:
|
2017-01-28 19:16:40 +00:00
|
|
|
books_in_shelf2 = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id) \
|
2017-01-22 20:30:36 +00:00
|
|
|
.order_by(ub.BookShelf.order.asc()).all()
|
2016-12-26 10:33:32 +00:00
|
|
|
for book in books_in_shelf2:
|
|
|
|
cur_book = db.session.query(db.Books).filter(db.Books.id == book.book_id).first()
|
|
|
|
result.append(cur_book)
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template('shelf_order.html', entries=result,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Change order of Shelf: '%(name)s'", name=shelf.name),
|
|
|
|
shelf=shelf, page="shelforder")
|
2016-12-26 10:33:32 +00:00
|
|
|
|
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/me", methods=["GET", "POST"])
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
|
|
|
def profile():
|
|
|
|
content = ub.session.query(ub.User).filter(ub.User.id == int(current_user.id)).first()
|
|
|
|
downloads = list()
|
2018-08-19 08:14:20 +00:00
|
|
|
languages = speaking_language()
|
2016-12-23 08:53:39 +00:00
|
|
|
translations = babel.list_translations() + [LC('en')]
|
2015-08-02 18:59:11 +00:00
|
|
|
for book in content.downloads:
|
2017-01-28 19:16:40 +00:00
|
|
|
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.book_id).first()
|
2017-01-10 20:20:12 +00:00
|
|
|
if downloadBook:
|
|
|
|
downloads.append(db.session.query(db.Books).filter(db.Books.id == book.book_id).first())
|
|
|
|
else:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.delete_download(book.book_id)
|
|
|
|
# ub.session.query(ub.Downloads).filter(book.book_id == ub.Downloads.book_id).delete()
|
|
|
|
# ub.session.commit()
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2016-11-09 18:24:33 +00:00
|
|
|
content.random_books = 0
|
2016-04-27 14:00:58 +00:00
|
|
|
if current_user.role_passwd() or current_user.role_admin():
|
2018-09-02 09:48:58 +00:00
|
|
|
if "password" in to_save and to_save["password"]:
|
2016-04-27 14:00:58 +00:00
|
|
|
content.password = generate_password_hash(to_save["password"])
|
2016-08-08 19:01:38 +00:00
|
|
|
if "kindle_mail" in to_save and to_save["kindle_mail"] != content.kindle_mail:
|
2015-08-02 18:59:11 +00:00
|
|
|
content.kindle_mail = to_save["kindle_mail"]
|
2015-10-13 16:07:17 +00:00
|
|
|
if to_save["email"] and to_save["email"] != content.email:
|
2018-08-28 07:42:19 +00:00
|
|
|
if config.config_public_reg and not check_valid_domain(to_save["email"]):
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"E-mail is not from valid domain"), category="error")
|
2018-08-24 13:48:09 +00:00
|
|
|
return render_title_template("user_edit.html", content=content, downloads=downloads,
|
2018-10-03 19:58:37 +00:00
|
|
|
title=_(u"%(name)s's profile", name=current_user.nickname))
|
2015-10-13 16:07:17 +00:00
|
|
|
content.email = to_save["email"]
|
2016-11-09 18:24:33 +00:00
|
|
|
if "show_random" in to_save and to_save["show_random"] == "on":
|
|
|
|
content.random_books = 1
|
|
|
|
if "default_language" in to_save:
|
|
|
|
content.default_language = to_save["default_language"]
|
2018-08-28 07:42:19 +00:00
|
|
|
if "locale" in to_save:
|
2016-11-09 18:24:33 +00:00
|
|
|
content.locale = to_save["locale"]
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view = 0
|
|
|
|
if "show_random" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RANDOM
|
|
|
|
if "show_language" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_LANGUAGE
|
|
|
|
if "show_series" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_SERIES
|
|
|
|
if "show_category" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_CATEGORY
|
2017-11-12 13:06:33 +00:00
|
|
|
if "show_recent" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RECENT
|
|
|
|
if "show_sorted" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_SORTED
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_hot" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_HOT
|
2017-02-04 13:28:18 +00:00
|
|
|
if "show_best_rated" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_BEST_RATED
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_author" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_AUTHOR
|
2018-09-30 16:30:24 +00:00
|
|
|
if "show_publisher" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_PUBLISHER
|
2017-02-19 20:08:22 +00:00
|
|
|
if "show_read_and_unread" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_READ_AND_UNREAD
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_detail_random" in to_save:
|
|
|
|
content.sidebar_view += ub.DETAIL_RANDOM
|
2017-08-12 16:52:56 +00:00
|
|
|
|
|
|
|
content.mature_content = "show_mature_content" in to_save
|
|
|
|
|
2015-10-13 16:07:17 +00:00
|
|
|
try:
|
|
|
|
ub.session.commit()
|
|
|
|
except IntegrityError:
|
|
|
|
ub.session.rollback()
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Found an existing account for this e-mail address."), category="error")
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template("user_edit.html", content=content, downloads=downloads,
|
2017-01-28 19:16:40 +00:00
|
|
|
title=_(u"%(name)s's profile", name=current_user.nickname))
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Profile updated"), category="success")
|
2017-01-28 19:16:40 +00:00
|
|
|
return render_title_template("user_edit.html", translations=translations, profile=1, languages=languages,
|
2018-07-15 16:50:35 +00:00
|
|
|
content=content, downloads=downloads, title=_(u"%(name)s's profile",
|
|
|
|
name=current_user.nickname), page="me")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-02 17:52:33 +00:00
|
|
|
@app.route("/admin/view")
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
2015-10-13 16:07:17 +00:00
|
|
|
@admin_required
|
2017-01-02 17:52:33 +00:00
|
|
|
def admin():
|
2019-01-20 16:45:42 +00:00
|
|
|
version = updater_thread.get_current_version_info()
|
2018-09-10 08:42:28 +00:00
|
|
|
if version is False:
|
2017-12-02 17:42:21 +00:00
|
|
|
commit = _(u'Unknown')
|
|
|
|
else:
|
2019-01-20 16:45:42 +00:00
|
|
|
if 'datetime' in version:
|
|
|
|
commit = version['datetime']
|
|
|
|
|
|
|
|
tz = datetime.timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone)
|
|
|
|
form_date = datetime.datetime.strptime(commit[:19], "%Y-%m-%dT%H:%M:%S")
|
|
|
|
if len(commit) > 19: # check if string has timezone
|
|
|
|
if commit[19] == '+':
|
|
|
|
form_date -= datetime.timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
|
|
|
|
elif commit[19] == '-':
|
|
|
|
form_date += datetime.timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
|
|
|
|
commit = format_datetime(form_date - tz, format='short', locale=get_locale())
|
|
|
|
else:
|
|
|
|
commit = version['version']
|
2017-12-02 17:42:21 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
content = ub.session.query(ub.User).all()
|
2015-08-02 19:23:24 +00:00
|
|
|
settings = ub.session.query(ub.Settings).first()
|
2017-02-15 17:19:48 +00:00
|
|
|
return render_title_template("admin.html", content=content, email=settings, config=config, commit=commit,
|
2018-08-16 19:17:26 +00:00
|
|
|
title=_(u"Admin page"), page="admin")
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-02 17:52:33 +00:00
|
|
|
|
2017-01-22 20:30:36 +00:00
|
|
|
@app.route("/admin/config", methods=["GET", "POST"])
|
2017-01-02 17:52:33 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def configuration():
|
2017-01-28 19:16:40 +00:00
|
|
|
return configuration_helper(0)
|
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
|
2018-07-15 13:08:06 +00:00
|
|
|
@app.route("/admin/viewconfig", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def view_configuration():
|
2018-07-28 15:19:08 +00:00
|
|
|
reboot_required = False
|
2018-07-15 13:08:06 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
content = ub.session.query(ub.Settings).first()
|
|
|
|
if "config_calibre_web_title" in to_save:
|
|
|
|
content.config_calibre_web_title = to_save["config_calibre_web_title"]
|
|
|
|
if "config_columns_to_ignore" in to_save:
|
|
|
|
content.config_columns_to_ignore = to_save["config_columns_to_ignore"]
|
|
|
|
if "config_read_column" in to_save:
|
|
|
|
content.config_read_column = int(to_save["config_read_column"])
|
2019-01-11 07:36:28 +00:00
|
|
|
if "config_theme" in to_save:
|
|
|
|
content.config_theme = int(to_save["config_theme"])
|
2018-07-15 13:08:06 +00:00
|
|
|
if "config_title_regex" in to_save:
|
|
|
|
if content.config_title_regex != to_save["config_title_regex"]:
|
|
|
|
content.config_title_regex = to_save["config_title_regex"]
|
|
|
|
reboot_required = True
|
|
|
|
if "config_random_books" in to_save:
|
|
|
|
content.config_random_books = int(to_save["config_random_books"])
|
|
|
|
if "config_books_per_page" in to_save:
|
|
|
|
content.config_books_per_page = int(to_save["config_books_per_page"])
|
|
|
|
# Mature Content configuration
|
|
|
|
if "config_mature_content_tags" in to_save:
|
|
|
|
content.config_mature_content_tags = to_save["config_mature_content_tags"].strip()
|
|
|
|
|
|
|
|
# Default user configuration
|
|
|
|
content.config_default_role = 0
|
|
|
|
if "admin_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_ADMIN
|
|
|
|
if "download_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_DOWNLOAD
|
|
|
|
if "upload_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_UPLOAD
|
|
|
|
if "edit_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_EDIT
|
|
|
|
if "delete_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_DELETE_BOOKS
|
|
|
|
if "passwd_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_PASSWD
|
|
|
|
if "passwd_role" in to_save:
|
|
|
|
content.config_default_role = content.config_default_role + ub.ROLE_EDIT_SHELFS
|
|
|
|
content.config_default_show = 0
|
|
|
|
if "show_detail_random" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.DETAIL_RANDOM
|
|
|
|
if "show_language" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_LANGUAGE
|
|
|
|
if "show_series" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_SERIES
|
|
|
|
if "show_category" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_CATEGORY
|
|
|
|
if "show_hot" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_HOT
|
|
|
|
if "show_random" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_RANDOM
|
|
|
|
if "show_author" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_AUTHOR
|
2018-09-30 16:30:24 +00:00
|
|
|
if "show_publisher" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_PUBLISHER
|
2018-07-15 13:08:06 +00:00
|
|
|
if "show_best_rated" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_BEST_RATED
|
|
|
|
if "show_read_and_unread" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_READ_AND_UNREAD
|
|
|
|
if "show_recent" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_RECENT
|
|
|
|
if "show_sorted" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.SIDEBAR_SORTED
|
|
|
|
if "show_mature_content" in to_save:
|
|
|
|
content.config_default_show = content.config_default_show + ub.MATURE_CONTENT
|
|
|
|
ub.session.commit()
|
2018-08-24 13:48:09 +00:00
|
|
|
flash(_(u"Calibre-Web configuration updated"), category="success")
|
2018-07-15 13:08:06 +00:00
|
|
|
config.loadSettings()
|
2019-01-11 07:36:28 +00:00
|
|
|
before_request()
|
2018-07-18 18:21:44 +00:00
|
|
|
if reboot_required:
|
|
|
|
# db.engine.dispose() # ToDo verify correct
|
2018-09-08 12:58:00 +00:00
|
|
|
# ub.session.close()
|
|
|
|
# ub.engine.dispose()
|
2018-07-18 18:21:44 +00:00
|
|
|
# stop Server
|
|
|
|
server.Server.setRestartTyp(True)
|
|
|
|
server.Server.stopServer()
|
|
|
|
app.logger.info('Reboot required, restarting')
|
2018-07-15 13:08:06 +00:00
|
|
|
readColumn = db.session.query(db.Custom_Columns)\
|
|
|
|
.filter(db.and_(db.Custom_Columns.datatype == 'bool',db.Custom_Columns.mark_for_delete == 0)).all()
|
|
|
|
return render_title_template("config_view_edit.html", content=config, readColumns=readColumn,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"UI Configuration"), page="uiconfig")
|
2018-07-15 13:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
@app.route("/config", methods=["GET", "POST"])
|
2017-01-22 15:44:37 +00:00
|
|
|
@unconfigured
|
|
|
|
def basic_configuration():
|
2018-09-07 18:04:35 +00:00
|
|
|
logout_user()
|
2017-01-28 19:16:40 +00:00
|
|
|
return configuration_helper(1)
|
|
|
|
|
2017-01-22 20:30:36 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
def configuration_helper(origin):
|
|
|
|
reboot_required = False
|
2018-06-02 08:59:34 +00:00
|
|
|
gdriveError=None
|
2017-01-28 19:16:40 +00:00
|
|
|
db_change = False
|
|
|
|
success = False
|
2018-08-24 13:48:09 +00:00
|
|
|
filedata = None
|
2018-08-16 19:17:26 +00:00
|
|
|
if gdriveutils.gdrive_support == False:
|
2018-07-15 13:08:06 +00:00
|
|
|
gdriveError = _('Import of optional Google Drive requirements missing')
|
2018-06-02 08:59:34 +00:00
|
|
|
else:
|
2018-07-07 09:41:39 +00:00
|
|
|
if not os.path.isfile(os.path.join(config.get_main_dir,'client_secrets.json')):
|
|
|
|
gdriveError = _('client_secrets.json is missing or not readable')
|
2018-06-02 08:59:34 +00:00
|
|
|
else:
|
2018-07-07 09:41:39 +00:00
|
|
|
with open(os.path.join(config.get_main_dir,'client_secrets.json'), 'r') as settings:
|
2018-06-02 08:59:34 +00:00
|
|
|
filedata=json.load(settings)
|
|
|
|
if not 'web' in filedata:
|
2018-07-07 09:41:39 +00:00
|
|
|
gdriveError = _('client_secrets.json is not configured for web application')
|
2017-01-22 15:44:37 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2017-11-30 15:49:46 +00:00
|
|
|
content = ub.session.query(ub.Settings).first() # type: ub.Settings
|
2017-01-22 15:44:37 +00:00
|
|
|
if "config_calibre_dir" in to_save:
|
2017-01-22 20:30:36 +00:00
|
|
|
if content.config_calibre_dir != to_save["config_calibre_dir"]:
|
|
|
|
content.config_calibre_dir = to_save["config_calibre_dir"]
|
2017-01-28 19:16:40 +00:00
|
|
|
db_change = True
|
2017-04-14 18:29:11 +00:00
|
|
|
# Google drive setup
|
2019-02-03 15:38:27 +00:00
|
|
|
if not os.path.isfile(os.path.join(config.get_main_dir, 'settings.yaml')):
|
|
|
|
content.config_use_google_drive = False
|
2018-06-02 08:59:34 +00:00
|
|
|
if "config_use_google_drive" in to_save and not content.config_use_google_drive and not gdriveError:
|
|
|
|
if filedata:
|
|
|
|
if filedata['web']['redirect_uris'][0].endswith('/'):
|
|
|
|
filedata['web']['redirect_uris'][0] = filedata['web']['redirect_uris'][0][:-1]
|
2018-07-07 09:41:39 +00:00
|
|
|
with open(os.path.join(config.get_main_dir,'settings.yaml'), 'w') as f:
|
|
|
|
yaml = "client_config_backend: settings\nclient_config_file: %(client_file)s\n" \
|
|
|
|
"client_config:\n" \
|
2018-06-02 08:59:34 +00:00
|
|
|
" client_id: %(client_id)s\n client_secret: %(client_secret)s\n" \
|
2018-06-07 13:13:27 +00:00
|
|
|
" redirect_uri: %(redirect_uri)s\n\nsave_credentials: True\n" \
|
2018-07-07 09:41:39 +00:00
|
|
|
"save_credentials_backend: file\nsave_credentials_file: %(credential)s\n\n" \
|
2018-06-02 08:59:34 +00:00
|
|
|
"get_refresh_token: True\n\noauth_scope:\n" \
|
2018-06-07 13:13:27 +00:00
|
|
|
" - https://www.googleapis.com/auth/drive\n"
|
2018-07-07 09:41:39 +00:00
|
|
|
f.write(yaml % {'client_file': os.path.join(config.get_main_dir,'client_secrets.json'),
|
|
|
|
'client_id': filedata['web']['client_id'],
|
2018-06-02 08:59:34 +00:00
|
|
|
'client_secret': filedata['web']['client_secret'],
|
2018-07-07 09:41:39 +00:00
|
|
|
'redirect_uri': filedata['web']['redirect_uris'][0],
|
|
|
|
'credential': os.path.join(config.get_main_dir,'gdrive_credentials')})
|
2018-06-02 08:59:34 +00:00
|
|
|
else:
|
2018-07-07 09:41:39 +00:00
|
|
|
flash(_(u'client_secrets.json is not configured for web application'), category="error")
|
2018-06-02 08:59:34 +00:00
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
2018-08-16 19:17:26 +00:00
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
2018-07-15 16:50:35 +00:00
|
|
|
goodreads=goodreads_support, title=_(u"Basic Configuration"),
|
|
|
|
page="config")
|
2018-06-02 08:59:34 +00:00
|
|
|
# always show google drive settings, but in case of error deny support
|
2018-07-09 18:30:38 +00:00
|
|
|
if "config_use_google_drive" in to_save and not gdriveError:
|
2017-02-20 18:34:37 +00:00
|
|
|
content.config_use_google_drive = "config_use_google_drive" in to_save
|
2018-06-02 08:59:34 +00:00
|
|
|
else:
|
|
|
|
content.config_use_google_drive = 0
|
2017-02-20 18:34:37 +00:00
|
|
|
if "config_google_drive_folder" in to_save:
|
|
|
|
if content.config_google_drive_folder != to_save["config_google_drive_folder"]:
|
|
|
|
content.config_google_drive_folder = to_save["config_google_drive_folder"]
|
2018-06-02 08:59:34 +00:00
|
|
|
gdriveutils.deleteDatabaseOnChange()
|
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
if "config_port" in to_save:
|
2017-01-22 20:30:36 +00:00
|
|
|
if content.config_port != int(to_save["config_port"]):
|
|
|
|
content.config_port = int(to_save["config_port"])
|
|
|
|
reboot_required = True
|
2018-03-30 19:20:47 +00:00
|
|
|
if "config_keyfile" in to_save:
|
|
|
|
if content.config_keyfile != to_save["config_keyfile"]:
|
|
|
|
if os.path.isfile(to_save["config_keyfile"]) or to_save["config_keyfile"] is u"":
|
|
|
|
content.config_keyfile = to_save["config_keyfile"]
|
|
|
|
reboot_required = True
|
|
|
|
else:
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u'Keyfile location is not valid, please enter correct path'), category="error")
|
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
2018-08-16 19:17:26 +00:00
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
2018-07-15 16:50:35 +00:00
|
|
|
goodreads=goodreads_support, title=_(u"Basic Configuration"),
|
|
|
|
page="config")
|
2018-03-30 19:20:47 +00:00
|
|
|
if "config_certfile" in to_save:
|
|
|
|
if content.config_certfile != to_save["config_certfile"]:
|
|
|
|
if os.path.isfile(to_save["config_certfile"]) or to_save["config_certfile"] is u"":
|
|
|
|
content.config_certfile = to_save["config_certfile"]
|
|
|
|
reboot_required = True
|
|
|
|
else:
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u'Certfile location is not valid, please enter correct path'), category="error")
|
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
2018-08-16 19:17:26 +00:00
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
2018-07-15 16:50:35 +00:00
|
|
|
goodreads=goodreads_support, title=_(u"Basic Configuration"),
|
|
|
|
page="config")
|
2018-07-15 14:27:16 +00:00
|
|
|
content.config_uploading = 0
|
|
|
|
content.config_anonbrowse = 0
|
|
|
|
content.config_public_reg = 0
|
|
|
|
if "config_uploading" in to_save and to_save["config_uploading"] == "on":
|
|
|
|
content.config_uploading = 1
|
|
|
|
if "config_anonbrowse" in to_save and to_save["config_anonbrowse"] == "on":
|
|
|
|
content.config_anonbrowse = 1
|
|
|
|
if "config_public_reg" in to_save and to_save["config_public_reg"] == "on":
|
|
|
|
content.config_public_reg = 1
|
2017-07-08 01:18:03 +00:00
|
|
|
|
2018-07-18 18:21:44 +00:00
|
|
|
if "config_converterpath" in to_save:
|
|
|
|
content.config_converterpath = to_save["config_converterpath"].strip()
|
|
|
|
if "config_calibre" in to_save:
|
|
|
|
content.config_calibre = to_save["config_calibre"].strip()
|
|
|
|
if "config_ebookconverter" in to_save:
|
|
|
|
content.config_ebookconverter = int(to_save["config_ebookconverter"])
|
|
|
|
|
2017-07-08 23:05:20 +00:00
|
|
|
# Remote login configuration
|
|
|
|
content.config_remote_login = ("config_remote_login" in to_save and to_save["config_remote_login"] == "on")
|
2017-07-08 01:18:03 +00:00
|
|
|
if not content.config_remote_login:
|
|
|
|
ub.session.query(ub.RemoteAuthToken).delete()
|
2017-02-09 05:46:07 +00:00
|
|
|
|
2017-07-08 23:05:20 +00:00
|
|
|
# Goodreads configuration
|
|
|
|
content.config_use_goodreads = ("config_use_goodreads" in to_save and to_save["config_use_goodreads"] == "on")
|
|
|
|
if "config_goodreads_api_key" in to_save:
|
|
|
|
content.config_goodreads_api_key = to_save["config_goodreads_api_key"]
|
|
|
|
if "config_goodreads_api_secret" in to_save:
|
|
|
|
content.config_goodreads_api_secret = to_save["config_goodreads_api_secret"]
|
2019-01-20 16:45:42 +00:00
|
|
|
if "config_updater" in to_save:
|
|
|
|
content.config_updatechannel = int(to_save["config_updater"])
|
2018-07-15 14:27:16 +00:00
|
|
|
if "config_log_level" in to_save:
|
|
|
|
content.config_log_level = int(to_save["config_log_level"])
|
2017-12-02 10:15:51 +00:00
|
|
|
if content.config_logfile != to_save["config_logfile"]:
|
|
|
|
# check valid path, only path or file
|
|
|
|
if os.path.dirname(to_save["config_logfile"]):
|
2018-09-23 17:15:50 +00:00
|
|
|
if os.path.exists(os.path.dirname(to_save["config_logfile"])) and \
|
|
|
|
os.path.basename(to_save["config_logfile"]) and not os.path.isdir(to_save["config_logfile"]):
|
2017-12-02 10:15:51 +00:00
|
|
|
content.config_logfile = to_save["config_logfile"]
|
|
|
|
else:
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u'Logfile location is not valid, please enter correct path'), category="error")
|
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
2018-08-16 19:17:26 +00:00
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
2018-07-15 16:50:35 +00:00
|
|
|
goodreads=goodreads_support, title=_(u"Basic Configuration"),
|
|
|
|
page="config")
|
2017-12-02 10:15:51 +00:00
|
|
|
else:
|
|
|
|
content.config_logfile = to_save["config_logfile"]
|
|
|
|
reboot_required = True
|
2017-07-08 23:05:20 +00:00
|
|
|
|
2017-11-18 09:34:21 +00:00
|
|
|
# Rarfile Content configuration
|
2018-09-02 09:48:58 +00:00
|
|
|
if "config_rarfile_location" in to_save and to_save['config_rarfile_location'] is not u"":
|
2017-11-19 17:08:55 +00:00
|
|
|
check = helper.check_unrar(to_save["config_rarfile_location"].strip())
|
|
|
|
if not check[0] :
|
|
|
|
content.config_rarfile_location = to_save["config_rarfile_location"].strip()
|
|
|
|
else:
|
|
|
|
flash(check[1], category="error")
|
2018-11-25 10:25:20 +00:00
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
|
|
|
gdrive=gdriveutils.gdrive_support, goodreads=goodreads_support,
|
|
|
|
rarfile_support=rar_support, title=_(u"Basic Configuration"))
|
2017-01-22 15:44:37 +00:00
|
|
|
try:
|
2019-02-03 15:38:27 +00:00
|
|
|
if content.config_use_google_drive and is_gdrive_ready() and not \
|
|
|
|
os.path.exists(os.path.join(content.config_calibre_dir, "metadata.db")):
|
2018-07-14 06:31:52 +00:00
|
|
|
gdriveutils.downloadFile(None, "metadata.db", config.config_calibre_dir + "/metadata.db")
|
2017-01-28 19:16:40 +00:00
|
|
|
if db_change:
|
|
|
|
if config.db_configured:
|
|
|
|
db.session.close()
|
|
|
|
db.engine.dispose()
|
2017-01-22 15:44:37 +00:00
|
|
|
ub.session.commit()
|
2018-08-24 13:48:09 +00:00
|
|
|
flash(_(u"Calibre-Web configuration updated"), category="success")
|
2017-01-22 15:44:37 +00:00
|
|
|
config.loadSettings()
|
2017-01-28 19:16:40 +00:00
|
|
|
app.logger.setLevel(config.config_log_level)
|
|
|
|
logging.getLogger("book_formats").setLevel(config.config_log_level)
|
2018-07-29 15:51:23 +00:00
|
|
|
except Exception as e:
|
2017-01-22 15:44:37 +00:00
|
|
|
flash(e, category="error")
|
2018-11-25 10:25:20 +00:00
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
|
|
|
goodreads=goodreads_support, rarfile_support=rar_support,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Basic Configuration"), page="config")
|
2017-01-28 19:16:40 +00:00
|
|
|
if db_change:
|
|
|
|
reload(db)
|
|
|
|
if not db.setup_db():
|
|
|
|
flash(_(u'DB location is not valid, please enter correct path'), category="error")
|
2018-11-25 10:25:20 +00:00
|
|
|
return render_title_template("config_edit.html", content=config, origin=origin,
|
|
|
|
gdrive=gdriveutils.gdrive_support,gdriveError=gdriveError,
|
|
|
|
goodreads=goodreads_support, rarfile_support=rar_support,
|
2018-07-15 16:50:35 +00:00
|
|
|
title=_(u"Basic Configuration"), page="config")
|
2017-01-22 20:30:36 +00:00
|
|
|
if reboot_required:
|
2018-07-09 16:47:36 +00:00
|
|
|
# stop Server
|
|
|
|
server.Server.setRestartTyp(True)
|
|
|
|
server.Server.stopServer()
|
2017-01-28 19:16:40 +00:00
|
|
|
app.logger.info('Reboot required, restarting')
|
|
|
|
if origin:
|
|
|
|
success = True
|
2019-02-03 15:38:27 +00:00
|
|
|
if is_gdrive_ready() and gdriveutils.gdrive_support == True: # and config.config_use_google_drive == True:
|
2018-06-02 08:59:34 +00:00
|
|
|
gdrivefolders=gdriveutils.listRootFolders()
|
|
|
|
else:
|
2018-08-24 13:48:09 +00:00
|
|
|
gdrivefolders=list()
|
2017-02-15 17:19:48 +00:00
|
|
|
return render_title_template("config_edit.html", origin=origin, success=success, content=config,
|
2018-11-25 10:25:20 +00:00
|
|
|
show_authenticate_google_drive=not is_gdrive_ready(),
|
|
|
|
gdrive=gdriveutils.gdrive_support, gdriveError=gdriveError,
|
|
|
|
gdrivefolders=gdrivefolders, rarfile_support=rar_support,
|
2018-07-15 16:50:35 +00:00
|
|
|
goodreads=goodreads_support, title=_(u"Basic Configuration"), page="config")
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
@app.route("/admin/user/new", methods=["GET", "POST"])
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
2015-10-13 16:07:17 +00:00
|
|
|
@admin_required
|
2015-08-02 18:59:11 +00:00
|
|
|
def new_user():
|
|
|
|
content = ub.User()
|
2018-08-19 08:14:20 +00:00
|
|
|
languages = speaking_language()
|
2017-01-02 17:52:33 +00:00
|
|
|
translations = [LC('en')] + babel.list_translations()
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2016-11-09 18:24:33 +00:00
|
|
|
content.default_language = to_save["default_language"]
|
2017-08-08 16:52:00 +00:00
|
|
|
content.mature_content = "show_mature_content" in to_save
|
2017-01-22 15:44:37 +00:00
|
|
|
if "locale" in to_save:
|
2017-01-22 13:07:54 +00:00
|
|
|
content.locale = to_save["locale"]
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view = 0
|
|
|
|
if "show_random" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RANDOM
|
2016-11-09 18:24:33 +00:00
|
|
|
if "show_language" in to_save:
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view += ub.SIDEBAR_LANGUAGE
|
2016-11-09 18:24:33 +00:00
|
|
|
if "show_series" in to_save:
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view += ub.SIDEBAR_SERIES
|
2016-11-09 18:24:33 +00:00
|
|
|
if "show_category" in to_save:
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view += ub.SIDEBAR_CATEGORY
|
2016-11-09 18:24:33 +00:00
|
|
|
if "show_hot" in to_save:
|
2017-01-28 19:16:40 +00:00
|
|
|
content.sidebar_view += ub.SIDEBAR_HOT
|
2017-03-03 21:09:53 +00:00
|
|
|
if "show_read_and_unread" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_READ_AND_UNREAD
|
2017-02-04 13:28:18 +00:00
|
|
|
if "show_best_rated" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_BEST_RATED
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_author" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_AUTHOR
|
2018-10-28 13:40:31 +00:00
|
|
|
if "show_publisher" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_PUBLISHER
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_detail_random" in to_save:
|
|
|
|
content.sidebar_view += ub.DETAIL_RANDOM
|
2018-09-27 20:04:57 +00:00
|
|
|
if "show_sorted" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_SORTED
|
|
|
|
if "show_recent" in to_save:
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RECENT
|
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
content.role = 0
|
|
|
|
if "admin_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_ADMIN
|
|
|
|
if "download_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_DOWNLOAD
|
|
|
|
if "upload_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_UPLOAD
|
|
|
|
if "edit_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_EDIT
|
2018-12-27 09:53:19 +00:00
|
|
|
if "delete_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_DELETE_BOOKS
|
2016-04-27 14:00:58 +00:00
|
|
|
if "passwd_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_PASSWD
|
2017-03-19 19:29:35 +00:00
|
|
|
if "edit_shelf_role" in to_save:
|
|
|
|
content.role = content.role + ub.ROLE_EDIT_SHELFS
|
2018-08-28 07:42:19 +00:00
|
|
|
if not to_save["nickname"] or not to_save["email"] or not to_save["password"]:
|
|
|
|
flash(_(u"Please fill out all fields!"), category="error")
|
|
|
|
return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
|
|
|
|
title=_(u"Add new user"))
|
|
|
|
content.password = generate_password_hash(to_save["password"])
|
|
|
|
content.nickname = to_save["nickname"]
|
|
|
|
if config.config_public_reg and not check_valid_domain(to_save["email"]):
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"E-mail is not from valid domain"), category="error")
|
2018-08-28 07:42:19 +00:00
|
|
|
return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
|
|
|
|
title=_(u"Add new user"))
|
|
|
|
else:
|
|
|
|
content.email = to_save["email"]
|
2015-08-02 18:59:11 +00:00
|
|
|
try:
|
|
|
|
ub.session.add(content)
|
|
|
|
ub.session.commit()
|
2017-01-22 15:44:37 +00:00
|
|
|
flash(_(u"User '%(user)s' created", user=content.nickname), category="success")
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('admin'))
|
2015-10-13 16:07:17 +00:00
|
|
|
except IntegrityError:
|
|
|
|
ub.session.rollback()
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Found an existing account for this e-mail address or nickname."), category="error")
|
2017-02-10 19:18:37 +00:00
|
|
|
else:
|
2017-04-14 18:29:11 +00:00
|
|
|
content.role = config.config_default_role
|
2017-11-30 19:12:13 +00:00
|
|
|
content.sidebar_view = config.config_default_show
|
2018-03-27 18:18:11 +00:00
|
|
|
content.mature_content = bool(config.config_default_show & ub.MATURE_CONTENT)
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
|
2018-07-15 16:50:35 +00:00
|
|
|
languages=languages, title=_(u"Add new user"), page="newuser")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2017-01-02 17:52:33 +00:00
|
|
|
@app.route("/admin/mailsettings", methods=["GET", "POST"])
|
2015-08-02 19:23:24 +00:00
|
|
|
@login_required
|
2015-10-13 16:07:17 +00:00
|
|
|
@admin_required
|
2015-08-02 19:23:24 +00:00
|
|
|
def edit_mailsettings():
|
|
|
|
content = ub.session.query(ub.Settings).first()
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
content.mail_server = to_save["mail_server"]
|
|
|
|
content.mail_port = int(to_save["mail_port"])
|
|
|
|
content.mail_login = to_save["mail_login"]
|
|
|
|
content.mail_password = to_save["mail_password"]
|
|
|
|
content.mail_from = to_save["mail_from"]
|
2017-01-18 18:07:45 +00:00
|
|
|
content.mail_use_ssl = int(to_save["mail_use_ssl"])
|
2015-08-02 19:23:24 +00:00
|
|
|
try:
|
|
|
|
ub.session.commit()
|
2018-07-29 15:51:23 +00:00
|
|
|
except Exception as e:
|
2015-08-02 19:23:24 +00:00
|
|
|
flash(e, category="error")
|
2017-01-06 13:45:43 +00:00
|
|
|
if "test" in to_save and to_save["test"]:
|
2017-03-12 18:12:17 +00:00
|
|
|
if current_user.kindle_mail:
|
2018-07-30 18:12:41 +00:00
|
|
|
result = helper.send_test_mail(current_user.kindle_mail, current_user.nickname)
|
2017-03-12 18:12:17 +00:00
|
|
|
if result is None:
|
2018-08-24 13:48:09 +00:00
|
|
|
flash(_(u"Test e-mail successfully send to %(kindlemail)s", kindlemail=current_user.kindle_mail),
|
2017-03-12 18:12:17 +00:00
|
|
|
category="success")
|
|
|
|
else:
|
2018-08-24 13:48:09 +00:00
|
|
|
flash(_(u"There was an error sending the Test e-mail: %(res)s", res=result), category="error")
|
2016-12-23 08:53:39 +00:00
|
|
|
else:
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"Please configure your kindle e-mail address first..."), category="error")
|
2017-02-23 18:58:56 +00:00
|
|
|
else:
|
2018-08-28 08:29:11 +00:00
|
|
|
flash(_(u"E-mail server settings updated"), category="success")
|
2018-08-24 13:48:09 +00:00
|
|
|
return render_title_template("email_edit.html", content=content, title=_(u"Edit e-mail server settings"),
|
|
|
|
page="mailset")
|
2015-08-02 19:23:24 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
@app.route("/admin/user/<int:user_id>", methods=["GET", "POST"])
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
2015-10-13 16:07:17 +00:00
|
|
|
@admin_required
|
2015-08-02 18:59:11 +00:00
|
|
|
def edit_user(user_id):
|
2017-08-08 16:52:00 +00:00
|
|
|
content = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first() # type: ub.User
|
2015-08-02 18:59:11 +00:00
|
|
|
downloads = list()
|
2018-08-19 08:14:20 +00:00
|
|
|
languages = speaking_language()
|
2016-11-09 18:24:33 +00:00
|
|
|
translations = babel.list_translations() + [LC('en')]
|
2015-08-02 18:59:11 +00:00
|
|
|
for book in content.downloads:
|
2018-08-24 13:48:09 +00:00
|
|
|
downloadbook = db.session.query(db.Books).filter(db.Books.id == book.book_id).first()
|
|
|
|
if downloadbook:
|
|
|
|
downloads.append(downloadbook)
|
2017-01-10 20:20:12 +00:00
|
|
|
else:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.delete_download(book.book_id)
|
|
|
|
# ub.session.query(ub.Downloads).filter(book.book_id == ub.Downloads.book_id).delete()
|
|
|
|
# ub.session.commit()
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
if "delete" in to_save:
|
2017-05-25 07:09:35 +00:00
|
|
|
ub.session.query(ub.User).filter(ub.User.id == content.id).delete()
|
|
|
|
ub.session.commit()
|
2016-12-23 08:53:39 +00:00
|
|
|
flash(_(u"User '%(nick)s' deleted", nick=content.nickname), category="success")
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('admin'))
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2017-01-12 19:43:36 +00:00
|
|
|
if "password" in to_save and to_save["password"]:
|
2016-04-03 13:12:48 +00:00
|
|
|
content.password = generate_password_hash(to_save["password"])
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
if "admin_role" in to_save and not content.role_admin():
|
|
|
|
content.role = content.role + ub.ROLE_ADMIN
|
2016-12-23 08:53:39 +00:00
|
|
|
elif "admin_role" not in to_save and content.role_admin():
|
2016-04-27 08:35:23 +00:00
|
|
|
content.role = content.role - ub.ROLE_ADMIN
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
if "download_role" in to_save and not content.role_download():
|
|
|
|
content.role = content.role + ub.ROLE_DOWNLOAD
|
2016-12-23 08:53:39 +00:00
|
|
|
elif "download_role" not in to_save and content.role_download():
|
2016-04-27 08:35:23 +00:00
|
|
|
content.role = content.role - ub.ROLE_DOWNLOAD
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
if "upload_role" in to_save and not content.role_upload():
|
|
|
|
content.role = content.role + ub.ROLE_UPLOAD
|
2016-12-23 08:53:39 +00:00
|
|
|
elif "upload_role" not in to_save and content.role_upload():
|
2016-04-27 08:35:23 +00:00
|
|
|
content.role = content.role - ub.ROLE_UPLOAD
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2016-04-27 08:35:23 +00:00
|
|
|
if "edit_role" in to_save and not content.role_edit():
|
|
|
|
content.role = content.role + ub.ROLE_EDIT
|
2016-12-23 08:53:39 +00:00
|
|
|
elif "edit_role" not in to_save and content.role_edit():
|
2016-04-27 08:35:23 +00:00
|
|
|
content.role = content.role - ub.ROLE_EDIT
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
if "delete_role" in to_save and not content.role_delete_books():
|
|
|
|
content.role = content.role + ub.ROLE_DELETE_BOOKS
|
|
|
|
elif "delete_role" not in to_save and content.role_delete_books():
|
|
|
|
content.role = content.role - ub.ROLE_DELETE_BOOKS
|
|
|
|
|
2016-04-27 14:00:58 +00:00
|
|
|
if "passwd_role" in to_save and not content.role_passwd():
|
|
|
|
content.role = content.role + ub.ROLE_PASSWD
|
2016-12-23 08:53:39 +00:00
|
|
|
elif "passwd_role" not in to_save and content.role_passwd():
|
2016-04-27 14:00:58 +00:00
|
|
|
content.role = content.role - ub.ROLE_PASSWD
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-03-19 19:29:35 +00:00
|
|
|
if "edit_shelf_role" in to_save and not content.role_edit_shelfs():
|
|
|
|
content.role = content.role + ub.ROLE_EDIT_SHELFS
|
|
|
|
elif "edit_shelf_role" not in to_save and content.role_edit_shelfs():
|
|
|
|
content.role = content.role - ub.ROLE_EDIT_SHELFS
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_random" in to_save and not content.show_random_books():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RANDOM
|
|
|
|
elif "show_random" not in to_save and content.show_random_books():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_RANDOM
|
|
|
|
|
|
|
|
if "show_language" in to_save and not content.show_language():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_LANGUAGE
|
|
|
|
elif "show_language" not in to_save and content.show_language():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_LANGUAGE
|
|
|
|
|
|
|
|
if "show_series" in to_save and not content.show_series():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_SERIES
|
|
|
|
elif "show_series" not in to_save and content.show_series():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_SERIES
|
|
|
|
|
|
|
|
if "show_category" in to_save and not content.show_category():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_CATEGORY
|
|
|
|
elif "show_category" not in to_save and content.show_category():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_CATEGORY
|
|
|
|
|
2017-11-12 13:06:33 +00:00
|
|
|
if "show_recent" in to_save and not content.show_recent():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_RECENT
|
|
|
|
elif "show_recent" not in to_save and content.show_recent():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_RECENT
|
|
|
|
|
|
|
|
if "show_sorted" in to_save and not content.show_sorted():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_SORTED
|
|
|
|
elif "show_sorted" not in to_save and content.show_sorted():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_SORTED
|
|
|
|
|
2019-01-26 18:06:54 +00:00
|
|
|
if "show_publisher" in to_save and not content.show_publisher():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_PUBLISHER
|
|
|
|
elif "show_publisher" not in to_save and content.show_publisher():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_PUBLISHER
|
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_hot" in to_save and not content.show_hot_books():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_HOT
|
|
|
|
elif "show_hot" not in to_save and content.show_hot_books():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_HOT
|
|
|
|
|
2017-02-04 13:28:18 +00:00
|
|
|
if "show_best_rated" in to_save and not content.show_best_rated_books():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_BEST_RATED
|
|
|
|
elif "show_best_rated" not in to_save and content.show_best_rated_books():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_BEST_RATED
|
|
|
|
|
2017-03-19 16:14:16 +00:00
|
|
|
if "show_read_and_unread" in to_save and not content.show_read_and_unread():
|
2017-03-03 21:09:53 +00:00
|
|
|
content.sidebar_view += ub.SIDEBAR_READ_AND_UNREAD
|
|
|
|
elif "show_read_and_unread" not in to_save and content.show_read_and_unread():
|
2017-03-19 16:14:16 +00:00
|
|
|
content.sidebar_view -= ub.SIDEBAR_READ_AND_UNREAD
|
2017-03-03 21:09:53 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
if "show_author" in to_save and not content.show_author():
|
|
|
|
content.sidebar_view += ub.SIDEBAR_AUTHOR
|
|
|
|
elif "show_author" not in to_save and content.show_author():
|
|
|
|
content.sidebar_view -= ub.SIDEBAR_AUTHOR
|
|
|
|
|
|
|
|
if "show_detail_random" in to_save and not content.show_detail_random():
|
|
|
|
content.sidebar_view += ub.DETAIL_RANDOM
|
|
|
|
elif "show_detail_random" not in to_save and content.show_detail_random():
|
|
|
|
content.sidebar_view -= ub.DETAIL_RANDOM
|
|
|
|
|
2017-08-08 16:52:00 +00:00
|
|
|
content.mature_content = "show_mature_content" in to_save
|
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
if "default_language" in to_save:
|
|
|
|
content.default_language = to_save["default_language"]
|
2017-01-12 19:43:36 +00:00
|
|
|
if "locale" in to_save and to_save["locale"]:
|
2016-11-09 18:24:33 +00:00
|
|
|
content.locale = to_save["locale"]
|
2015-10-13 16:07:17 +00:00
|
|
|
if to_save["email"] and to_save["email"] != content.email:
|
|
|
|
content.email = to_save["email"]
|
2016-08-08 19:01:38 +00:00
|
|
|
if "kindle_mail" in to_save and to_save["kindle_mail"] != content.kindle_mail:
|
2015-10-13 16:07:17 +00:00
|
|
|
content.kindle_mail = to_save["kindle_mail"]
|
|
|
|
try:
|
|
|
|
ub.session.commit()
|
2016-12-23 08:53:39 +00:00
|
|
|
flash(_(u"User '%(nick)s' updated", nick=content.nickname), category="success")
|
2015-10-13 16:07:17 +00:00
|
|
|
except IntegrityError:
|
|
|
|
ub.session.rollback()
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"An unknown error occured."), category="error")
|
2017-01-22 20:30:36 +00:00
|
|
|
return render_title_template("user_edit.html", translations=translations, languages=languages, new_user=0,
|
2018-07-15 16:50:35 +00:00
|
|
|
content=content, downloads=downloads, title=_(u"Edit User %(nick)s",
|
|
|
|
nick=content.nickname), page="edituser")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
@app.route("/admin/resetpassword/<int:user_id>")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def reset_password(user_id):
|
|
|
|
if not config.config_public_reg:
|
|
|
|
abort(404)
|
|
|
|
if current_user is not None and current_user.is_authenticated:
|
|
|
|
existing_user = ub.session.query(ub.User).filter(ub.User.id == user_id).first()
|
|
|
|
password = helper.generate_random_password()
|
|
|
|
existing_user.password = generate_password_hash(password)
|
|
|
|
try:
|
|
|
|
ub.session.commit()
|
|
|
|
helper.send_registration_mail(existing_user.email, existing_user.nickname, password, True)
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Password for user %(user)s reset", user=existing_user.nickname), category="success")
|
2018-08-24 13:48:09 +00:00
|
|
|
except Exception:
|
|
|
|
ub.session.rollback()
|
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
|
|
|
return redirect(url_for('admin'))
|
|
|
|
|
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
def render_edit_book(book_id):
|
2016-12-23 08:53:39 +00:00
|
|
|
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
2016-04-20 16:56:03 +00:00
|
|
|
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
2017-08-08 16:52:00 +00:00
|
|
|
book = db.session.query(db.Books)\
|
2017-08-26 15:12:16 +00:00
|
|
|
.filter(db.Books.id == book_id).filter(common_filters()).first()
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2017-07-09 23:25:59 +00:00
|
|
|
if not book:
|
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible"), category="error")
|
|
|
|
return redirect(url_for("index"))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-08-28 12:07:48 +00:00
|
|
|
for indx in range(0, len(book.languages)):
|
2018-09-09 08:13:08 +00:00
|
|
|
book.languages[indx].language_name = language_table[get_locale()][book.languages[indx].lang_code]
|
2019-01-06 14:00:34 +00:00
|
|
|
|
2019-01-06 13:16:52 +00:00
|
|
|
book = order_authors(book)
|
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
author_names = []
|
2018-08-28 12:07:48 +00:00
|
|
|
for authr in book.authors:
|
|
|
|
author_names.append(authr.name.replace('|', ','))
|
2017-07-09 23:25:59 +00:00
|
|
|
|
2018-08-29 03:32:30 +00:00
|
|
|
# Option for showing convertbook button
|
2018-09-09 16:17:15 +00:00
|
|
|
valid_source_formats=list()
|
2018-08-28 01:27:16 +00:00
|
|
|
if config.config_ebookconverter == 2:
|
2018-09-09 16:17:15 +00:00
|
|
|
for file in book.data:
|
|
|
|
if file.format.lower() in EXTENSIONS_CONVERT:
|
|
|
|
valid_source_formats.append(file.format.lower())
|
2018-08-28 01:27:16 +00:00
|
|
|
|
2018-08-29 03:32:30 +00:00
|
|
|
# Determine what formats don't already exist
|
2018-09-09 16:17:15 +00:00
|
|
|
allowed_conversion_formats = EXTENSIONS_CONVERT.copy()
|
2018-08-28 01:27:16 +00:00
|
|
|
for file in book.data:
|
2018-08-28 23:22:17 +00:00
|
|
|
try:
|
|
|
|
allowed_conversion_formats.remove(file.format.lower())
|
|
|
|
except Exception:
|
2018-08-29 03:32:30 +00:00
|
|
|
app.logger.warning(file.format.lower() + ' already removed from list.')
|
2018-08-28 01:27:16 +00:00
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,
|
|
|
|
title=_(u"edit metadata"), page="editbook",
|
|
|
|
conversion_formats=allowed_conversion_formats,
|
|
|
|
source_formats=valid_source_formats)
|
2018-08-28 01:27:16 +00:00
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
|
|
|
|
def edit_cc_data(book_id, book, to_save):
|
|
|
|
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
|
|
|
for c in cc:
|
|
|
|
cc_string = "custom_column_" + str(c.id)
|
|
|
|
if not c.is_multiple:
|
|
|
|
if len(getattr(book, cc_string)) > 0:
|
|
|
|
cc_db_value = getattr(book, cc_string)[0].value
|
|
|
|
else:
|
|
|
|
cc_db_value = None
|
|
|
|
if to_save[cc_string].strip():
|
|
|
|
if c.datatype == 'bool':
|
|
|
|
if to_save[cc_string] == 'None':
|
|
|
|
to_save[cc_string] = None
|
|
|
|
else:
|
|
|
|
to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0
|
|
|
|
if to_save[cc_string] != cc_db_value:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
if to_save[cc_string] is not None:
|
|
|
|
setattr(getattr(book, cc_string)[0], 'value', to_save[cc_string])
|
|
|
|
else:
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
else:
|
|
|
|
cc_class = db.cc_classes[c.id]
|
|
|
|
new_cc = cc_class(value=to_save[cc_string], book=book_id)
|
|
|
|
db.session.add(new_cc)
|
|
|
|
elif c.datatype == 'int':
|
|
|
|
if to_save[cc_string] == 'None':
|
|
|
|
to_save[cc_string] = None
|
|
|
|
if to_save[cc_string] != cc_db_value:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
if to_save[cc_string] is not None:
|
|
|
|
setattr(getattr(book, cc_string)[0], 'value', to_save[cc_string])
|
|
|
|
else:
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
else:
|
|
|
|
cc_class = db.cc_classes[c.id]
|
|
|
|
new_cc = cc_class(value=to_save[cc_string], book=book_id)
|
|
|
|
db.session.add(new_cc)
|
|
|
|
|
|
|
|
else:
|
|
|
|
if c.datatype == 'rating':
|
|
|
|
to_save[cc_string] = str(int(float(to_save[cc_string]) * 2))
|
|
|
|
if to_save[cc_string].strip() != cc_db_value:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
# remove old cc_val
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
if len(del_cc.books) == 0:
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
cc_class = db.cc_classes[c.id]
|
|
|
|
new_cc = db.session.query(cc_class).filter(
|
|
|
|
cc_class.value == to_save[cc_string].strip()).first()
|
|
|
|
# if no cc val is found add it
|
|
|
|
if new_cc is None:
|
|
|
|
new_cc = cc_class(value=to_save[cc_string].strip())
|
|
|
|
db.session.add(new_cc)
|
|
|
|
db.session.flush()
|
|
|
|
new_cc = db.session.query(cc_class).filter(
|
|
|
|
cc_class.value == to_save[cc_string].strip()).first()
|
|
|
|
# add cc value to book
|
|
|
|
getattr(book, cc_string).append(new_cc)
|
|
|
|
else:
|
|
|
|
if cc_db_value is not None:
|
|
|
|
# remove old cc_val
|
|
|
|
del_cc = getattr(book, cc_string)[0]
|
|
|
|
getattr(book, cc_string).remove(del_cc)
|
|
|
|
if len(del_cc.books) == 0:
|
|
|
|
db.session.delete(del_cc)
|
|
|
|
else:
|
|
|
|
input_tags = to_save[cc_string].split(',')
|
|
|
|
input_tags = list(map(lambda it: it.strip(), input_tags))
|
|
|
|
modify_database_object(input_tags, getattr(book, cc_string), db.cc_classes[c.id], db.session,
|
|
|
|
'custom')
|
2018-12-24 16:05:47 +00:00
|
|
|
return cc
|
2018-09-30 07:43:20 +00:00
|
|
|
|
|
|
|
def upload_single_file(request, book, book_id):
|
2017-08-09 17:56:46 +00:00
|
|
|
# Check and handle Uploaded file
|
2018-08-18 10:15:32 +00:00
|
|
|
if 'btn-upload-format' in request.files:
|
2017-08-09 17:56:46 +00:00
|
|
|
requested_file = request.files['btn-upload-format']
|
2018-08-18 20:00:43 +00:00
|
|
|
# check for empty request
|
|
|
|
if requested_file.filename != '':
|
|
|
|
if '.' in requested_file.filename:
|
|
|
|
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
2018-09-09 16:17:15 +00:00
|
|
|
if file_ext not in EXTENSIONS_UPLOAD:
|
2018-09-08 12:50:52 +00:00
|
|
|
flash(_("File extension '%(ext)s' is not allowed to be uploaded to this server", ext=file_ext),
|
2018-08-28 12:07:48 +00:00
|
|
|
category="error")
|
2018-08-18 20:00:43 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
|
|
|
else:
|
|
|
|
flash(_('File to be uploaded must have an extension'), category="error")
|
2018-08-18 10:15:32 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2017-08-09 17:56:46 +00:00
|
|
|
|
2018-08-18 20:00:43 +00:00
|
|
|
file_name = book.path.rsplit('/', 1)[-1]
|
|
|
|
filepath = os.path.normpath(os.path.join(config.config_calibre_dir, book.path))
|
|
|
|
saved_filename = os.path.join(filepath, file_name + '.' + file_ext)
|
2017-08-09 17:56:46 +00:00
|
|
|
|
2018-08-18 20:00:43 +00:00
|
|
|
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
try:
|
|
|
|
os.makedirs(filepath)
|
|
|
|
except OSError:
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Failed to create path %(path)s (Permission denied).", path=filepath), category="error")
|
2018-08-18 20:00:43 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2018-08-18 10:15:32 +00:00
|
|
|
try:
|
2018-08-18 20:00:43 +00:00
|
|
|
requested_file.save(saved_filename)
|
2018-08-18 10:15:32 +00:00
|
|
|
except OSError:
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Failed to store file %(file)s.", file=saved_filename), category="error")
|
2018-08-18 10:15:32 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2017-08-09 17:56:46 +00:00
|
|
|
|
2018-08-18 20:00:43 +00:00
|
|
|
file_size = os.path.getsize(saved_filename)
|
2018-11-25 10:25:20 +00:00
|
|
|
is_format = db.session.query(db.Data).filter(db.Data.book == book_id).\
|
|
|
|
filter(db.Data.format == file_ext.upper()).first()
|
2018-08-18 20:00:43 +00:00
|
|
|
|
|
|
|
# Format entry already exists, no need to update the database
|
|
|
|
if is_format:
|
|
|
|
app.logger.info('Book format already existing')
|
|
|
|
else:
|
|
|
|
db_format = db.Data(book_id, file_ext.upper(), file_size, file_name)
|
|
|
|
db.session.add(db_format)
|
|
|
|
db.session.commit()
|
|
|
|
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
2018-08-18 10:15:32 +00:00
|
|
|
|
2018-08-18 20:00:43 +00:00
|
|
|
# Queue uploader info
|
2018-09-08 10:28:48 +00:00
|
|
|
uploadText=_(u"File format %(ext)s added to %(book)s", ext=file_ext.upper(), book=book.title)
|
2018-09-30 07:43:20 +00:00
|
|
|
helper.global_WorkerThread.add_upload(current_user.nickname,
|
2018-08-28 12:07:48 +00:00
|
|
|
"<a href=\"" + url_for('show_book', book_id=book.id) + "\">" + uploadText + "</a>")
|
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
def upload_cover(request, book):
|
2018-08-28 12:07:48 +00:00
|
|
|
if 'btn-upload-cover' in request.files:
|
|
|
|
requested_file = request.files['btn-upload-cover']
|
|
|
|
# check for empty request
|
|
|
|
if requested_file.filename != '':
|
|
|
|
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
|
|
|
filepath = os.path.normpath(os.path.join(config.config_calibre_dir, book.path))
|
|
|
|
saved_filename = os.path.join(filepath, 'cover.' + file_ext)
|
2017-08-09 17:56:46 +00:00
|
|
|
|
2018-08-28 12:07:48 +00:00
|
|
|
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
try:
|
|
|
|
os.makedirs(filepath)
|
|
|
|
except OSError:
|
2018-11-25 10:25:20 +00:00
|
|
|
flash(_(u"Failed to create path for cover %(path)s (Permission denied).", cover=filepath),
|
|
|
|
category="error")
|
2018-08-28 12:07:48 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
|
|
|
try:
|
|
|
|
requested_file.save(saved_filename)
|
|
|
|
# im=Image.open(saved_filename)
|
|
|
|
book.has_cover = 1
|
|
|
|
except OSError:
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Failed to store cover-file %(cover)s.", cover=saved_filename), category="error")
|
2018-08-28 12:07:48 +00:00
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
|
|
|
except IOError:
|
|
|
|
flash(_(u"Cover-file is not a valid image file" % saved_filename), category="error")
|
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
@app.route("/admin/book/<int:book_id>", methods=['GET', 'POST'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
@edit_required
|
|
|
|
def edit_book(book_id):
|
|
|
|
# Show form
|
|
|
|
if request.method != 'POST':
|
|
|
|
return render_edit_book(book_id)
|
|
|
|
|
|
|
|
# create the function for sorting...
|
|
|
|
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).filter(common_filters()).first()
|
|
|
|
|
|
|
|
# Book not found
|
|
|
|
if not book:
|
|
|
|
flash(_(u"Error opening eBook. File does not exist or file is not accessible"), category="error")
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
|
|
|
upload_single_file(request, book, book_id)
|
|
|
|
upload_cover(request, book)
|
2018-07-08 07:28:35 +00:00
|
|
|
try:
|
2018-09-30 07:43:20 +00:00
|
|
|
to_save = request.form.to_dict()
|
2018-09-09 08:13:08 +00:00
|
|
|
# Update book
|
2018-09-30 07:43:20 +00:00
|
|
|
edited_books_id = None
|
2018-09-09 08:13:08 +00:00
|
|
|
#handle book title
|
2018-10-03 12:03:08 +00:00
|
|
|
if book.title != to_save["book_title"].rstrip().strip():
|
2018-09-30 07:43:20 +00:00
|
|
|
if to_save["book_title"] == '':
|
|
|
|
to_save["book_title"] = _(u'unknown')
|
2018-10-03 12:03:08 +00:00
|
|
|
book.title = to_save["book_title"].rstrip().strip()
|
2018-09-30 07:43:20 +00:00
|
|
|
edited_books_id = book.id
|
2017-07-09 23:27:46 +00:00
|
|
|
|
2018-09-09 08:13:08 +00:00
|
|
|
# handle author(s)
|
2018-07-08 07:28:35 +00:00
|
|
|
input_authors = to_save["author_name"].split('&')
|
|
|
|
input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
|
|
|
|
# we have all author names now
|
|
|
|
if input_authors == ['']:
|
|
|
|
input_authors = [_(u'unknown')] # prevent empty Author
|
2019-01-05 20:44:29 +00:00
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
modify_database_object(input_authors, book.authors, db.Authors, db.session, 'author')
|
2019-01-05 20:44:29 +00:00
|
|
|
|
2019-01-06 13:16:52 +00:00
|
|
|
# Search for each author if author is in database, if not, authorname and sorted authorname is generated new
|
|
|
|
# everything then is assembled for sorted author field in database
|
2019-01-05 20:44:29 +00:00
|
|
|
sort_authors_list = list()
|
|
|
|
for inp in input_authors:
|
2019-01-06 13:16:52 +00:00
|
|
|
stored_author = db.session.query(db.Authors).filter(db.Authors.name == inp).first()
|
|
|
|
if not stored_author:
|
|
|
|
stored_author = helper.get_sorted_author(inp)
|
|
|
|
else:
|
|
|
|
stored_author = stored_author.sort
|
|
|
|
sort_authors_list.append(helper.get_sorted_author(stored_author))
|
2019-01-05 20:44:29 +00:00
|
|
|
sort_authors = ' & '.join(sort_authors_list)
|
|
|
|
if book.author_sort != sort_authors:
|
|
|
|
edited_books_id = book.id
|
|
|
|
book.author_sort = sort_authors
|
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
|
2018-07-14 06:31:52 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
error = False
|
2018-09-30 07:43:20 +00:00
|
|
|
if edited_books_id:
|
2019-01-05 20:44:29 +00:00
|
|
|
error = helper.update_dir_stucture(edited_books_id, config.config_calibre_dir, input_authors[0])
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
if not error:
|
|
|
|
if to_save["cover_url"]:
|
2018-08-04 15:08:32 +00:00
|
|
|
if helper.save_cover(to_save["cover_url"], book.path) is True:
|
2018-07-08 07:28:35 +00:00
|
|
|
book.has_cover = 1
|
|
|
|
else:
|
|
|
|
flash(_(u"Cover is not a jpg file, can't save"), category="error")
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
if book.series_index != to_save["series_index"]:
|
|
|
|
book.series_index = to_save["series_index"]
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-09-09 08:13:08 +00:00
|
|
|
# Handle book comments/description
|
2018-07-08 07:28:35 +00:00
|
|
|
if len(book.comments):
|
|
|
|
book.comments[0].text = to_save["description"]
|
|
|
|
else:
|
|
|
|
book.comments.append(db.Comments(text=to_save["description"], book=book.id))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-09-09 08:13:08 +00:00
|
|
|
# Handle book tags
|
2018-07-08 07:28:35 +00:00
|
|
|
input_tags = to_save["tags"].split(',')
|
|
|
|
input_tags = list(map(lambda it: it.strip(), input_tags))
|
|
|
|
modify_database_object(input_tags, book.tags, db.Tags, db.session, 'tags')
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2018-09-09 08:13:08 +00:00
|
|
|
# Handle book series
|
2018-07-08 07:28:35 +00:00
|
|
|
input_series = [to_save["series"].strip()]
|
|
|
|
input_series = [x for x in input_series if x != '']
|
|
|
|
modify_database_object(input_series, book.series, db.Series, db.session, 'series')
|
2017-11-30 15:49:46 +00:00
|
|
|
|
2018-07-08 07:28:35 +00:00
|
|
|
if to_save["pubdate"]:
|
|
|
|
try:
|
|
|
|
book.pubdate = datetime.datetime.strptime(to_save["pubdate"], "%Y-%m-%d")
|
|
|
|
except ValueError:
|
|
|
|
book.pubdate = db.Books.DEFAULT_PUBDATE
|
|
|
|
else:
|
2017-11-30 15:49:46 +00:00
|
|
|
book.pubdate = db.Books.DEFAULT_PUBDATE
|
2018-09-30 12:08:55 +00:00
|
|
|
|
|
|
|
if to_save["publisher"]:
|
2018-10-28 13:40:31 +00:00
|
|
|
publisher = to_save["publisher"].rstrip().strip()
|
|
|
|
if len(book.publishers) == 0 or (len(book.publishers) > 0 and publisher != book.publishers[0].name):
|
|
|
|
modify_database_object([publisher], book.publishers, db.Publishers, db.session, 'publisher')
|
|
|
|
elif len(book.publishers):
|
|
|
|
modify_database_object([], book.publishers, db.Publishers, db.session, 'publisher')
|
|
|
|
|
2018-09-02 15:21:01 +00:00
|
|
|
|
2018-09-09 08:13:08 +00:00
|
|
|
# handle book languages
|
|
|
|
input_languages = to_save["languages"].split(',')
|
|
|
|
input_languages = [x.strip().lower() for x in input_languages if x != '']
|
2018-07-08 07:28:35 +00:00
|
|
|
input_l = []
|
2018-09-09 08:13:08 +00:00
|
|
|
invers_lang_table = [x.lower() for x in language_table[get_locale()].values()]
|
|
|
|
for lang in input_languages:
|
2018-07-08 07:28:35 +00:00
|
|
|
try:
|
2018-09-09 08:13:08 +00:00
|
|
|
res = list(language_table[get_locale()].keys())[invers_lang_table.index(lang)]
|
|
|
|
input_l.append(res)
|
|
|
|
except ValueError:
|
|
|
|
app.logger.error('%s is not a valid language' % lang)
|
|
|
|
flash(_(u"%(langname)s is not a valid language", langname=lang), category="error")
|
2018-07-08 07:28:35 +00:00
|
|
|
modify_database_object(input_l, book.languages, db.Languages, db.session, 'languages')
|
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
# handle book ratings
|
2018-07-08 07:28:35 +00:00
|
|
|
if to_save["rating"].strip():
|
|
|
|
old_rating = False
|
|
|
|
if len(book.ratings) > 0:
|
|
|
|
old_rating = book.ratings[0].rating
|
|
|
|
ratingx2 = int(float(to_save["rating"]) * 2)
|
|
|
|
if ratingx2 != old_rating:
|
|
|
|
is_rating = db.session.query(db.Ratings).filter(db.Ratings.rating == ratingx2).first()
|
|
|
|
if is_rating:
|
|
|
|
book.ratings.append(is_rating)
|
|
|
|
else:
|
|
|
|
new_rating = db.Ratings(rating=ratingx2)
|
|
|
|
book.ratings.append(new_rating)
|
|
|
|
if old_rating:
|
|
|
|
book.ratings.remove(book.ratings[0])
|
|
|
|
else:
|
|
|
|
if len(book.ratings) > 0:
|
2017-11-30 15:49:46 +00:00
|
|
|
book.ratings.remove(book.ratings[0])
|
2018-07-08 07:28:35 +00:00
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
# handle cc data
|
|
|
|
edit_cc_data(book_id, book, to_save)
|
2018-07-08 07:28:35 +00:00
|
|
|
|
|
|
|
db.session.commit()
|
2018-08-18 10:15:32 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
2018-07-08 07:28:35 +00:00
|
|
|
if "detail_view" in to_save:
|
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2016-11-09 18:24:33 +00:00
|
|
|
else:
|
2018-10-03 19:18:08 +00:00
|
|
|
flash(_("Metadata successfully updated"), category="success")
|
2018-09-30 07:43:20 +00:00
|
|
|
return render_edit_book(book_id)
|
2017-07-09 23:27:46 +00:00
|
|
|
else:
|
2018-07-08 07:28:35 +00:00
|
|
|
db.session.rollback()
|
2018-08-19 08:14:20 +00:00
|
|
|
flash(error, category="error")
|
2018-09-30 07:43:20 +00:00
|
|
|
return render_edit_book(book_id)
|
2018-07-08 07:28:35 +00:00
|
|
|
except Exception as e:
|
|
|
|
app.logger.exception(e)
|
2017-11-30 15:49:46 +00:00
|
|
|
db.session.rollback()
|
2018-07-08 07:28:35 +00:00
|
|
|
flash(_("Error editing book, please check logfile for details"), category="error")
|
|
|
|
return redirect(url_for('show_book', book_id=book.id))
|
2016-04-03 21:52:32 +00:00
|
|
|
|
2016-06-05 15:41:47 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
@app.route("/upload", methods=["GET", "POST"])
|
2017-01-12 19:43:36 +00:00
|
|
|
@login_required_if_no_ano
|
2016-04-27 08:35:23 +00:00
|
|
|
@upload_required
|
2016-04-03 21:52:32 +00:00
|
|
|
def upload():
|
2017-01-22 20:30:36 +00:00
|
|
|
if not config.config_uploading:
|
2016-04-15 17:39:25 +00:00
|
|
|
abort(404)
|
2016-04-03 21:52:32 +00:00
|
|
|
if request.method == 'POST' and 'btn-upload' in request.files:
|
2018-03-06 12:31:46 +00:00
|
|
|
for requested_file in request.files.getlist("btn-upload"):
|
2018-08-16 19:17:26 +00:00
|
|
|
# create the function for sorting...
|
2018-03-06 12:31:46 +00:00
|
|
|
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
|
|
|
db.session.connection().connection.connection.create_function('uuid4', 0, lambda: str(uuid4()))
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# check if file extension is correct
|
2018-03-06 12:31:46 +00:00
|
|
|
if '.' in requested_file.filename:
|
|
|
|
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
2018-09-09 16:17:15 +00:00
|
|
|
if file_ext not in EXTENSIONS_UPLOAD:
|
2018-03-06 12:31:46 +00:00
|
|
|
flash(
|
2018-09-08 12:50:52 +00:00
|
|
|
_("File extension '%(ext)s' is not allowed to be uploaded to this server",
|
2018-09-08 10:28:48 +00:00
|
|
|
ext=file_ext), category="error")
|
2018-03-06 12:31:46 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
else:
|
|
|
|
flash(_('File to be uploaded must have an extension'), category="error")
|
2017-02-23 05:52:58 +00:00
|
|
|
return redirect(url_for('index'))
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# extract metadata from file
|
2018-03-06 12:31:46 +00:00
|
|
|
meta = uploader.upload(requested_file)
|
|
|
|
title = meta.title
|
2018-08-28 12:07:48 +00:00
|
|
|
authr = meta.author
|
2018-03-06 12:31:46 +00:00
|
|
|
tags = meta.tags
|
|
|
|
series = meta.series
|
|
|
|
series_index = meta.series_id
|
|
|
|
title_dir = helper.get_valid_filename(title)
|
2018-08-28 12:07:48 +00:00
|
|
|
author_dir = helper.get_valid_filename(authr)
|
2018-08-18 10:15:32 +00:00
|
|
|
filepath = os.path.join(config.config_calibre_dir, author_dir, title_dir)
|
|
|
|
saved_filename = os.path.join(filepath, title_dir + meta.extension.lower())
|
2018-03-06 12:31:46 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
|
2018-03-06 12:31:46 +00:00
|
|
|
if not os.path.exists(filepath):
|
|
|
|
try:
|
|
|
|
os.makedirs(filepath)
|
|
|
|
except OSError:
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Failed to create path %(path)s (Permission denied).", path=filepath), category="error")
|
2018-03-06 12:31:46 +00:00
|
|
|
return redirect(url_for('index'))
|
2016-04-17 15:42:22 +00:00
|
|
|
try:
|
2018-03-06 12:31:46 +00:00
|
|
|
copyfile(meta.file_path, saved_filename)
|
2016-04-17 15:42:22 +00:00
|
|
|
except OSError:
|
2018-09-08 10:28:48 +00:00
|
|
|
flash(_(u"Failed to store file %(file)s (Permission denied).", file=saved_filename), category="error")
|
2017-01-17 03:09:02 +00:00
|
|
|
return redirect(url_for('index'))
|
2018-03-06 12:31:46 +00:00
|
|
|
try:
|
|
|
|
os.unlink(meta.file_path)
|
|
|
|
except OSError:
|
2018-11-25 10:25:20 +00:00
|
|
|
flash(_(u"Failed to delete file %(file)s (Permission denied).", file= meta.file_path),
|
|
|
|
category="warning")
|
2016-06-05 15:41:47 +00:00
|
|
|
|
2018-03-06 12:31:46 +00:00
|
|
|
if meta.cover is None:
|
|
|
|
has_cover = 0
|
2018-11-25 10:25:20 +00:00
|
|
|
copyfile(os.path.join(config.get_main_dir, "cps/static/generic_cover.jpg"),
|
|
|
|
os.path.join(filepath, "cover.jpg"))
|
2018-03-06 12:31:46 +00:00
|
|
|
else:
|
|
|
|
has_cover = 1
|
|
|
|
move(meta.cover, os.path.join(filepath, "cover.jpg"))
|
2016-06-05 15:41:47 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# handle authors
|
2018-08-28 12:07:48 +00:00
|
|
|
is_author = db.session.query(db.Authors).filter(db.Authors.name == authr).first()
|
2018-03-06 12:31:46 +00:00
|
|
|
if is_author:
|
|
|
|
db_author = is_author
|
2017-03-02 14:57:02 +00:00
|
|
|
else:
|
2018-08-28 12:07:48 +00:00
|
|
|
db_author = db.Authors(authr, helper.get_sorted_author(authr), "")
|
2018-03-06 12:31:46 +00:00
|
|
|
db.session.add(db_author)
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# handle series
|
2018-03-06 12:31:46 +00:00
|
|
|
db_series = None
|
|
|
|
is_series = db.session.query(db.Series).filter(db.Series.name == series).first()
|
|
|
|
if is_series:
|
|
|
|
db_series = is_series
|
|
|
|
elif series != '':
|
|
|
|
db_series = db.Series(series, "")
|
|
|
|
db.session.add(db_series)
|
|
|
|
|
|
|
|
# add language actually one value in list
|
|
|
|
input_language = meta.languages
|
|
|
|
db_language = None
|
|
|
|
if input_language != "":
|
|
|
|
input_language = isoLanguages.get(name=input_language).part3
|
|
|
|
hasLanguage = db.session.query(db.Languages).filter(db.Languages.lang_code == input_language).first()
|
|
|
|
if hasLanguage:
|
|
|
|
db_language = hasLanguage
|
|
|
|
else:
|
|
|
|
db_language = db.Languages(input_language)
|
|
|
|
db.session.add(db_language)
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-03-06 12:31:46 +00:00
|
|
|
# combine path and normalize path from windows systems
|
|
|
|
path = os.path.join(author_dir, title_dir).replace('\\', '/')
|
|
|
|
db_book = db.Books(title, "", db_author.sort, datetime.datetime.now(), datetime.datetime(101, 1, 1),
|
|
|
|
series_index, datetime.datetime.now(), path, has_cover, db_author, [], db_language)
|
|
|
|
db_book.authors.append(db_author)
|
|
|
|
if db_series:
|
|
|
|
db_book.series.append(db_series)
|
|
|
|
if db_language is not None:
|
|
|
|
db_book.languages.append(db_language)
|
2018-08-19 08:14:20 +00:00
|
|
|
file_size = os.path.getsize(saved_filename)
|
2018-08-18 10:15:32 +00:00
|
|
|
db_data = db.Data(db_book, meta.extension.upper()[1:], file_size, title_dir)
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# handle tags
|
2018-07-14 11:48:51 +00:00
|
|
|
input_tags = tags.split(',')
|
|
|
|
input_tags = list(map(lambda it: it.strip(), input_tags))
|
|
|
|
if input_tags[0] !="":
|
|
|
|
modify_database_object(input_tags, db_book.tags, db.Tags, db.session, 'tags')
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# flush content, get db_book.id available
|
2018-03-06 12:31:46 +00:00
|
|
|
db_book.data.append(db_data)
|
|
|
|
db.session.add(db_book)
|
2018-08-19 08:14:20 +00:00
|
|
|
db.session.flush()
|
2018-07-14 06:31:52 +00:00
|
|
|
|
2018-03-06 12:31:46 +00:00
|
|
|
# add comment
|
2018-07-14 11:48:51 +00:00
|
|
|
book_id = db_book.id
|
2018-03-06 12:31:46 +00:00
|
|
|
upload_comment = Markup(meta.description).unescape()
|
|
|
|
if upload_comment != "":
|
2018-07-14 11:48:51 +00:00
|
|
|
db.session.add(db.Comments(upload_comment, book_id))
|
2018-10-03 19:58:37 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# save data to database, reread data
|
2018-07-14 06:31:52 +00:00
|
|
|
db.session.commit()
|
2018-07-14 21:03:54 +00:00
|
|
|
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
2018-11-25 10:25:20 +00:00
|
|
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).filter(common_filters()).first()
|
2018-07-14 11:48:51 +00:00
|
|
|
|
2018-08-18 10:15:32 +00:00
|
|
|
# upload book to gdrive if nesseccary and add "(bookid)" to folder name
|
2018-07-14 06:31:52 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
2019-02-18 19:52:58 +00:00
|
|
|
time.sleep(10)
|
2018-07-14 11:48:51 +00:00
|
|
|
error = helper.update_dir_stucture(book.id, config.config_calibre_dir)
|
2018-08-18 10:15:32 +00:00
|
|
|
db.session.commit()
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
gdriveutils.updateGdriveCalibreFromLocal()
|
2018-07-14 06:31:52 +00:00
|
|
|
if error:
|
2018-08-16 19:17:26 +00:00
|
|
|
flash(error, category="error")
|
2018-11-18 16:09:13 +00:00
|
|
|
uploadText=(u"File %s" % book.title)
|
2018-10-03 19:58:37 +00:00
|
|
|
helper.global_WorkerThread.add_upload(current_user.nickname,
|
2018-08-16 19:17:26 +00:00
|
|
|
"<a href=\"" + url_for('show_book', book_id=book.id) + "\">" + uploadText + "</a>")
|
2018-07-14 11:48:51 +00:00
|
|
|
|
2018-08-19 08:14:20 +00:00
|
|
|
# create data for displaying display Full language name instead of iso639.part3language
|
|
|
|
if db_language is not None:
|
2018-07-14 11:48:51 +00:00
|
|
|
book.languages[0].language_name = _(meta.languages)
|
2018-03-06 12:31:46 +00:00
|
|
|
author_names = []
|
|
|
|
for author in db_book.authors:
|
|
|
|
author_names.append(author.name)
|
2018-03-07 07:40:48 +00:00
|
|
|
if len(request.files.getlist("btn-upload")) < 2:
|
|
|
|
if current_user.role_edit() or current_user.role_admin():
|
2019-02-16 09:05:51 +00:00
|
|
|
resp = {"location": url_for('edit_book', book_id=db_book.id)}
|
|
|
|
return Response(json.dumps(resp), mimetype='application/json')
|
|
|
|
else:
|
|
|
|
resp = {"location": url_for('show_book', book_id=db_book.id)}
|
|
|
|
return Response(json.dumps(resp), mimetype='application/json')
|
|
|
|
return Response(json.dumps({"location": url_for("index")}), mimetype='application/json')
|
2017-03-13 00:44:20 +00:00
|
|
|
|
2018-08-28 23:13:04 +00:00
|
|
|
|
|
|
|
@app.route("/admin/book/convert/<int:book_id>", methods=['POST'])
|
|
|
|
@login_required_if_no_ano
|
|
|
|
@edit_required
|
|
|
|
def convert_bookformat(book_id):
|
2018-08-29 04:49:50 +00:00
|
|
|
# check to see if we have form fields to work with - if not send user back
|
|
|
|
book_format_from = request.form.get('book_format_from', None)
|
|
|
|
book_format_to = request.form.get('book_format_to', None)
|
|
|
|
|
|
|
|
if (book_format_from is None) or (book_format_to is None):
|
2018-08-31 13:00:22 +00:00
|
|
|
flash(_(u"Source or destination format for conversion missing"), category="error")
|
2018-08-29 04:49:50 +00:00
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
|
2018-08-29 03:32:30 +00:00
|
|
|
app.logger.debug('converting: book id: ' + str(book_id) +
|
|
|
|
' from: ' + request.form['book_format_from'] +
|
|
|
|
' to: ' + request.form['book_format_to'])
|
2018-08-31 13:00:22 +00:00
|
|
|
rtn = helper.convert_book_format(book_id, config.config_calibre_dir, book_format_from.upper(),
|
|
|
|
book_format_to.upper(), current_user.nickname)
|
2018-08-29 03:32:30 +00:00
|
|
|
|
|
|
|
if rtn is None:
|
|
|
|
flash(_(u"Book successfully queued for converting to %(book_format)s",
|
2018-08-31 13:00:22 +00:00
|
|
|
book_format=book_format_to),
|
2018-08-29 03:32:30 +00:00
|
|
|
category="success")
|
|
|
|
else:
|
2018-08-31 13:00:22 +00:00
|
|
|
flash(_(u"There was an error converting this book: %(res)s", res=rtn), category="error")
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|