2015-08-02 18:59:11 +00:00
|
|
|
# -*- 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 os
|
2020-04-20 16:56:39 +00:00
|
|
|
from datetime import datetime
|
2019-07-13 18:45:48 +00:00
|
|
|
import json
|
|
|
|
import mimetypes
|
2020-12-09 10:04:29 +00:00
|
|
|
import chardet # dependency of requests
|
2021-04-13 17:41:44 +00:00
|
|
|
import copy
|
2022-03-13 09:23:13 +00:00
|
|
|
from functools import wraps
|
2018-11-03 12:43:38 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
from babel.dates import format_date
|
2022-03-13 11:34:21 +00:00
|
|
|
from babel import Locale
|
2020-08-22 20:31:00 +00:00
|
|
|
from flask import Blueprint, jsonify
|
2020-12-12 10:23:17 +00:00
|
|
|
from flask import request, redirect, send_from_directory, make_response, flash, abort, url_for
|
|
|
|
from flask import session as flask_session
|
2019-07-13 18:45:48 +00:00
|
|
|
from flask_babel import gettext as _
|
2020-12-12 10:23:17 +00:00
|
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
2020-05-06 16:47:33 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError, InvalidRequestError, OperationalError
|
2021-03-07 03:15:18 +00:00
|
|
|
from sqlalchemy.sql.expression import text, func, false, not_, and_, or_
|
2020-07-08 19:18:38 +00:00
|
|
|
from sqlalchemy.orm.attributes import flag_modified
|
2020-05-19 19:35:56 +00:00
|
|
|
from sqlalchemy.sql.functions import coalesce
|
2020-08-23 02:44:28 +00:00
|
|
|
|
|
|
|
from .services.worker import WorkerThread
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
from werkzeug.datastructures import Headers
|
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
2018-09-30 07:43:20 +00:00
|
|
|
|
2020-08-23 03:35:48 +00:00
|
|
|
from . import constants, logger, isoLanguages, services
|
2020-12-12 10:23:17 +00:00
|
|
|
from . import babel, db, ub, config, get_locale, app
|
2021-10-10 08:23:58 +00:00
|
|
|
from . import calibre_db, kobo_sync_status
|
2019-07-13 18:45:48 +00:00
|
|
|
from .gdriveutils import getFileFromEbooksFolder, do_gdrive_download
|
2021-04-04 17:40:34 +00:00
|
|
|
from .helper import check_valid_domain, render_task_status, check_email, check_username, \
|
2020-01-25 23:29:17 +00:00
|
|
|
get_cc_columns, get_book_cover, get_download_link, send_mail, generate_random_password, \
|
2022-02-05 14:36:18 +00:00
|
|
|
send_registration_mail, check_send_to_kindle, check_read_formats, tags_filters, reset_password, valid_email, \
|
|
|
|
edit_book_read_status
|
2019-07-13 18:45:48 +00:00
|
|
|
from .pagination import Pagination
|
|
|
|
from .redirect import redirect_back
|
2020-12-12 10:23:17 +00:00
|
|
|
from .usermanagement import login_required_if_no_ano
|
2021-12-01 19:29:05 +00:00
|
|
|
from .kobo_sync_status import remove_synced_book
|
2020-12-12 10:23:17 +00:00
|
|
|
from .render_template import render_title_template
|
2021-12-05 12:04:13 +00:00
|
|
|
from .kobo_sync_status import change_archived_books
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
feature_support = {
|
2020-04-15 17:57:33 +00:00
|
|
|
'ldap': bool(services.ldap),
|
|
|
|
'goodreads': bool(services.goodreads_support),
|
|
|
|
'kobo': bool(services.kobo)
|
|
|
|
}
|
2018-09-30 07:43:20 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-13 18:45:48 +00:00
|
|
|
from .oauth_bb import oauth_check, register_user_with_oauth, logout_oauth_user, get_oauth_status
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
feature_support['oauth'] = True
|
2018-09-30 07:43:20 +00:00
|
|
|
except ImportError:
|
2019-07-13 18:45:48 +00:00
|
|
|
feature_support['oauth'] = False
|
|
|
|
oauth_check = {}
|
2022-03-13 11:34:21 +00:00
|
|
|
register_user_with_oauth = logout_oauth_user = get_oauth_status = None
|
2018-09-30 07:43:20 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from natsort import natsorted as sort
|
|
|
|
except ImportError:
|
2020-04-15 17:57:33 +00:00
|
|
|
sort = sorted # Just use regular sort then, may cause issues with badly named pages in cbz/cbr files
|
2017-03-05 09:40:39 +00:00
|
|
|
|
2018-10-01 08:35:13 +00:00
|
|
|
|
2020-05-08 12:49:12 +00:00
|
|
|
@app.after_request
|
|
|
|
def add_security_headers(resp):
|
2022-03-13 11:34:21 +00:00
|
|
|
csp = "default-src 'self'"
|
|
|
|
csp += ''.join([' ' + host for host in config.config_trustedhosts.strip().split(',')])
|
2022-03-26 18:35:56 +00:00
|
|
|
csp += " 'unsafe-inline' 'unsafe-eval'; font-src 'self' data:; img-src 'self' "
|
|
|
|
if request.path.startswith("/author/") and config.config_use_goodreads:
|
|
|
|
csp += "images.gr-assets.com i.gr-assets.com s.gr-assets.com"
|
|
|
|
csp += " data:"
|
2022-03-13 11:34:21 +00:00
|
|
|
resp.headers['Content-Security-Policy'] = csp
|
2022-03-20 10:21:15 +00:00
|
|
|
if request.endpoint == "edit-book.show_edit_book" or config.config_use_google_drive:
|
2021-10-04 15:50:32 +00:00
|
|
|
resp.headers['Content-Security-Policy'] += " *"
|
2021-11-02 14:55:56 +00:00
|
|
|
elif request.endpoint == "web.read_book":
|
|
|
|
resp.headers['Content-Security-Policy'] += " blob:;style-src-elem 'self' blob: 'unsafe-inline';"
|
2020-05-08 12:49:12 +00:00
|
|
|
resp.headers['X-Content-Type-Options'] = 'nosniff'
|
|
|
|
resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
|
|
|
|
resp.headers['X-XSS-Protection'] = '1; mode=block'
|
2021-10-23 07:45:04 +00:00
|
|
|
resp.headers['Strict-Transport-Security'] = 'max-age=31536000;'
|
2020-05-08 12:49:12 +00:00
|
|
|
return resp
|
2020-04-25 19:12:07 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
web = Blueprint('web', __name__)
|
|
|
|
log = logger.create()
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2020-04-15 17:57:33 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Login logic and rights management ###############################################
|
2017-01-22 15:44:37 +00:00
|
|
|
|
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):
|
2019-03-23 19:29:53 +00:00
|
|
|
if current_user.role_download():
|
2016-04-27 08:35:23 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def viewer_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_viewer():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### data provider functions #########################################################
|
2017-01-12 19:43:36 +00:00
|
|
|
|
2017-01-15 11:37:58 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/ajax/emailstat")
|
2018-07-30 18:12:41 +00:00
|
|
|
@login_required
|
|
|
|
def get_email_status_json():
|
2020-08-23 02:44:28 +00:00
|
|
|
tasks = WorkerThread.getInstance().tasks
|
2020-08-22 20:31:00 +00:00
|
|
|
return jsonify(render_task_status(tasks))
|
2018-07-30 18:12:41 +00:00
|
|
|
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/ajax/bookmark/<int:book_id>/<book_format>", methods=['POST'])
|
2018-08-24 13:48:09 +00:00
|
|
|
@login_required
|
2022-03-13 09:23:13 +00:00
|
|
|
def set_bookmark(book_id, book_format):
|
2019-07-13 18:45:48 +00:00
|
|
|
bookmark_key = request.form["bookmark"]
|
|
|
|
ub.session.query(ub.Bookmark).filter(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:
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2019-07-13 18:45:48 +00:00
|
|
|
return "", 204
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
l_bookmark = ub.Bookmark(user_id=current_user.id,
|
|
|
|
book_id=book_id,
|
|
|
|
format=book_format,
|
|
|
|
bookmark_key=bookmark_key)
|
|
|
|
ub.session.merge(l_bookmark)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("Bookmark for user {} in book {} created".format(current_user.id, book_id))
|
2019-07-13 18:45:48 +00:00
|
|
|
return "", 201
|
2018-08-24 13:48:09 +00:00
|
|
|
|
2018-09-02 09:48:58 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/ajax/toggleread/<int:book_id>", methods=['POST'])
|
2018-08-24 13:48:09 +00:00
|
|
|
@login_required
|
2019-07-13 18:45:48 +00:00
|
|
|
def toggle_read(book_id):
|
2022-02-05 14:36:18 +00:00
|
|
|
message = edit_book_read_status(book_id)
|
|
|
|
if message:
|
|
|
|
return message, 400
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2022-02-05 14:36:18 +00:00
|
|
|
return message
|
|
|
|
|
2018-08-24 13:48:09 +00:00
|
|
|
|
2020-01-25 23:29:17 +00:00
|
|
|
@web.route("/ajax/togglearchived/<int:book_id>", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def toggle_archived(book_id):
|
2022-03-13 11:34:21 +00:00
|
|
|
is_archived = change_archived_books(book_id, message="Book {} archive bit toggled".format(book_id))
|
2021-12-05 12:04:13 +00:00
|
|
|
if is_archived:
|
2021-12-01 19:29:05 +00:00
|
|
|
remove_synced_book(book_id)
|
2020-01-25 23:29:17 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2020-04-13 21:15:44 +00:00
|
|
|
@web.route("/ajax/view", methods=["POST"])
|
2020-09-27 17:12:10 +00:00
|
|
|
@login_required_if_no_ano
|
2020-04-13 21:15:44 +00:00
|
|
|
def update_view():
|
2021-11-01 12:11:49 +00:00
|
|
|
to_save = request.get_json()
|
2020-09-27 10:37:41 +00:00
|
|
|
try:
|
|
|
|
for element in to_save:
|
|
|
|
for param in to_save[element]:
|
2020-09-27 17:12:10 +00:00
|
|
|
current_user.set_view_property(element, param, to_save[element][param])
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.error("Could not save view_settings: %r %r: %e", request, to_save, ex)
|
2020-04-13 21:15:44 +00:00
|
|
|
return "Invalid request", 400
|
2020-08-23 07:13:44 +00:00
|
|
|
return "1", 200
|
2020-04-13 21:15:44 +00:00
|
|
|
|
|
|
|
|
2018-08-31 08:47:58 +00:00
|
|
|
'''
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/ajax/getcomic/<int:book_id>/<book_format>/<int:page>")
|
2017-11-18 09:34:21 +00:00
|
|
|
@login_required
|
|
|
|
def get_comic_book(book_id, book_format, page):
|
2020-05-23 08:16:29 +00:00
|
|
|
book = calibre_db.get_book(book_id)
|
2017-11-18 09:34:21 +00:00
|
|
|
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"):
|
2019-07-13 18:45:48 +00:00
|
|
|
if feature_support['rar'] == True:
|
2017-11-18 09:34:21 +00:00
|
|
|
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
|
2019-07-13 18:45:48 +00:00
|
|
|
log.error('Unrar binary not found, or unable to decompress file %s', cbr_file)
|
2017-11-19 19:37:43 +00:00
|
|
|
return "", 204
|
2017-11-18 09:34:21 +00:00
|
|
|
else:
|
2019-07-13 18:45:48 +00:00
|
|
|
log.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:
|
2019-07-13 18:45:48 +00:00
|
|
|
log.error('unsupported comic format')
|
2018-01-11 13:43:39 +00:00
|
|
|
return "", 204
|
|
|
|
|
2021-10-04 16:26:46 +00:00
|
|
|
b64 = codecs.encode(extract(page), 'base64').decode()
|
2018-01-11 13:43:39 +00:00
|
|
|
ext = names[page].rpartition('.')[-1]
|
2020-12-17 15:53:24 +00:00
|
|
|
if ext not in ('png', 'gif', 'jpg', 'jpeg', 'webp'):
|
2018-01-11 13:43:39 +00:00
|
|
|
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
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Typeahead ##################################################################
|
|
|
|
|
|
|
|
|
2020-04-14 17:27:18 +00:00
|
|
|
@web.route("/get_authors_json", methods=['GET'])
|
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():
|
2020-05-23 08:16:29 +00:00
|
|
|
return calibre_db.get_typeahead(db.Authors, request.args.get('q'), ('|', ','))
|
2016-04-15 21:35:18 +00:00
|
|
|
|
2019-01-08 06:58:22 +00:00
|
|
|
|
2020-04-14 17:27:18 +00:00
|
|
|
@web.route("/get_publishers_json", methods=['GET'])
|
2018-09-30 12:08:55 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_publishers_json():
|
2020-05-23 08:16:29 +00:00
|
|
|
return calibre_db.get_typeahead(db.Publishers, request.args.get('q'), ('|', ','))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2020-04-14 17:27:18 +00:00
|
|
|
@web.route("/get_tags_json", methods=['GET'])
|
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():
|
2020-05-23 08:16:29 +00:00
|
|
|
return calibre_db.get_typeahead(db.Tags, request.args.get('q'), tag_filter=tags_filters())
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2020-04-14 17:27:18 +00:00
|
|
|
@web.route("/get_series_json", methods=['GET'])
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_series_json():
|
2020-05-23 08:16:29 +00:00
|
|
|
return calibre_db.get_typeahead(db.Series, request.args.get('q'))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2020-04-14 17:27:18 +00:00
|
|
|
@web.route("/get_languages_json", methods=['GET'])
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def get_languages_json():
|
2020-04-14 17:27:18 +00:00
|
|
|
query = (request.args.get('q') or '').lower()
|
|
|
|
language_names = isoLanguages.get_language_names(get_locale())
|
|
|
|
entries_start = [s for key, s in language_names.items() if s.lower().startswith(query.lower())]
|
|
|
|
if len(entries_start) < 5:
|
|
|
|
entries = [s for key, s in language_names.items() if query in s.lower()]
|
2020-04-15 17:57:33 +00:00
|
|
|
entries_start.extend(entries[0:(5 - len(entries_start))])
|
2020-04-14 17:27:18 +00:00
|
|
|
entries_start = list(set(entries_start))
|
|
|
|
json_dumps = json.dumps([dict(name=r) for r in entries_start[0:5]])
|
|
|
|
return json_dumps
|
|
|
|
|
|
|
|
|
|
|
|
@web.route("/get_matching_tags", methods=['GET'])
|
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': []}
|
2020-05-27 17:19:17 +00:00
|
|
|
q = calibre_db.session.query(db.Books).filter(calibre_db.common_filters(True))
|
2020-05-23 10:51:48 +00:00
|
|
|
calibre_db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
2020-04-14 17:27:18 +00:00
|
|
|
author_input = request.args.get('author_name') or ''
|
|
|
|
title_input = request.args.get('book_title') or ''
|
|
|
|
include_tag_inputs = request.args.getlist('include_tag') or ''
|
|
|
|
exclude_tag_inputs = request.args.getlist('exclude_tag') or ''
|
|
|
|
q = q.filter(db.Books.authors.any(func.lower(db.Authors.name).ilike("%" + author_input + "%")),
|
|
|
|
func.lower(db.Books.title).ilike("%" + title_input + "%"))
|
|
|
|
if len(include_tag_inputs) > 0:
|
|
|
|
for tag in include_tag_inputs:
|
|
|
|
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
|
|
|
|
if len(exclude_tag_inputs) > 0:
|
|
|
|
for tag in exclude_tag_inputs:
|
|
|
|
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
|
|
|
|
for book in q:
|
|
|
|
for tag in book.tags:
|
|
|
|
if tag.id not in tag_dict['tags']:
|
|
|
|
tag_dict['tags'].append(tag.id)
|
2016-05-02 20:43:50 +00:00
|
|
|
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
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
def generate_char_list(data_colum, db_link):
|
|
|
|
return (calibre_db.session.query(func.upper(func.substr(data_colum, 1, 1)).label('char'))
|
|
|
|
.join(db_link).join(db.Books).filter(calibre_db.common_filters())
|
|
|
|
.group_by(func.upper(func.substr(data_colum, 1, 1))).all())
|
|
|
|
|
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
def get_sort_function(sort_param, data):
|
2021-11-20 10:57:51 +00:00
|
|
|
order = [db.Books.timestamp.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'stored':
|
|
|
|
sort_param = current_user.get_view_property(data, 'stored')
|
2020-07-11 10:09:34 +00:00
|
|
|
else:
|
2022-03-12 15:51:50 +00:00
|
|
|
current_user.set_view_property(data, 'stored', sort_param)
|
|
|
|
if sort_param == 'pubnew':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.pubdate.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'pubold':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.pubdate]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'abc':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.sort]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'zyx':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.sort.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'new':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.timestamp.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'old':
|
2019-07-13 18:45:48 +00:00
|
|
|
order = [db.Books.timestamp]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'authaz':
|
2021-07-26 05:52:01 +00:00
|
|
|
order = [db.Books.author_sort.asc(), db.Series.name, db.Books.series_index]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'authza':
|
2021-07-26 05:52:01 +00:00
|
|
|
order = [db.Books.author_sort.desc(), db.Series.name.desc(), db.Books.series_index.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'seriesasc':
|
2020-12-01 13:51:25 +00:00
|
|
|
order = [db.Books.series_index.asc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'seriesdesc':
|
2020-12-01 13:51:25 +00:00
|
|
|
order = [db.Books.series_index.desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'hotdesc':
|
2021-11-04 16:50:48 +00:00
|
|
|
order = [func.count(ub.Downloads.book_id).desc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == 'hotasc':
|
2021-11-04 16:50:48 +00:00
|
|
|
order = [func.count(ub.Downloads.book_id).asc()]
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param is None:
|
|
|
|
sort_param = "new"
|
|
|
|
return order, sort_param
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
def render_books_list(data, sort_param, book_id, page):
|
|
|
|
order = get_sort_function(sort_param, data)
|
2019-07-13 18:45:48 +00:00
|
|
|
if data == "rated":
|
2021-03-14 14:06:09 +00:00
|
|
|
return render_rated_books(page, book_id, order=order)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif data == "discover":
|
2022-03-26 18:35:56 +00:00
|
|
|
return render_discover_books(book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif data == "unread":
|
|
|
|
return render_read_books(page, False, order=order)
|
|
|
|
elif data == "read":
|
|
|
|
return render_read_books(page, True, order=order)
|
|
|
|
elif data == "hot":
|
2021-11-04 16:50:48 +00:00
|
|
|
return render_hot_books(page, order)
|
2020-09-27 14:00:17 +00:00
|
|
|
elif data == "download":
|
2021-03-21 17:55:02 +00:00
|
|
|
return render_downloaded_books(page, order, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif data == "author":
|
|
|
|
return render_author_books(page, book_id, order)
|
|
|
|
elif data == "publisher":
|
|
|
|
return render_publisher_books(page, book_id, order)
|
|
|
|
elif data == "series":
|
|
|
|
return render_series_books(page, book_id, order)
|
|
|
|
elif data == "ratings":
|
|
|
|
return render_ratings_books(page, book_id, order)
|
|
|
|
elif data == "formats":
|
|
|
|
return render_formats_books(page, book_id, order)
|
|
|
|
elif data == "category":
|
|
|
|
return render_category_books(page, book_id, order)
|
2019-07-22 19:38:52 +00:00
|
|
|
elif data == "language":
|
|
|
|
return render_language_books(page, book_id, order)
|
2020-01-25 23:29:17 +00:00
|
|
|
elif data == "archived":
|
2020-12-23 08:07:49 +00:00
|
|
|
return render_archived_books(page, order)
|
2020-07-11 10:09:34 +00:00
|
|
|
elif data == "search":
|
|
|
|
term = (request.args.get('query') or '')
|
|
|
|
offset = int(int(config.config_books_per_page) * (page - 1))
|
2020-10-04 17:23:06 +00:00
|
|
|
return render_search_results(term, offset, order, config.config_books_per_page)
|
|
|
|
elif data == "advsearch":
|
|
|
|
term = json.loads(flask_session['query'])
|
|
|
|
offset = int(int(config.config_books_per_page) * (page - 1))
|
|
|
|
return render_adv_search_results(term, offset, order, config.config_books_per_page)
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
2020-07-11 10:09:34 +00:00
|
|
|
website = data or "newest"
|
2021-11-04 16:50:48 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0, db.Books, True, order[0],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2021-07-26 05:52:01 +00:00
|
|
|
db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book,
|
|
|
|
db.Series)
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Books"), page=website, order=order[1])
|
2017-07-06 02:09:01 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2021-03-14 14:06:09 +00:00
|
|
|
def render_rated_books(page, book_id, order):
|
|
|
|
if current_user.check_visibility(constants.SIDEBAR_BEST_RATED):
|
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
|
|
|
db.Books,
|
|
|
|
db.Books.ratings.any(db.Ratings.rating > 9),
|
2021-11-04 16:50:48 +00:00
|
|
|
order[0],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2021-07-30 14:58:20 +00:00
|
|
|
db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book,
|
|
|
|
db.Series)
|
|
|
|
|
2021-03-14 14:06:09 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2021-11-04 16:50:48 +00:00
|
|
|
id=book_id, title=_(u"Top Rated Books"), page="rated", order=order[1])
|
2021-03-14 14:06:09 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2022-03-26 18:35:56 +00:00
|
|
|
def render_discover_books(book_id):
|
2021-03-14 14:06:09 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_RANDOM):
|
2022-03-26 18:35:56 +00:00
|
|
|
entries, __, ___ = calibre_db.fill_indexpage(1, 0, db.Books, True, [func.randomblob(2)],
|
|
|
|
join_archive_read=True,
|
|
|
|
config_read_column=config.config_read_column)
|
2021-03-14 14:06:09 +00:00
|
|
|
pagination = Pagination(1, config.config_books_per_page, config.config_books_per_page)
|
2022-03-26 18:35:56 +00:00
|
|
|
return render_title_template('index.html', random=false(), entries=entries, pagination=pagination, id=book_id,
|
2021-03-14 14:06:09 +00:00
|
|
|
title=_(u"Discover (Random Books)"), page="discover")
|
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2021-11-04 16:50:48 +00:00
|
|
|
def render_hot_books(page, order):
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_HOT):
|
2021-11-04 16:50:48 +00:00
|
|
|
if order[1] not in ['hotasc', 'hotdesc']:
|
2022-03-13 11:34:21 +00:00
|
|
|
# Unary expression comparsion only working (for this expression) in sqlalchemy 1.4+
|
|
|
|
# if not (order[0][0].compare(func.count(ub.Downloads.book_id).desc()) or
|
|
|
|
# order[0][0].compare(func.count(ub.Downloads.book_id).asc())):
|
2021-11-04 16:50:48 +00:00
|
|
|
order = [func.count(ub.Downloads.book_id).desc()], 'hotdesc'
|
2017-11-12 13:06:33 +00:00
|
|
|
if current_user.show_detail_random():
|
2022-03-26 18:35:56 +00:00
|
|
|
random_query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
random = (random_query.filter(calibre_db.common_filters())
|
|
|
|
.order_by(func.random())
|
|
|
|
.limit(config.config_random_books).all())
|
2017-03-07 18:10:17 +00:00
|
|
|
else:
|
2018-08-28 07:42:19 +00:00
|
|
|
random = false()
|
2022-03-26 18:35:56 +00:00
|
|
|
|
2017-11-12 13:06:33 +00:00
|
|
|
off = int(int(config.config_books_per_page) * (page - 1))
|
2022-03-13 11:34:21 +00:00
|
|
|
all_books = ub.session.query(ub.Downloads, func.count(ub.Downloads.book_id)) \
|
2021-11-04 16:50:48 +00:00
|
|
|
.order_by(*order[0]).group_by(ub.Downloads.book_id)
|
2017-11-12 13:06:33 +00:00
|
|
|
hot_books = all_books.offset(off).limit(config.config_books_per_page)
|
|
|
|
entries = list()
|
|
|
|
for book in hot_books:
|
2022-03-26 18:35:56 +00:00
|
|
|
query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
download_book = query.filter(calibre_db.common_filters()).filter(
|
|
|
|
book.Downloads.book_id == db.Books.id).first()
|
2022-03-13 11:34:21 +00:00
|
|
|
if download_book:
|
|
|
|
entries.append(download_book)
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.delete_download(book.Downloads.book_id)
|
2022-03-13 11:34:21 +00:00
|
|
|
num_books = entries.__len__()
|
|
|
|
pagination = Pagination(page, config.config_books_per_page, num_books)
|
2017-11-12 13:06:33 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Hot Books (Most Downloaded)"), page="hot", order=order[1])
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
2019-07-13 18:45:48 +00:00
|
|
|
abort(404)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2021-03-21 17:55:02 +00:00
|
|
|
def render_downloaded_books(page, order, user_id):
|
2021-03-21 18:31:32 +00:00
|
|
|
if current_user.role_admin():
|
|
|
|
user_id = int(user_id)
|
|
|
|
else:
|
|
|
|
user_id = current_user.id
|
2020-09-27 14:00:17 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_DOWNLOAD):
|
2022-03-26 18:35:56 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page,
|
2020-09-27 14:00:17 +00:00
|
|
|
0,
|
|
|
|
db.Books,
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.Downloads.user_id == user_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
order[0],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2021-11-04 16:50:48 +00:00
|
|
|
db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book,
|
|
|
|
db.Series,
|
2020-09-27 14:00:17 +00:00
|
|
|
ub.Downloads, db.Books.id == ub.Downloads.book_id)
|
|
|
|
for book in entries:
|
2022-03-26 18:35:56 +00:00
|
|
|
if not (calibre_db.session.query(db.Books).filter(calibre_db.common_filters())
|
|
|
|
.filter(db.Books.id == book.Books.id).first()):
|
|
|
|
ub.delete_download(book.Books.id)
|
2021-03-21 17:55:02 +00:00
|
|
|
user = ub.session.query(ub.User).filter(ub.User.id == user_id).first()
|
2020-09-27 14:00:17 +00:00
|
|
|
return render_title_template('index.html',
|
|
|
|
random=random,
|
|
|
|
entries=entries,
|
|
|
|
pagination=pagination,
|
2021-03-21 18:31:32 +00:00
|
|
|
id=user_id,
|
2022-03-13 11:34:21 +00:00
|
|
|
title=_(u"Downloaded books by %(user)s", user=user.name),
|
2021-11-04 16:50:48 +00:00
|
|
|
page="download",
|
|
|
|
order=order[1])
|
2020-09-27 14:00:17 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def render_author_books(page, author_id, order):
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.authors.any(db.Authors.id == author_id),
|
2021-11-04 16:50:48 +00:00
|
|
|
[order[0][0], db.Series.name, db.Books.series_index],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.books_series_link,
|
2022-03-26 18:35:56 +00:00
|
|
|
db.books_series_link.c.book == db.Books.id,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Series)
|
2019-07-13 18:45:48 +00:00
|
|
|
if entries is None or not len(entries):
|
2020-04-15 17:57:33 +00:00
|
|
|
flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"),
|
|
|
|
category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for("web.index"))
|
2021-07-26 05:52:01 +00:00
|
|
|
if constants.sqlalchemy_version2:
|
|
|
|
author = calibre_db.session.get(db.Authors, author_id)
|
|
|
|
else:
|
|
|
|
author = calibre_db.session.query(db.Authors).get(author_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
author_name = author.name.replace('|', ',')
|
|
|
|
|
|
|
|
author_info = None
|
|
|
|
other_books = []
|
2019-08-20 17:12:40 +00:00
|
|
|
if services.goodreads_support and config.config_use_goodreads:
|
|
|
|
author_info = services.goodreads_support.get_author_info(author_name)
|
2022-03-26 18:35:56 +00:00
|
|
|
book_entries = [entry.Books for entry in entries]
|
|
|
|
other_books = services.goodreads_support.get_other_books(author_info, book_entries)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('author.html', entries=entries, pagination=pagination, id=author_id,
|
2019-07-22 19:38:52 +00:00
|
|
|
title=_(u"Author: %(name)s", name=author_name), author=author_info,
|
2021-11-04 16:50:48 +00:00
|
|
|
other_books=other_books, page="author", order=order[1])
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_publisher_books(page, book_id, order):
|
2020-05-21 16:16:11 +00:00
|
|
|
publisher = calibre_db.session.query(db.Publishers).filter(db.Publishers.id == book_id).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if publisher:
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.publishers.any(db.Publishers.id == book_id),
|
2021-11-04 16:50:48 +00:00
|
|
|
[db.Series.name, order[0][0], db.Books.series_index],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.books_series_link,
|
2021-03-22 15:04:53 +00:00
|
|
|
db.Books.id == db.books_series_link.c.book,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Series)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=book_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Publisher: %(name)s", name=publisher.name),
|
|
|
|
page="publisher",
|
|
|
|
order=order[1])
|
2018-10-28 13:40:31 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2017-02-04 13:28:18 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def render_series_books(page, book_id, order):
|
2020-05-21 16:16:11 +00:00
|
|
|
name = calibre_db.session.query(db.Series).filter(db.Series.id == book_id).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if name:
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.series.any(db.Series.id == book_id),
|
2022-03-26 18:35:56 +00:00
|
|
|
[order[0][0]],
|
|
|
|
True, config.config_read_column)
|
2019-07-22 19:38:52 +00:00
|
|
|
return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Series: %(serie)s", serie=name.name), page="series", order=order[1])
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
def render_ratings_books(page, book_id, order):
|
2020-05-21 16:16:11 +00:00
|
|
|
name = calibre_db.session.query(db.Ratings).filter(db.Ratings.id == book_id).first()
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.ratings.any(db.Ratings.id == book_id),
|
2022-03-26 18:35:56 +00:00
|
|
|
[order[0][0]],
|
|
|
|
True, config.config_read_column)
|
2019-07-22 19:38:52 +00:00
|
|
|
if name and name.rating <= 10:
|
|
|
|
return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Rating: %(rating)s stars", rating=int(name.rating / 2)),
|
|
|
|
page="ratings",
|
|
|
|
order=order[1])
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
def render_formats_books(page, book_id, order):
|
2020-05-21 16:16:11 +00:00
|
|
|
name = calibre_db.session.query(db.Data).filter(db.Data.format == book_id.upper()).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if name:
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.data.any(db.Data.format == book_id.upper()),
|
2022-03-26 18:35:56 +00:00
|
|
|
[order[0][0]],
|
|
|
|
True, config.config_read_column)
|
2019-07-22 19:38:52 +00:00
|
|
|
return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"File format: %(format)s", format=name.format),
|
|
|
|
page="formats",
|
|
|
|
order=order[1])
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
def render_category_books(page, book_id, order):
|
2020-05-21 16:16:11 +00:00
|
|
|
name = calibre_db.session.query(db.Tags).filter(db.Tags.id == book_id).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if name:
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.tags.any(db.Tags.id == book_id),
|
2021-11-04 16:50:48 +00:00
|
|
|
[order[0][0], db.Series.name, db.Books.series_index],
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column,
|
2021-03-22 15:04:53 +00:00
|
|
|
db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book,
|
|
|
|
db.Series)
|
2019-07-22 19:38:52 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=book_id,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Category: %(name)s", name=name.name), page="category", order=order[1])
|
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
|
|
|
|
2019-07-22 19:38:52 +00:00
|
|
|
def render_language_books(page, name, order):
|
|
|
|
try:
|
2021-09-29 17:00:02 +00:00
|
|
|
lang_name = isoLanguages.get_language_name(get_locale(), name)
|
|
|
|
except KeyError:
|
|
|
|
abort(404)
|
|
|
|
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.languages.any(db.Languages.lang_code == name),
|
2022-03-26 18:35:56 +00:00
|
|
|
[order[0][0]],
|
|
|
|
True, config.config_read_column)
|
2019-07-22 19:38:52 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=name,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Language: %(name)s", name=lang_name), page="language", order=order[1])
|
2019-07-22 19:38:52 +00:00
|
|
|
|
2020-12-13 12:54:09 +00:00
|
|
|
|
|
|
|
def render_read_books(page, are_read, as_xml=False, order=None):
|
2022-03-12 15:51:50 +00:00
|
|
|
sort_param = order[0] if order else []
|
2020-07-11 10:09:34 +00:00
|
|
|
if not config.config_read_column:
|
|
|
|
if are_read:
|
|
|
|
db_filter = and_(ub.ReadBook.user_id == int(current_user.id),
|
|
|
|
ub.ReadBook.read_status == ub.ReadBook.STATUS_FINISHED)
|
|
|
|
else:
|
2021-01-02 06:51:48 +00:00
|
|
|
db_filter = coalesce(ub.ReadBook.read_status, 0) != ub.ReadBook.STATUS_FINISHED
|
2020-07-11 10:09:34 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if are_read:
|
|
|
|
db_filter = db.cc_classes[config.config_read_column].value == True
|
|
|
|
else:
|
|
|
|
db_filter = coalesce(db.cc_classes[config.config_read_column].value, False) != True
|
2022-03-20 10:21:15 +00:00
|
|
|
except (KeyError, AttributeError, IndexError):
|
|
|
|
log.error("Custom Column No.{} is not existing in calibre database".format(config.config_read_column))
|
2020-07-11 10:09:34 +00:00
|
|
|
if not as_xml:
|
|
|
|
flash(_("Custom Column No.%(column)d is not existing in calibre database",
|
|
|
|
column=config.config_read_column),
|
|
|
|
category="error")
|
|
|
|
return redirect(url_for("web.index"))
|
2022-03-13 11:34:21 +00:00
|
|
|
return [] # ToDo: Handle error Case for opds
|
2022-03-13 09:23:13 +00:00
|
|
|
|
2022-03-26 18:35:56 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage(page, 0,
|
|
|
|
db.Books,
|
|
|
|
db_filter,
|
|
|
|
sort_param,
|
|
|
|
True, config.config_read_column,
|
|
|
|
db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book,
|
|
|
|
db.Series)
|
|
|
|
|
2020-07-11 10:09:34 +00:00
|
|
|
if as_xml:
|
|
|
|
return entries, pagination
|
|
|
|
else:
|
|
|
|
if are_read:
|
|
|
|
name = _(u'Read Books') + ' (' + str(pagination.total_count) + ')'
|
2022-03-13 11:34:21 +00:00
|
|
|
page_name = "read"
|
2020-07-11 10:09:34 +00:00
|
|
|
else:
|
|
|
|
name = _(u'Unread Books') + ' (' + str(pagination.total_count) + ')'
|
2022-03-13 11:34:21 +00:00
|
|
|
page_name = "unread"
|
2020-07-11 10:09:34 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2022-03-13 11:34:21 +00:00
|
|
|
title=name, page=page_name, order=order[1])
|
2020-07-11 10:09:34 +00:00
|
|
|
|
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
def render_archived_books(page, sort_param):
|
|
|
|
order = sort_param[0] or []
|
2022-03-13 11:34:21 +00:00
|
|
|
archived_books = (ub.session.query(ub.ArchivedBook)
|
|
|
|
.filter(ub.ArchivedBook.user_id == int(current_user.id))
|
|
|
|
.filter(ub.ArchivedBook.is_archived == True)
|
|
|
|
.all())
|
2020-07-11 10:09:34 +00:00
|
|
|
archived_book_ids = [archived_book.book_id for archived_book in archived_books]
|
|
|
|
|
|
|
|
archived_filter = db.Books.id.in_(archived_book_ids)
|
|
|
|
|
2021-10-24 19:22:08 +00:00
|
|
|
entries, random, pagination = calibre_db.fill_indexpage_with_archived_books(page, db.Books,
|
|
|
|
0,
|
2020-07-11 10:09:34 +00:00
|
|
|
archived_filter,
|
|
|
|
order,
|
2021-10-24 08:57:29 +00:00
|
|
|
True,
|
2022-03-26 18:35:56 +00:00
|
|
|
True, config.config_read_column)
|
2020-07-11 10:09:34 +00:00
|
|
|
|
|
|
|
name = _(u'Archived Books') + ' (' + str(len(archived_book_ids)) + ')'
|
2022-03-13 11:34:21 +00:00
|
|
|
page_name = "archived"
|
2020-07-11 10:09:34 +00:00
|
|
|
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
2022-03-13 11:34:21 +00:00
|
|
|
title=name, page=page_name, order=sort_param[1])
|
2020-07-11 10:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_prepare_search_form(cc):
|
|
|
|
# prepare data for search-form
|
2022-03-13 11:34:21 +00:00
|
|
|
tags = calibre_db.session.query(db.Tags) \
|
|
|
|
.join(db.books_tags_link) \
|
|
|
|
.join(db.Books) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.filter(calibre_db.common_filters()) \
|
2022-03-13 11:34:21 +00:00
|
|
|
.group_by(text('books_tags_link.tag')) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.order_by(db.Tags.name).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
series = calibre_db.session.query(db.Series) \
|
|
|
|
.join(db.books_series_link) \
|
|
|
|
.join(db.Books) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.filter(calibre_db.common_filters()) \
|
2022-03-13 11:34:21 +00:00
|
|
|
.group_by(text('books_series_link.series')) \
|
|
|
|
.order_by(db.Series.name) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.filter(calibre_db.common_filters()).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
shelves = ub.session.query(ub.Shelf) \
|
|
|
|
.filter(or_(ub.Shelf.is_public == 1, ub.Shelf.user_id == int(current_user.id))) \
|
2021-03-07 02:54:39 +00:00
|
|
|
.order_by(ub.Shelf.name).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
extensions = calibre_db.session.query(db.Data) \
|
|
|
|
.join(db.Books) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.filter(calibre_db.common_filters()) \
|
2022-03-13 11:34:21 +00:00
|
|
|
.group_by(db.Data.format) \
|
2020-07-11 10:09:34 +00:00
|
|
|
.order_by(db.Data.format).all()
|
|
|
|
if current_user.filter_language() == u"all":
|
|
|
|
languages = calibre_db.speaking_language()
|
|
|
|
else:
|
|
|
|
languages = None
|
|
|
|
return render_title_template('search_form.html', tags=tags, languages=languages, extensions=extensions,
|
2022-03-13 11:34:21 +00:00
|
|
|
series=series, shelves=shelves, title=_(u"Advanced Search"), cc=cc, page="advsearch")
|
2020-07-11 10:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_search_results(term, offset=None, order=None, limit=None):
|
2022-03-26 18:35:56 +00:00
|
|
|
join = db.books_series_link, db.books_series_link.c.book == db.Books.id, db.Series
|
2021-11-13 13:57:01 +00:00
|
|
|
entries, result_count, pagination = calibre_db.get_search_results(term,
|
|
|
|
offset,
|
|
|
|
order,
|
|
|
|
limit,
|
|
|
|
config.config_read_column,
|
|
|
|
*join)
|
2020-07-11 10:09:34 +00:00
|
|
|
return render_title_template('search.html',
|
|
|
|
searchterm=term,
|
2020-10-04 17:23:06 +00:00
|
|
|
pagination=pagination,
|
2020-07-11 10:09:34 +00:00
|
|
|
query=term,
|
|
|
|
adv_searchterm=term,
|
|
|
|
entries=entries,
|
|
|
|
result_count=result_count,
|
|
|
|
title=_(u"Search"),
|
2021-11-04 16:50:48 +00:00
|
|
|
page="search",
|
|
|
|
order=order[1])
|
2020-07-11 10:09:34 +00:00
|
|
|
|
2020-10-04 17:23:06 +00:00
|
|
|
|
2020-07-11 10:09:34 +00:00
|
|
|
# ################################### View Books list ##################################################################
|
|
|
|
|
|
|
|
|
|
|
|
@web.route("/", defaults={'page': 1})
|
|
|
|
@web.route('/page/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def index(page):
|
|
|
|
sort_param = (request.args.get('sort') or 'stored').lower()
|
|
|
|
return render_books_list("newest", sort_param, 1, page)
|
|
|
|
|
|
|
|
|
2021-02-09 17:04:49 +00:00
|
|
|
@web.route('/<data>/<sort_param>', defaults={'page': 1, 'book_id': 1})
|
|
|
|
@web.route('/<data>/<sort_param>/', defaults={'page': 1, 'book_id': 1})
|
2020-07-11 10:09:34 +00:00
|
|
|
@web.route('/<data>/<sort_param>/<book_id>', defaults={'page': 1})
|
|
|
|
@web.route('/<data>/<sort_param>/<book_id>/<int:page>')
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def books_list(data, sort_param, book_id, page):
|
|
|
|
return render_books_list(data, sort_param, book_id, page)
|
|
|
|
|
2019-07-22 19:38:52 +00:00
|
|
|
|
2020-05-09 08:58:59 +00:00
|
|
|
@web.route("/table")
|
2020-06-08 15:34:03 +00:00
|
|
|
@login_required
|
2020-05-09 08:58:59 +00:00
|
|
|
def books_table():
|
2020-07-08 19:18:38 +00:00
|
|
|
visibility = current_user.view_settings.get('table', {})
|
2021-10-16 18:46:16 +00:00
|
|
|
cc = get_cc_columns(filter_config_custom_read=True)
|
|
|
|
return render_title_template('book_table.html', title=_(u"Books List"), cc=cc, page="book_table",
|
2020-06-12 14:15:54 +00:00
|
|
|
visiblility=visibility)
|
2020-06-05 18:13:39 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2020-06-05 18:13:39 +00:00
|
|
|
@web.route("/ajax/listbooks")
|
2020-06-08 15:34:03 +00:00
|
|
|
@login_required
|
2020-06-05 18:13:39 +00:00
|
|
|
def list_books():
|
2021-04-11 17:59:20 +00:00
|
|
|
off = int(request.args.get("offset") or 0)
|
|
|
|
limit = int(request.args.get("limit") or config.config_books_per_page)
|
2022-03-13 11:34:21 +00:00
|
|
|
search_param = request.args.get("search")
|
2022-03-12 15:51:50 +00:00
|
|
|
sort_param = request.args.get("sort", "id")
|
2021-04-12 16:39:09 +00:00
|
|
|
order = request.args.get("order", "").lower()
|
2021-04-11 17:59:20 +00:00
|
|
|
state = None
|
2021-10-24 19:22:08 +00:00
|
|
|
join = tuple()
|
2021-04-12 16:39:09 +00:00
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
if sort_param == "state":
|
2021-04-12 17:04:27 +00:00
|
|
|
state = json.loads(request.args.get("state", "[]"))
|
2022-03-12 15:51:50 +00:00
|
|
|
elif sort_param == "tags":
|
2021-04-17 08:27:30 +00:00
|
|
|
order = [db.Tags.name.asc()] if order == "asc" else [db.Tags.name.desc()]
|
2021-10-24 08:57:29 +00:00
|
|
|
join = db.books_tags_link, db.Books.id == db.books_tags_link.c.book, db.Tags
|
2022-03-12 15:51:50 +00:00
|
|
|
elif sort_param == "series":
|
2021-04-17 08:27:30 +00:00
|
|
|
order = [db.Series.name.asc()] if order == "asc" else [db.Series.name.desc()]
|
2021-10-24 08:57:29 +00:00
|
|
|
join = db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series
|
2022-03-12 15:51:50 +00:00
|
|
|
elif sort_param == "publishers":
|
2021-04-17 08:27:30 +00:00
|
|
|
order = [db.Publishers.name.asc()] if order == "asc" else [db.Publishers.name.desc()]
|
2021-10-24 08:57:29 +00:00
|
|
|
join = db.books_publishers_link, db.Books.id == db.books_publishers_link.c.book, db.Publishers
|
2022-03-12 15:51:50 +00:00
|
|
|
elif sort_param == "authors":
|
2021-07-26 05:52:01 +00:00
|
|
|
order = [db.Authors.name.asc(), db.Series.name, db.Books.series_index] if order == "asc" \
|
|
|
|
else [db.Authors.name.desc(), db.Series.name.desc(), db.Books.series_index.desc()]
|
2022-03-13 11:34:21 +00:00
|
|
|
join = db.books_authors_link, db.Books.id == db.books_authors_link.c.book, db.Authors, db.books_series_link, \
|
|
|
|
db.Books.id == db.books_series_link.c.book, db.Series
|
2022-03-12 15:51:50 +00:00
|
|
|
elif sort_param == "languages":
|
2021-04-17 08:27:30 +00:00
|
|
|
order = [db.Languages.lang_code.asc()] if order == "asc" else [db.Languages.lang_code.desc()]
|
2021-07-30 14:58:20 +00:00
|
|
|
join = db.books_languages_link, db.Books.id == db.books_languages_link.c.book, db.Languages
|
2022-03-12 15:51:50 +00:00
|
|
|
elif order and sort_param in ["sort", "title", "authors_sort", "series_index"]:
|
|
|
|
order = [text(sort_param + " " + order)]
|
2021-04-12 16:39:09 +00:00
|
|
|
elif not state:
|
2021-04-08 17:37:08 +00:00
|
|
|
order = [db.Books.timestamp.desc()]
|
2021-04-11 17:59:20 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
total_count = filtered_count = calibre_db.session.query(db.Books).filter(
|
|
|
|
calibre_db.common_filters(allow_show_archived=True)).count()
|
2021-10-16 09:28:35 +00:00
|
|
|
if state is not None:
|
2022-03-13 11:34:21 +00:00
|
|
|
if search_param:
|
|
|
|
books = calibre_db.search_query(search_param, config.config_read_column).all()
|
2021-04-11 17:59:20 +00:00
|
|
|
filtered_count = len(books)
|
|
|
|
else:
|
2022-03-26 18:35:56 +00:00
|
|
|
query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
books = query.filter(calibre_db.common_filters(allow_show_archived=True)).all()
|
2021-10-24 08:57:29 +00:00
|
|
|
entries = calibre_db.get_checkbox_sorted(books, state, off, limit, order, True)
|
2022-03-13 11:34:21 +00:00
|
|
|
elif search_param:
|
|
|
|
entries, filtered_count, __ = calibre_db.get_search_results(search_param,
|
2021-10-24 19:22:08 +00:00
|
|
|
off,
|
2022-03-13 11:34:21 +00:00
|
|
|
[order, ''],
|
2021-10-24 19:22:08 +00:00
|
|
|
limit,
|
|
|
|
config.config_read_column,
|
|
|
|
*join)
|
2020-06-06 19:21:10 +00:00
|
|
|
else:
|
2021-12-05 12:04:13 +00:00
|
|
|
entries, __, __ = calibre_db.fill_indexpage_with_archived_books((int(off) / (int(limit)) + 1),
|
|
|
|
db.Books,
|
|
|
|
limit,
|
|
|
|
True,
|
|
|
|
order,
|
|
|
|
True,
|
|
|
|
True,
|
|
|
|
config.config_read_column,
|
|
|
|
*join)
|
2021-10-24 08:57:29 +00:00
|
|
|
|
|
|
|
result = list()
|
2020-06-11 19:19:09 +00:00
|
|
|
for entry in entries:
|
2021-10-24 08:57:29 +00:00
|
|
|
val = entry[0]
|
2022-03-26 18:35:56 +00:00
|
|
|
val.is_archived = entry[1] is True
|
|
|
|
val.read_status = entry[2] == ub.ReadBook.STATUS_FINISHED
|
2022-03-13 11:34:21 +00:00
|
|
|
for lang_index in range(0, len(val.languages)):
|
|
|
|
val.languages[lang_index].language_name = isoLanguages.get_language_name(get_locale(), val.languages[
|
|
|
|
lang_index].lang_code)
|
2021-10-24 08:57:29 +00:00
|
|
|
result.append(val)
|
|
|
|
|
|
|
|
table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": result}
|
2020-06-06 19:21:10 +00:00
|
|
|
js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)
|
|
|
|
|
2020-06-05 18:13:39 +00:00
|
|
|
response = make_response(js_list)
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2020-06-12 14:15:54 +00:00
|
|
|
@web.route("/ajax/table_settings", methods=['POST'])
|
2020-06-12 11:45:07 +00:00
|
|
|
@login_required
|
|
|
|
def update_table_settings():
|
2020-07-08 19:18:38 +00:00
|
|
|
current_user.view_settings['table'] = json.loads(request.data)
|
|
|
|
try:
|
2020-08-24 19:00:13 +00:00
|
|
|
try:
|
|
|
|
flag_modified(current_user, "view_settings")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2020-07-08 19:18:38 +00:00
|
|
|
ub.session.commit()
|
2020-12-07 18:53:34 +00:00
|
|
|
except (InvalidRequestError, OperationalError):
|
2020-07-08 19:18:38 +00:00
|
|
|
log.error("Invalid request received: %r ", request, )
|
|
|
|
return "Invalid request", 400
|
2020-06-12 14:15:54 +00:00
|
|
|
return ""
|
2020-05-09 08:58:59 +00:00
|
|
|
|
2020-12-07 18:53:34 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/author")
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def author_list():
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_AUTHOR):
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('author', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Authors.sort.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Authors.sort.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Authors, func.count('books_authors_link.book').label('count')) \
|
2020-05-23 08:16:29 +00:00
|
|
|
.join(db.books_authors_link).join(db.Books).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(text('books_authors_link.author')).order_by(order).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = generate_char_list(db.Authors.sort, db.books_authors_link)
|
2021-04-13 17:47:55 +00:00
|
|
|
# If not creating a copy, readonly databases can not display authornames with "|" in it as changing the name
|
|
|
|
# starts a change session
|
2022-03-13 11:34:21 +00:00
|
|
|
author_copy = copy.deepcopy(entries)
|
|
|
|
for entry in author_copy:
|
2017-11-30 15:49:46 +00:00
|
|
|
entry.Authors.name = entry.Authors.name.replace('|', ',')
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_title_template('list.html', entries=author_copy, folder='web.books_list', charlist=char_list,
|
2021-03-21 18:31:32 +00:00
|
|
|
title=u"Authors", page="authorlist", data='author', order=order_no)
|
2017-11-12 13:06:33 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2021-03-21 17:55:02 +00:00
|
|
|
@web.route("/downloadlist")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def download_list():
|
|
|
|
if current_user.get_view_property('download', 'dir') == 'desc':
|
2021-03-21 18:31:32 +00:00
|
|
|
order = ub.User.name.desc()
|
|
|
|
order_no = 0
|
2021-03-21 17:55:02 +00:00
|
|
|
else:
|
2021-03-21 18:31:32 +00:00
|
|
|
order = ub.User.name.asc()
|
|
|
|
order_no = 1
|
2021-03-21 17:55:02 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_DOWNLOAD) and current_user.role_admin():
|
2022-03-13 11:34:21 +00:00
|
|
|
entries = ub.session.query(ub.User, func.count(ub.Downloads.book_id).label('count')) \
|
2021-03-21 17:55:02 +00:00
|
|
|
.join(ub.Downloads).group_by(ub.Downloads.user_id).order_by(order).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = ub.session.query(func.upper(func.substr(ub.User.name, 1, 1)).label('char')) \
|
2021-03-21 17:55:02 +00:00
|
|
|
.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS) \
|
|
|
|
.group_by(func.upper(func.substr(ub.User.name, 1, 1))).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=char_list,
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"Downloads"), page="downloadlist", data="download", order=order_no)
|
2021-03-21 17:55:02 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/publisher")
|
2018-09-30 16:30:24 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def publisher_list():
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('publisher', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Publishers.name.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Publishers.name.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_PUBLISHER):
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Publishers, func.count('books_publishers_link.book').label('count')) \
|
2020-05-23 08:16:29 +00:00
|
|
|
.join(db.books_publishers_link).join(db.Books).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(text('books_publishers_link.publisher')).order_by(order).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = generate_char_list(db.Publishers.name, db.books_publishers_link)
|
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=char_list,
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"Publishers"), page="publisherlist", data="publisher", order=order_no)
|
2018-09-30 16:30:24 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/series")
|
2018-09-30 16:30:24 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def series_list():
|
|
|
|
if current_user.check_visibility(constants.SIDEBAR_SERIES):
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('series', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Series.sort.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Series.sort.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = generate_char_list(db.Series.sort, db.books_series_link)
|
2020-09-27 10:37:41 +00:00
|
|
|
if current_user.get_view_property('series', 'series_view') == 'list':
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Series, func.count('books_series_link.book').label('count')) \
|
2020-05-23 08:16:29 +00:00
|
|
|
.join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(text('books_series_link.series')).order_by(order).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=char_list,
|
2021-11-01 12:11:49 +00:00
|
|
|
title=_(u"Series"), page="serieslist", data="series", order=order_no)
|
2020-04-13 21:15:44 +00:00
|
|
|
else:
|
2021-11-04 13:32:31 +00:00
|
|
|
entries = calibre_db.session.query(db.Books, func.count('books_series_link').label('count'),
|
|
|
|
func.max(db.Books.series_index), db.Books.id) \
|
2022-03-13 11:34:21 +00:00
|
|
|
.join(db.books_series_link).join(db.Series).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(text('books_series_link.series')).order_by(order).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_title_template('grid.html', entries=entries, folder='web.books_list', charlist=char_list,
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"Series"), page="serieslist", data="series", bodyClass="grid-view",
|
|
|
|
order=order_no)
|
2018-10-28 13:40:31 +00:00
|
|
|
else:
|
|
|
|
abort(404)
|
2018-09-30 16:30:24 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/ratings")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def ratings_list():
|
|
|
|
if current_user.check_visibility(constants.SIDEBAR_RATING):
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('ratings', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Ratings.rating.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Ratings.rating.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Ratings, func.count('books_ratings_link.book').label('count'),
|
2022-03-13 11:34:21 +00:00
|
|
|
(db.Ratings.rating / 2).label('name')) \
|
2020-05-23 08:16:29 +00:00
|
|
|
.join(db.books_ratings_link).join(db.Books).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(text('books_ratings_link.rating')).order_by(order).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=list(),
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"Ratings list"), page="ratingslist", data="ratings", order=order_no)
|
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
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/formats")
|
2016-04-27 08:35:23 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def formats_list():
|
|
|
|
if current_user.check_visibility(constants.SIDEBAR_FORMAT):
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('ratings', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Data.format.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Data.format.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Data,
|
|
|
|
func.count('data.book').label('count'),
|
|
|
|
db.Data.format.label('format')) \
|
|
|
|
.join(db.Books).filter(calibre_db.common_filters()) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.group_by(db.Data.format).order_by(order).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=list(),
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"File formats list"), page="formatslist", data="formats", order=order_no)
|
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
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/language")
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def language_overview():
|
2021-11-21 09:21:45 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_LANGUAGE) and current_user.filter_language() == u"all":
|
2021-12-01 20:38:43 +00:00
|
|
|
order_no = 0 if current_user.get_view_property('language', 'dir') == 'desc' else 1
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = list()
|
2021-12-01 20:38:43 +00:00
|
|
|
languages = calibre_db.speaking_language(reverse_order=not order_no, with_count=True)
|
2021-11-21 09:21:45 +00:00
|
|
|
for lang in languages:
|
2021-12-01 20:38:43 +00:00
|
|
|
upper_lang = lang[0].name[0].upper()
|
2022-03-13 11:34:21 +00:00
|
|
|
if upper_lang not in char_list:
|
|
|
|
char_list.append(upper_lang)
|
2021-12-01 20:38:43 +00:00
|
|
|
return render_title_template('languages.html', languages=languages,
|
2022-03-13 11:34:21 +00:00
|
|
|
charlist=char_list, title=_(u"Languages"), page="langlist",
|
2021-11-21 09:21:45 +00:00
|
|
|
data="language", order=order_no)
|
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
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/category")
|
2016-11-09 18:24:33 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def category_list():
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user.check_visibility(constants.SIDEBAR_CATEGORY):
|
2020-09-28 19:24:47 +00:00
|
|
|
if current_user.get_view_property('category', 'dir') == 'desc':
|
2020-09-27 10:37:41 +00:00
|
|
|
order = db.Tags.name.desc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 0
|
2020-09-28 19:24:47 +00:00
|
|
|
else:
|
|
|
|
order = db.Tags.name.asc()
|
2021-03-21 18:31:32 +00:00
|
|
|
order_no = 1
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Tags, func.count('books_tags_link.book').label('count')) \
|
2020-09-27 10:37:41 +00:00
|
|
|
.join(db.books_tags_link).join(db.Books).order_by(order).filter(calibre_db.common_filters()) \
|
2019-03-08 15:32:56 +00:00
|
|
|
.group_by(text('books_tags_link.tag')).all()
|
2022-03-13 11:34:21 +00:00
|
|
|
char_list = generate_char_list(db.Tags.name, db.books_tags_link)
|
|
|
|
return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=char_list,
|
2021-03-21 18:31:32 +00:00
|
|
|
title=_(u"Categories"), page="catlist", data="category", order=order_no)
|
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
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Task functions ################################################################
|
2019-01-06 13:16:52 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/tasks")
|
2018-07-30 18:12:41 +00:00
|
|
|
@login_required
|
|
|
|
def get_tasks_status():
|
|
|
|
# if current user admin, show all email, otherwise only own emails
|
2020-08-23 03:35:48 +00:00
|
|
|
tasks = WorkerThread.getInstance().tasks
|
2019-07-13 18:45:48 +00:00
|
|
|
answer = render_task_status(tasks)
|
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
|
|
|
|
2020-05-23 19:26:39 +00:00
|
|
|
# ################################### Search functions ################################################################
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.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():
|
2020-04-13 20:23:58 +00:00
|
|
|
term = request.args.get("query")
|
2015-08-02 18:59:11 +00:00
|
|
|
if term:
|
2021-12-28 10:31:32 +00:00
|
|
|
return redirect(url_for('web.books_list', data="search", sort_param='stored', query=term.strip()))
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2020-04-26 16:28:09 +00:00
|
|
|
return render_title_template('search.html',
|
|
|
|
searchterm="",
|
2020-06-08 15:34:03 +00:00
|
|
|
result_count=0,
|
2020-04-26 16:28:09 +00:00
|
|
|
title=_(u"Search"),
|
|
|
|
page="search")
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2020-10-10 08:32:53 +00:00
|
|
|
|
2020-10-16 18:24:15 +00:00
|
|
|
@web.route("/advsearch", methods=['POST'])
|
2016-05-02 20:43:50 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def advanced_search():
|
2020-10-16 18:24:15 +00:00
|
|
|
values = dict(request.form)
|
2021-05-13 11:31:36 +00:00
|
|
|
params = ['include_tag', 'exclude_tag', 'include_serie', 'exclude_serie', 'include_shelf', 'exclude_shelf',
|
|
|
|
'include_language', 'exclude_language', 'include_extension', 'exclude_extension']
|
2020-10-16 18:24:15 +00:00
|
|
|
for param in params:
|
|
|
|
values[param] = list(request.form.getlist(param))
|
|
|
|
flask_session['query'] = json.dumps(values)
|
2020-10-16 17:56:24 +00:00
|
|
|
return redirect(url_for('web.books_list', data="advsearch", sort_param='stored', query=""))
|
|
|
|
|
2020-10-04 17:23:06 +00:00
|
|
|
|
2021-03-14 13:29:40 +00:00
|
|
|
def adv_search_custom_columns(cc, term, q):
|
|
|
|
for c in cc:
|
2021-05-13 11:31:36 +00:00
|
|
|
if c.datatype == "datetime":
|
|
|
|
custom_start = term.get('custom_column_' + str(c.id) + '_start')
|
|
|
|
custom_end = term.get('custom_column_' + str(c.id) + '_end')
|
|
|
|
if custom_start:
|
2021-03-14 13:29:40 +00:00
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any(
|
2021-05-15 05:51:12 +00:00
|
|
|
func.datetime(db.cc_classes[c.id].value) >= func.datetime(custom_start)))
|
2021-05-13 11:31:36 +00:00
|
|
|
if custom_end:
|
2021-03-14 13:29:40 +00:00
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any(
|
2021-05-15 05:51:12 +00:00
|
|
|
func.datetime(db.cc_classes[c.id].value) <= func.datetime(custom_end)))
|
2021-05-13 11:31:36 +00:00
|
|
|
else:
|
|
|
|
custom_query = term.get('custom_column_' + str(c.id))
|
|
|
|
if custom_query != '' and custom_query is not None:
|
|
|
|
if c.datatype == 'bool':
|
|
|
|
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' or c.datatype == 'float':
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any(
|
|
|
|
db.cc_classes[c.id].value == custom_query))
|
|
|
|
elif c.datatype == 'rating':
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any(
|
|
|
|
db.cc_classes[c.id].value == int(float(custom_query) * 2)))
|
|
|
|
else:
|
|
|
|
q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any(
|
|
|
|
func.lower(db.cc_classes[c.id].value).ilike("%" + custom_query + "%")))
|
2021-03-14 13:29:40 +00:00
|
|
|
return q
|
|
|
|
|
|
|
|
|
2021-03-14 13:40:04 +00:00
|
|
|
def adv_search_read_status(q, read_status):
|
|
|
|
if read_status:
|
|
|
|
if config.config_read_column:
|
2021-05-01 16:42:57 +00:00
|
|
|
try:
|
|
|
|
if read_status == "True":
|
|
|
|
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
|
|
|
|
.filter(db.cc_classes[config.config_read_column].value == True)
|
|
|
|
else:
|
|
|
|
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
|
|
|
|
.filter(coalesce(db.cc_classes[config.config_read_column].value, False) != True)
|
2022-03-20 10:21:15 +00:00
|
|
|
except (KeyError, AttributeError, IndexError):
|
|
|
|
log.error(
|
|
|
|
"Custom Column No.{} is not existing in calibre database".format(config.config_read_column))
|
2021-05-01 16:42:57 +00:00
|
|
|
flash(_("Custom Column No.%(column)d is not existing in calibre database",
|
|
|
|
column=config.config_read_column),
|
|
|
|
category="error")
|
|
|
|
return q
|
2021-03-14 13:40:04 +00:00
|
|
|
else:
|
|
|
|
if read_status == "True":
|
|
|
|
q = q.join(ub.ReadBook, db.Books.id == ub.ReadBook.book_id, isouter=True) \
|
|
|
|
.filter(ub.ReadBook.user_id == int(current_user.id),
|
|
|
|
ub.ReadBook.read_status == ub.ReadBook.STATUS_FINISHED)
|
|
|
|
else:
|
|
|
|
q = q.join(ub.ReadBook, db.Books.id == ub.ReadBook.book_id, isouter=True) \
|
|
|
|
.filter(ub.ReadBook.user_id == int(current_user.id),
|
|
|
|
coalesce(ub.ReadBook.read_status, 0) != ub.ReadBook.STATUS_FINISHED)
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
2022-03-14 16:12:35 +00:00
|
|
|
def adv_search_language(q, include_languages_inputs, exclude_languages_inputs):
|
|
|
|
if current_user.filter_language() != "all":
|
|
|
|
q = q.filter(db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()))
|
|
|
|
else:
|
|
|
|
return adv_search_text(q, include_languages_inputs, exclude_languages_inputs, db.Languages.id)
|
2022-03-13 11:34:21 +00:00
|
|
|
return q
|
|
|
|
|
|
|
|
|
2022-03-14 16:12:35 +00:00
|
|
|
def adv_search_ratings(q, rating_high, rating_low):
|
|
|
|
if rating_high:
|
|
|
|
rating_high = int(rating_high) * 2
|
|
|
|
q = q.filter(db.Books.ratings.any(db.Ratings.rating <= rating_high))
|
|
|
|
if rating_low:
|
|
|
|
rating_low = int(rating_low) * 2
|
|
|
|
q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low))
|
2021-03-14 13:40:04 +00:00
|
|
|
return q
|
|
|
|
|
|
|
|
|
2022-03-14 16:12:35 +00:00
|
|
|
def adv_search_text(q, include_inputs, exclude_inputs, data_table):
|
|
|
|
for inp in include_inputs:
|
|
|
|
q = q.filter(getattr(db.Books, data_table.class_.__tablename__).any(data_table == inp))
|
|
|
|
for excl in exclude_inputs:
|
|
|
|
q = q.filter(not_(getattr(db.Books, data_table.class_.__tablename__).any(data_table == excl)))
|
2021-03-14 13:40:04 +00:00
|
|
|
return q
|
|
|
|
|
|
|
|
|
2021-03-16 01:08:41 +00:00
|
|
|
def adv_search_shelf(q, include_shelf_inputs, exclude_shelf_inputs):
|
2022-03-13 11:34:21 +00:00
|
|
|
q = q.outerjoin(ub.BookShelf, db.Books.id == ub.BookShelf.book_id) \
|
2021-10-30 05:31:12 +00:00
|
|
|
.filter(or_(ub.BookShelf.shelf == None, ub.BookShelf.shelf.notin_(exclude_shelf_inputs)))
|
2021-03-20 17:10:27 +00:00
|
|
|
if len(include_shelf_inputs) > 0:
|
2021-03-16 01:08:41 +00:00
|
|
|
q = q.filter(ub.BookShelf.shelf.in_(include_shelf_inputs))
|
|
|
|
return q
|
2021-03-14 13:40:04 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2021-03-14 13:29:40 +00:00
|
|
|
def extend_search_term(searchterm,
|
|
|
|
author_name,
|
|
|
|
book_title,
|
|
|
|
publisher,
|
|
|
|
pub_start,
|
|
|
|
pub_end,
|
2021-03-20 17:10:27 +00:00
|
|
|
tags,
|
2021-03-14 13:29:40 +00:00
|
|
|
rating_high,
|
|
|
|
rating_low,
|
|
|
|
read_status,
|
|
|
|
):
|
|
|
|
searchterm.extend((author_name.replace('|', ','), book_title, publisher))
|
|
|
|
if pub_start:
|
|
|
|
try:
|
|
|
|
searchterm.extend([_(u"Published after ") +
|
|
|
|
format_date(datetime.strptime(pub_start, "%Y-%m-%d"),
|
|
|
|
format='medium', locale=get_locale())])
|
|
|
|
except ValueError:
|
|
|
|
pub_start = u""
|
|
|
|
if pub_end:
|
|
|
|
try:
|
|
|
|
searchterm.extend([_(u"Published before ") +
|
|
|
|
format_date(datetime.strptime(pub_end, "%Y-%m-%d"),
|
|
|
|
format='medium', locale=get_locale())])
|
|
|
|
except ValueError:
|
2021-09-25 14:10:46 +00:00
|
|
|
pub_end = u""
|
2022-03-13 11:34:21 +00:00
|
|
|
elements = {'tag': db.Tags, 'serie': db.Series, 'shelf': ub.Shelf}
|
2021-03-20 17:10:27 +00:00
|
|
|
for key, db_element in elements.items():
|
|
|
|
tag_names = calibre_db.session.query(db_element).filter(db_element.id.in_(tags['include_' + key])).all()
|
|
|
|
searchterm.extend(tag.name for tag in tag_names)
|
2021-05-28 12:57:35 +00:00
|
|
|
tag_names = calibre_db.session.query(db_element).filter(db_element.id.in_(tags['exclude_' + key])).all()
|
2021-03-20 17:10:27 +00:00
|
|
|
searchterm.extend(tag.name for tag in tag_names)
|
2021-03-14 13:29:40 +00:00
|
|
|
language_names = calibre_db.session.query(db.Languages). \
|
2021-03-20 17:10:27 +00:00
|
|
|
filter(db.Languages.id.in_(tags['include_language'])).all()
|
|
|
|
if language_names:
|
|
|
|
language_names = calibre_db.speaking_language(language_names)
|
|
|
|
searchterm.extend(language.name for language in language_names)
|
|
|
|
language_names = calibre_db.session.query(db.Languages). \
|
|
|
|
filter(db.Languages.id.in_(tags['exclude_language'])).all()
|
2021-03-14 13:29:40 +00:00
|
|
|
if language_names:
|
|
|
|
language_names = calibre_db.speaking_language(language_names)
|
|
|
|
searchterm.extend(language.name for language in language_names)
|
|
|
|
if rating_high:
|
|
|
|
searchterm.extend([_(u"Rating <= %(rating)s", rating=rating_high)])
|
|
|
|
if rating_low:
|
|
|
|
searchterm.extend([_(u"Rating >= %(rating)s", rating=rating_low)])
|
|
|
|
if read_status:
|
|
|
|
searchterm.extend([_(u"Read Status = %(status)s", status=read_status)])
|
2021-03-20 17:10:27 +00:00
|
|
|
searchterm.extend(ext for ext in tags['include_extension'])
|
|
|
|
searchterm.extend(ext for ext in tags['exclude_extension'])
|
2021-03-14 13:29:40 +00:00
|
|
|
# handle custom columns
|
|
|
|
searchterm = " + ".join(filter(None, searchterm))
|
2021-03-14 13:40:04 +00:00
|
|
|
return searchterm, pub_start, pub_end
|
2020-10-16 17:56:24 +00:00
|
|
|
|
2020-10-04 17:23:06 +00:00
|
|
|
|
|
|
|
def render_adv_search_results(term, offset=None, order=None, limit=None):
|
2022-03-12 15:51:50 +00:00
|
|
|
sort_param = order[0] if order else [db.Books.sort]
|
2020-10-04 17:23:06 +00:00
|
|
|
pagination = None
|
|
|
|
|
2020-05-23 19:26:39 +00:00
|
|
|
cc = get_cc_columns(filter_config_custom_read=True)
|
2020-05-23 10:51:48 +00:00
|
|
|
calibre_db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
2022-03-26 18:35:56 +00:00
|
|
|
query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
'''if not config.config_read_column:
|
2021-12-05 17:48:21 +00:00
|
|
|
query = (calibre_db.session.query(db.Books, ub.ArchivedBook.is_archived, ub.ReadBook).select_from(db.Books)
|
|
|
|
.outerjoin(ub.ReadBook, and_(db.Books.id == ub.ReadBook.book_id,
|
|
|
|
int(current_user.id) == ub.ReadBook.user_id)))
|
|
|
|
else:
|
|
|
|
try:
|
2022-03-26 18:35:56 +00:00
|
|
|
read_column = db.cc_classes[config.config_read_column]
|
2021-12-05 17:48:21 +00:00
|
|
|
query = (calibre_db.session.query(db.Books, ub.ArchivedBook.is_archived, read_column.value)
|
2022-03-26 18:35:56 +00:00
|
|
|
.select_from(db.Books)
|
|
|
|
.outerjoin(read_column, read_column.book == db.Books.id))
|
2022-03-20 10:21:15 +00:00
|
|
|
except (KeyError, AttributeError, IndexError):
|
|
|
|
log.error("Custom Column No.{} is not existing in calibre database".format(config.config_read_column))
|
2021-12-05 17:48:21 +00:00
|
|
|
# Skip linking read column
|
|
|
|
query = calibre_db.session.query(db.Books, ub.ArchivedBook.is_archived, None)
|
|
|
|
query = query.outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id,
|
2022-03-26 18:35:56 +00:00
|
|
|
int(current_user.id) == ub.ArchivedBook.user_id))'''
|
2021-12-05 17:48:21 +00:00
|
|
|
|
2022-03-26 18:35:56 +00:00
|
|
|
q = query.outerjoin(db.books_series_link, db.books_series_link.c.book == db.Books.id) \
|
2022-03-13 11:34:21 +00:00
|
|
|
.outerjoin(db.Series) \
|
2021-07-26 05:52:01 +00:00
|
|
|
.filter(calibre_db.common_filters(True))
|
2018-08-12 16:21:57 +00:00
|
|
|
|
2021-03-20 17:10:27 +00:00
|
|
|
# parse multiselects to a complete dict
|
|
|
|
tags = dict()
|
|
|
|
elements = ['tag', 'serie', 'shelf', 'language', 'extension']
|
|
|
|
for element in elements:
|
|
|
|
tags['include_' + element] = term.get('include_' + element)
|
|
|
|
tags['exclude_' + element] = term.get('exclude_' + element)
|
2020-07-11 10:09:34 +00:00
|
|
|
|
2020-10-04 17:23:06 +00:00
|
|
|
author_name = term.get("author_name")
|
|
|
|
book_title = term.get("book_title")
|
|
|
|
publisher = term.get("publisher")
|
2021-05-15 05:51:12 +00:00
|
|
|
pub_start = term.get("publishstart")
|
|
|
|
pub_end = term.get("publishend")
|
2020-10-04 17:23:06 +00:00
|
|
|
rating_low = term.get("ratinghigh")
|
|
|
|
rating_high = term.get("ratinglow")
|
|
|
|
description = term.get("comment")
|
2020-12-27 17:59:33 +00:00
|
|
|
read_status = term.get("read_status")
|
2019-07-13 18:45:48 +00:00
|
|
|
if author_name:
|
2020-04-15 17:57:33 +00:00
|
|
|
author_name = author_name.strip().lower().replace(',', '|')
|
2019-07-13 18:45:48 +00:00
|
|
|
if book_title:
|
|
|
|
book_title = book_title.strip().lower()
|
|
|
|
if publisher:
|
|
|
|
publisher = publisher.strip().lower()
|
2018-08-12 16:21:57 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
search_term = []
|
2018-08-12 16:21:57 +00:00
|
|
|
cc_present = False
|
|
|
|
for c in cc:
|
2021-05-13 11:31:36 +00:00
|
|
|
if c.datatype == "datetime":
|
|
|
|
column_start = term.get('custom_column_' + str(c.id) + '_start')
|
|
|
|
column_end = term.get('custom_column_' + str(c.id) + '_end')
|
|
|
|
if column_start:
|
2022-03-13 11:34:21 +00:00
|
|
|
search_term.extend([u"{} >= {}".format(c.name,
|
|
|
|
format_date(datetime.strptime(column_start, "%Y-%m-%d").date(),
|
|
|
|
format='medium',
|
|
|
|
locale=get_locale())
|
|
|
|
)])
|
2021-05-13 11:31:36 +00:00
|
|
|
cc_present = True
|
|
|
|
if column_end:
|
2022-03-13 11:34:21 +00:00
|
|
|
search_term.extend([u"{} <= {}".format(c.name,
|
|
|
|
format_date(datetime.strptime(column_end, "%Y-%m-%d").date(),
|
|
|
|
format='medium',
|
|
|
|
locale=get_locale())
|
|
|
|
)])
|
2021-05-13 11:31:36 +00:00
|
|
|
cc_present = True
|
|
|
|
elif term.get('custom_column_' + str(c.id)):
|
2022-03-13 11:34:21 +00:00
|
|
|
search_term.extend([(u"{}: {}".format(c.name, term.get('custom_column_' + str(c.id))))])
|
2018-08-12 16:21:57 +00:00
|
|
|
cc_present = True
|
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
if any(tags.values()) 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 or read_status:
|
|
|
|
search_term, pub_start, pub_end = extend_search_term(search_term,
|
|
|
|
author_name,
|
|
|
|
book_title,
|
|
|
|
publisher,
|
|
|
|
pub_start,
|
|
|
|
pub_end,
|
|
|
|
tags,
|
|
|
|
rating_high,
|
|
|
|
rating_low,
|
|
|
|
read_status)
|
2021-12-05 17:48:21 +00:00
|
|
|
# q = q.filter()
|
2018-08-12 16:21:57 +00:00
|
|
|
if author_name:
|
2019-07-13 18:45:48 +00:00
|
|
|
q = q.filter(db.Books.authors.any(func.lower(db.Authors.name).ilike("%" + author_name + "%")))
|
2018-08-12 16:21:57 +00:00
|
|
|
if book_title:
|
2019-07-13 18:45:48 +00:00
|
|
|
q = q.filter(func.lower(db.Books.title).ilike("%" + book_title + "%"))
|
2018-08-12 16:21:57 +00:00
|
|
|
if pub_start:
|
2021-05-15 05:51:12 +00:00
|
|
|
q = q.filter(func.datetime(db.Books.pubdate) > func.datetime(pub_start))
|
2018-08-12 16:21:57 +00:00
|
|
|
if pub_end:
|
2021-05-15 05:51:12 +00:00
|
|
|
q = q.filter(func.datetime(db.Books.pubdate) < func.datetime(pub_end))
|
2021-03-14 13:40:04 +00:00
|
|
|
q = adv_search_read_status(q, read_status)
|
2018-08-12 16:21:57 +00:00
|
|
|
if publisher:
|
2019-07-13 18:45:48 +00:00
|
|
|
q = q.filter(db.Books.publishers.any(func.lower(db.Publishers.name).ilike("%" + publisher + "%")))
|
2022-03-13 11:34:21 +00:00
|
|
|
q = adv_search_text(q, tags['include_tag'], tags['exclude_tag'], db.Tags.id)
|
|
|
|
q = adv_search_text(q, tags['include_serie'], tags['exclude_serie'], db.Series.id)
|
|
|
|
q = adv_search_text(q, tags['include_extension'], tags['exclude_extension'], db.Data.format)
|
2021-03-20 17:10:27 +00:00
|
|
|
q = adv_search_shelf(q, tags['include_shelf'], tags['exclude_shelf'])
|
2022-03-14 16:12:35 +00:00
|
|
|
q = adv_search_language(q, tags['include_language'], tags['exclude_language'])
|
2022-03-13 11:34:21 +00:00
|
|
|
q = adv_search_ratings(q, rating_high, rating_low, )
|
2021-03-14 13:40:04 +00:00
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
if description:
|
2019-07-13 18:45:48 +00:00
|
|
|
q = q.filter(db.Books.comments.any(func.lower(db.Comments.text).ilike("%" + description + "%")))
|
2018-08-12 16:21:57 +00:00
|
|
|
|
|
|
|
# search custom culumns
|
2021-05-28 12:54:18 +00:00
|
|
|
try:
|
|
|
|
q = adv_search_custom_columns(cc, term, q)
|
|
|
|
except AttributeError as ex:
|
2022-03-12 16:14:54 +00:00
|
|
|
log.error_or_exception(ex)
|
2021-05-28 12:54:18 +00:00
|
|
|
flash(_("Error on search for custom columns, please restart Calibre-Web"), category="error")
|
2021-03-14 13:29:40 +00:00
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
q = q.order_by(*sort_param).all()
|
2020-10-16 17:56:24 +00:00
|
|
|
flask_session['query'] = json.dumps(term)
|
2021-12-05 17:48:21 +00:00
|
|
|
ub.store_combo_ids(q)
|
2020-10-16 17:56:24 +00:00
|
|
|
result_count = len(q)
|
2021-10-16 09:28:35 +00:00
|
|
|
if offset is not None and limit is not None:
|
2020-10-16 17:56:24 +00:00
|
|
|
offset = int(offset)
|
|
|
|
limit_all = offset + int(limit)
|
|
|
|
pagination = Pagination((offset / (int(limit)) + 1), limit, result_count)
|
|
|
|
else:
|
|
|
|
offset = 0
|
|
|
|
limit_all = result_count
|
2021-12-05 17:48:21 +00:00
|
|
|
entries = calibre_db.order_authors(q[offset:limit_all], list_return=True, combined=True)
|
2020-10-04 17:23:06 +00:00
|
|
|
return render_title_template('search.html',
|
2022-03-13 11:34:21 +00:00
|
|
|
adv_searchterm=search_term,
|
2020-10-04 17:23:06 +00:00
|
|
|
pagination=pagination,
|
2021-11-13 13:57:01 +00:00
|
|
|
entries=entries,
|
2020-10-04 17:23:06 +00:00
|
|
|
result_count=result_count,
|
2021-11-04 16:50:48 +00:00
|
|
|
title=_(u"Advanced Search"), page="advsearch",
|
|
|
|
order=order[1])
|
2020-10-04 17:23:06 +00:00
|
|
|
|
2020-01-25 23:29:17 +00:00
|
|
|
|
2020-10-16 18:24:15 +00:00
|
|
|
@web.route("/advsearch", methods=['GET'])
|
2020-07-11 10:09:34 +00:00
|
|
|
@login_required_if_no_ano
|
|
|
|
def advanced_search_form():
|
|
|
|
# Build custom columns names
|
|
|
|
cc = get_cc_columns(filter_config_custom_read=True)
|
|
|
|
return render_prepare_search_form(cc)
|
2020-01-25 23:29:17 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Download/Send ##################################################################
|
2017-02-19 20:08:22 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/cover/<int:book_id>")
|
2017-02-19 20:08:22 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def get_cover(book_id):
|
|
|
|
return get_book_cover(book_id)
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2020-07-04 19:22:19 +00:00
|
|
|
@web.route("/robots.txt")
|
|
|
|
def get_robots():
|
|
|
|
return send_from_directory(constants.STATIC_DIR, "robots.txt")
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
|
2019-12-01 11:21:21 +00:00
|
|
|
@web.route("/show/<int:book_id>/<book_format>", defaults={'anyname': 'None'})
|
|
|
|
@web.route("/show/<int:book_id>/<book_format>/<anyname>")
|
2017-02-25 10:14:45 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
@viewer_required
|
2019-12-01 11:21:21 +00:00
|
|
|
def serve_book(book_id, book_format, anyname):
|
2019-07-13 18:45:48 +00:00
|
|
|
book_format = book_format.split(".")[0]
|
2020-05-23 08:16:29 +00:00
|
|
|
book = calibre_db.get_book(book_id)
|
2020-12-12 05:41:12 +00:00
|
|
|
data = calibre_db.get_book_format(book_id, book_format.upper())
|
|
|
|
if not data:
|
2021-01-24 10:16:57 +00:00
|
|
|
return "File not in Database"
|
2019-07-13 18:45:48 +00:00
|
|
|
log.info('Serving book: %s', data.name)
|
|
|
|
if config.config_use_google_drive:
|
2021-05-26 11:35:35 +00:00
|
|
|
try:
|
|
|
|
headers = Headers()
|
|
|
|
headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream")
|
|
|
|
df = getFileFromEbooksFolder(book.path, data.name + "." + book_format)
|
|
|
|
return do_gdrive_download(df, headers, (book_format.upper() == 'TXT'))
|
|
|
|
except AttributeError as ex:
|
2022-03-12 16:14:54 +00:00
|
|
|
log.error_or_exception(ex)
|
2021-05-26 11:35:35 +00:00
|
|
|
return "File Not Found"
|
2017-09-17 10:36:32 +00:00
|
|
|
else:
|
2020-12-09 10:04:29 +00:00
|
|
|
if book_format.upper() == 'TXT':
|
2021-01-24 10:16:57 +00:00
|
|
|
try:
|
|
|
|
rawdata = open(os.path.join(config.config_calibre_dir, book.path, data.name + "." + book_format),
|
|
|
|
"rb").read()
|
|
|
|
result = chardet.detect(rawdata)
|
|
|
|
return make_response(
|
2021-09-22 17:58:20 +00:00
|
|
|
rawdata.decode(result['encoding'], 'surrogatepass').encode('utf-8', 'surrogatepass'))
|
2021-01-24 10:16:57 +00:00
|
|
|
except FileNotFoundError:
|
2021-05-26 11:35:35 +00:00
|
|
|
log.error("File Not Found")
|
2021-01-24 10:16:57 +00:00
|
|
|
return "File Not Found"
|
2019-07-13 18:45:48 +00:00
|
|
|
return send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format)
|
2017-08-23 15:52:52 +00:00
|
|
|
|
2020-04-15 17:57:33 +00:00
|
|
|
|
2020-04-01 16:45:16 +00:00
|
|
|
@web.route("/download/<int:book_id>/<book_format>", defaults={'anyname': 'None'})
|
|
|
|
@web.route("/download/<int:book_id>/<book_format>/<anyname>")
|
2017-01-12 19:43:36 +00:00
|
|
|
@login_required_if_no_ano
|
2016-04-27 08:35:23 +00:00
|
|
|
@download_required
|
2020-04-01 16:45:16 +00:00
|
|
|
def download_link(book_id, book_format, anyname):
|
2021-04-04 17:40:34 +00:00
|
|
|
client = "kobo" if "Kobo" in request.headers.get('User-Agent') else ""
|
2019-08-18 19:44:19 +00:00
|
|
|
return get_download_link(book_id, book_format, client)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-12-25 09:09:11 +00:00
|
|
|
@web.route('/send/<int:book_id>/<book_format>/<int:convert>', methods=["POST"])
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
|
|
|
@download_required
|
|
|
|
def send_to_kindle(book_id, book_format, convert):
|
2019-12-28 15:18:21 +00:00
|
|
|
if not config.get_mail_server_configured():
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_(u"Please configure the SMTP mail settings first..."), category="error")
|
|
|
|
elif current_user.kindle_mail:
|
|
|
|
result = send_mail(book_id, book_format, convert, current_user.kindle_mail, config.config_calibre_dir,
|
2021-03-21 17:55:02 +00:00
|
|
|
current_user.name)
|
2019-07-13 18:45:48 +00:00
|
|
|
if result is None:
|
|
|
|
flash(_(u"Book successfully queued for sending to %(kindlemail)s", kindlemail=current_user.kindle_mail),
|
|
|
|
category="success")
|
2018-07-14 06:31:52 +00:00
|
|
|
ub.update_download(book_id, int(current_user.id))
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2020-03-19 02:10:10 +00:00
|
|
|
flash(_(u"Oops! There was an error sending this book: %(res)s", res=result), category="error")
|
2017-01-15 11:23:08 +00:00
|
|
|
else:
|
2020-03-19 02:10:10 +00:00
|
|
|
flash(_(u"Please update your profile with a valid Send to Kindle E-mail Address."), category="error")
|
2020-02-16 18:39:32 +00:00
|
|
|
if "HTTP_REFERER" in request.environ:
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
else:
|
|
|
|
return redirect(url_for('web.index'))
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Login Logout ##################################################################
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-04-14 18:29:11 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.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:
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-12-28 15:18:21 +00:00
|
|
|
if not config.get_mail_server_configured():
|
|
|
|
flash(_(u"E-Mail server is not configured, please contact your administrator!"), category="error")
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', title=_("Register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2021-04-04 17:40:34 +00:00
|
|
|
nickname = to_save["email"].strip() if config.config_register_email else to_save.get('name')
|
|
|
|
if not nickname or not to_save.get("email"):
|
2016-11-09 18:24:33 +00:00
|
|
|
flash(_(u"Please fill out all fields!"), category="error")
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', title=_("Register"), page="register")
|
2021-04-04 17:40:34 +00:00
|
|
|
try:
|
|
|
|
nickname = check_username(nickname)
|
|
|
|
email = check_email(to_save["email"])
|
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), category="error")
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', title=_("Register"), page="register")
|
2020-05-12 15:23:58 +00:00
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
content = ub.User()
|
|
|
|
if check_valid_domain(email):
|
|
|
|
content.name = nickname
|
|
|
|
content.email = email
|
|
|
|
password = generate_random_password()
|
|
|
|
content.password = generate_password_hash(password)
|
|
|
|
content.role = config.config_default_role
|
|
|
|
content.sidebar_view = config.config_default_show
|
|
|
|
try:
|
|
|
|
ub.session.add(content)
|
|
|
|
ub.session.commit()
|
|
|
|
if feature_support['oauth']:
|
|
|
|
register_user_with_oauth(content)
|
|
|
|
send_registration_mail(to_save["email"].strip(), nickname, password)
|
|
|
|
except Exception:
|
|
|
|
ub.session.rollback()
|
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', title=_("Register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
else:
|
2021-04-04 17:40:34 +00:00
|
|
|
flash(_(u"Your e-mail is not allowed to register"), category="error")
|
|
|
|
log.warning('Registering failed for user "%s" e-mail address: %s', nickname, to_save["email"])
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', title=_("Register"), page="register")
|
2021-04-04 17:40:34 +00:00
|
|
|
flash(_(u"Confirmation e-mail was send to your e-mail account."), category="success")
|
|
|
|
return redirect(url_for('web.login'))
|
2015-10-13 00:30:55 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
if feature_support['oauth']:
|
|
|
|
register_user_with_oauth()
|
2021-05-16 07:37:45 +00:00
|
|
|
return render_title_template('register.html', config=config, title=_("Register"), page="register")
|
2015-10-13 00:30:55 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route('/login', methods=['GET', 'POST'])
|
2015-08-02 18:59:11 +00:00
|
|
|
def login():
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
if config.config_login_type == constants.LOGIN_LDAP and not services.ldap:
|
2019-12-28 15:18:21 +00:00
|
|
|
log.error(u"Cannot activate LDAP authentication")
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_(u"Cannot activate LDAP authentication"), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
form = request.form.to_dict()
|
2021-03-21 17:55:02 +00:00
|
|
|
user = ub.session.query(ub.User).filter(func.lower(ub.User.name) == form['username'].strip().lower()) \
|
2019-07-13 18:45:48 +00:00
|
|
|
.first()
|
2020-04-13 20:23:58 +00:00
|
|
|
if config.config_login_type == constants.LOGIN_LDAP and services.ldap and user and form['password'] != "":
|
|
|
|
login_result, error = services.ldap.bind_user(form['username'], form['password'])
|
2019-07-13 18:45:48 +00:00
|
|
|
if login_result:
|
2020-06-20 13:41:26 +00:00
|
|
|
login_user(user, remember=bool(form.get('remember_me')))
|
2021-07-25 03:24:03 +00:00
|
|
|
ub.store_user_session()
|
2021-03-21 17:55:02 +00:00
|
|
|
log.debug(u"You are now logged in as: '%s'", user.name)
|
|
|
|
flash(_(u"you are now logged in as: '%(nickname)s'", nickname=user.name),
|
2019-07-13 18:45:48 +00:00
|
|
|
category="success")
|
|
|
|
return redirect_back(url_for("web.index"))
|
2020-04-15 17:57:33 +00:00
|
|
|
elif login_result is None and user and check_password_hash(str(user.password), form['password']) \
|
2022-03-13 11:34:21 +00:00
|
|
|
and user.name != "Guest":
|
2020-06-20 13:41:26 +00:00
|
|
|
login_user(user, remember=bool(form.get('remember_me')))
|
2021-07-25 03:24:03 +00:00
|
|
|
ub.store_user_session()
|
2021-03-21 17:55:02 +00:00
|
|
|
log.info("Local Fallback Login as: '%s'", user.name)
|
2020-04-13 20:23:58 +00:00
|
|
|
flash(_(u"Fallback Login as: '%(nickname)s', LDAP Server not reachable, or user not known",
|
2021-03-21 17:55:02 +00:00
|
|
|
nickname=user.name),
|
2020-04-15 17:57:33 +00:00
|
|
|
category="warning")
|
2019-12-28 06:45:42 +00:00
|
|
|
return redirect_back(url_for("web.index"))
|
|
|
|
elif login_result is None:
|
2020-04-13 20:23:58 +00:00
|
|
|
log.info(error)
|
|
|
|
flash(_(u"Could not login: %(message)s", message=error), category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2022-03-13 11:34:21 +00:00
|
|
|
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
|
|
|
|
log.warning('LDAP Login failed for user "%s" IP-address: %s', form['username'], ip_address)
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_(u"Wrong Username or Password"), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
else:
|
2022-03-13 11:34:21 +00:00
|
|
|
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
|
2019-12-15 16:08:17 +00:00
|
|
|
if 'forgot' in form and form['forgot'] == 'forgot':
|
2021-10-16 09:28:35 +00:00
|
|
|
if user is not None and user.name != "Guest":
|
2019-12-15 16:08:17 +00:00
|
|
|
ret, __ = reset_password(user.id)
|
|
|
|
if ret == 1:
|
|
|
|
flash(_(u"New Password was send to your email address"), category="info")
|
2022-03-13 11:34:21 +00:00
|
|
|
log.info('Password reset for user "%s" IP-address: %s', form['username'], ip_address)
|
2019-12-15 16:08:17 +00:00
|
|
|
else:
|
2020-08-17 16:29:10 +00:00
|
|
|
log.error(u"An unknown error occurred. Please try again later")
|
2019-12-15 16:08:17 +00:00
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
|
|
|
else:
|
|
|
|
flash(_(u"Please enter valid username to reset password"), category="error")
|
2022-03-13 11:34:21 +00:00
|
|
|
log.warning('Username missing for password reset IP-address: %s', ip_address)
|
2019-12-15 16:08:17 +00:00
|
|
|
else:
|
2021-03-21 17:55:02 +00:00
|
|
|
if user and check_password_hash(str(user.password), form['password']) and user.name != "Guest":
|
2020-06-20 13:41:26 +00:00
|
|
|
login_user(user, remember=bool(form.get('remember_me')))
|
2021-07-25 03:24:03 +00:00
|
|
|
ub.store_user_session()
|
2021-03-21 17:55:02 +00:00
|
|
|
log.debug(u"You are now logged in as: '%s'", user.name)
|
|
|
|
flash(_(u"You are now logged in as: '%(nickname)s'", nickname=user.name), category="success")
|
2019-12-28 06:12:18 +00:00
|
|
|
config.config_is_initial = False
|
2019-12-15 16:08:17 +00:00
|
|
|
return redirect_back(url_for("web.index"))
|
|
|
|
else:
|
2022-03-13 11:34:21 +00:00
|
|
|
log.warning('Login failed for user "%s" IP-address: %s', form['username'], ip_address)
|
2019-12-15 16:08:17 +00:00
|
|
|
flash(_(u"Wrong Username or Password"), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2020-06-27 09:00:08 +00:00
|
|
|
next_url = request.args.get('next', default=url_for("web.index"), type=str)
|
2020-09-21 16:34:39 +00:00
|
|
|
if url_for("web.logout") == next_url:
|
|
|
|
next_url = url_for("web.index")
|
2020-04-15 17:57:33 +00:00
|
|
|
return render_title_template('login.html',
|
2021-05-16 07:37:45 +00:00
|
|
|
title=_(u"Login"),
|
2020-04-15 17:57:33 +00:00
|
|
|
next_url=next_url,
|
|
|
|
config=config,
|
|
|
|
oauth_check=oauth_check,
|
|
|
|
mail=config.get_mail_server_configured(), page="login")
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route('/logout')
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
|
|
|
def logout():
|
2017-10-10 14:18:28 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2022-03-13 11:34:21 +00:00
|
|
|
ub.delete_user_session(current_user.id, flask_session.get('_id', ""))
|
2015-08-02 18:59:11 +00:00
|
|
|
logout_user()
|
2019-07-13 18:45:48 +00:00
|
|
|
if feature_support['oauth'] and (config.config_login_type == 2 or config.config_login_type == 3):
|
|
|
|
logout_oauth_user()
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"User logged out")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for('web.login'))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ################################### Users own configuration #########################################################
|
2021-07-23 18:12:37 +00:00
|
|
|
def change_profile(kobo_support, local_oauth_check, oauth_status, translations, languages):
|
2021-03-14 14:06:09 +00:00
|
|
|
to_save = request.form.to_dict()
|
|
|
|
current_user.random_books = 0
|
|
|
|
if current_user.role_passwd() or current_user.role_admin():
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("password"):
|
2021-03-14 14:06:09 +00:00
|
|
|
current_user.password = generate_password_hash(to_save["password"])
|
2021-04-04 17:40:34 +00:00
|
|
|
try:
|
|
|
|
if to_save.get("kindle_mail", current_user.kindle_mail) != current_user.kindle_mail:
|
|
|
|
current_user.kindle_mail = valid_email(to_save["kindle_mail"])
|
|
|
|
if to_save.get("email", current_user.email) != current_user.email:
|
|
|
|
current_user.email = check_email(to_save["email"])
|
2021-07-23 17:34:46 +00:00
|
|
|
if current_user.role_admin():
|
|
|
|
if to_save.get("name", current_user.name) != current_user.name:
|
2022-03-13 11:34:21 +00:00
|
|
|
# Query username, if not existing, change
|
2021-07-23 17:34:46 +00:00
|
|
|
current_user.name = check_username(to_save["name"])
|
2021-04-04 17:40:34 +00:00
|
|
|
current_user.random_books = 1 if to_save.get("show_random") == "on" else 0
|
|
|
|
if to_save.get("default_language"):
|
|
|
|
current_user.default_language = to_save["default_language"]
|
|
|
|
if to_save.get("locale"):
|
|
|
|
current_user.locale = to_save["locale"]
|
2021-10-10 08:23:58 +00:00
|
|
|
old_state = current_user.kobo_only_shelves_sync
|
|
|
|
# 1 -> 0: nothing has to be done
|
|
|
|
# 0 -> 1: all synced books have to be added to archived books, + currently synced shelfs which
|
|
|
|
# don't have to be synced have to be removed (added to Shelf archive)
|
2021-05-15 08:45:51 +00:00
|
|
|
current_user.kobo_only_shelves_sync = int(to_save.get("kobo_only_shelves_sync") == "on") or 0
|
2021-10-10 08:23:58 +00:00
|
|
|
if old_state == 0 and current_user.kobo_only_shelves_sync == 1:
|
|
|
|
kobo_sync_status.update_on_sync_shelfs(current_user.id)
|
2021-05-15 08:45:51 +00:00
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), category="error")
|
2021-07-23 18:12:37 +00:00
|
|
|
return render_title_template("user_edit.html",
|
|
|
|
content=current_user,
|
|
|
|
translations=translations,
|
|
|
|
profile=1,
|
|
|
|
languages=languages,
|
|
|
|
title=_(u"%(name)s's profile", name=current_user.name),
|
|
|
|
page="me",
|
2021-04-04 17:40:34 +00:00
|
|
|
kobo_support=kobo_support,
|
2021-07-23 18:12:37 +00:00
|
|
|
registered_oauth=local_oauth_check,
|
|
|
|
oauth_status=oauth_status)
|
2021-03-14 12:28:52 +00:00
|
|
|
|
|
|
|
val = 0
|
|
|
|
for key, __ in to_save.items():
|
|
|
|
if key.startswith('show'):
|
|
|
|
val += int(key[5:])
|
|
|
|
current_user.sidebar_view = val
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("Show_detail_random"):
|
2021-03-14 12:28:52 +00:00
|
|
|
current_user.sidebar_view += constants.DETAIL_RANDOM
|
|
|
|
|
|
|
|
try:
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u"Profile updated"), category="success")
|
|
|
|
log.debug(u"Profile updated")
|
|
|
|
except IntegrityError:
|
|
|
|
ub.session.rollback()
|
2021-05-16 07:37:45 +00:00
|
|
|
flash(_(u"Found an existing account for this e-mail address"), category="error")
|
|
|
|
log.debug(u"Found an existing account for this e-mail address")
|
2021-03-14 12:28:52 +00:00
|
|
|
except OperationalError as e:
|
|
|
|
ub.session.rollback()
|
|
|
|
log.error("Database error: %s", e)
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e), category="error")
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/me", methods=["GET", "POST"])
|
2015-08-02 18:59:11 +00:00
|
|
|
@login_required
|
|
|
|
def profile():
|
2020-05-23 08:16:29 +00:00
|
|
|
languages = calibre_db.speaking_language()
|
2022-03-13 11:34:21 +00:00
|
|
|
translations = babel.list_translations() + [Locale('en')]
|
2020-01-27 19:32:37 +00:00
|
|
|
kobo_support = feature_support['kobo'] and config.config_kobo_sync
|
2020-08-22 08:27:09 +00:00
|
|
|
if feature_support['oauth'] and config.config_login_type == 2:
|
2019-07-13 18:45:48 +00:00
|
|
|
oauth_status = get_oauth_status()
|
2020-08-26 16:22:56 +00:00
|
|
|
local_oauth_check = oauth_check
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
oauth_status = None
|
2020-08-26 16:22:56 +00:00
|
|
|
local_oauth_check = {}
|
2020-01-27 19:32:37 +00:00
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
if request.method == "POST":
|
2021-07-23 18:12:37 +00:00
|
|
|
change_profile(kobo_support, local_oauth_check, oauth_status, translations, languages)
|
2020-09-27 14:00:17 +00:00
|
|
|
return render_title_template("user_edit.html",
|
|
|
|
translations=translations,
|
|
|
|
profile=1,
|
|
|
|
languages=languages,
|
|
|
|
content=current_user,
|
|
|
|
kobo_support=kobo_support,
|
2021-03-21 17:55:02 +00:00
|
|
|
title=_(u"%(name)s's profile", name=current_user.name),
|
2020-09-27 14:00:17 +00:00
|
|
|
page="me",
|
|
|
|
registered_oauth=local_oauth_check,
|
|
|
|
oauth_status=oauth_status)
|
2017-01-28 19:16:40 +00:00
|
|
|
|
2017-01-22 15:44:37 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
# ###################################Show single book ##################################################################
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-12-23 08:53:39 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/read/<int:book_id>/<book_format>")
|
2018-09-30 07:43:20 +00:00
|
|
|
@login_required_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
@viewer_required
|
|
|
|
def read_book(book_id, book_format):
|
2020-05-23 08:16:29 +00:00
|
|
|
book = calibre_db.get_filtered_book(book_id)
|
2022-03-13 09:23:13 +00:00
|
|
|
book.ordered_authors = calibre_db.order_authors([book], False)
|
|
|
|
|
2018-09-30 07:43:20 +00:00
|
|
|
if not book:
|
2022-03-13 11:34:21 +00:00
|
|
|
flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"),
|
|
|
|
category="error")
|
2021-05-16 07:37:45 +00:00
|
|
|
log.debug(u"Oops! Selected book title is unavailable. File does not exist or is not accessible")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for("web.index"))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
2022-03-13 09:23:13 +00:00
|
|
|
# check if book has a bookmark
|
2019-07-13 18:45:48 +00:00
|
|
|
bookmark = None
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
bookmark = ub.session.query(ub.Bookmark).filter(and_(ub.Bookmark.user_id == int(current_user.id),
|
|
|
|
ub.Bookmark.book_id == book_id,
|
|
|
|
ub.Bookmark.format == book_format.upper())).first()
|
|
|
|
if book_format.lower() == "epub":
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"Start epub reader for %d", book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
return render_title_template('read.html', bookid=book_id, title=book.title, bookmark=bookmark)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif book_format.lower() == "pdf":
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"Start pdf reader for %d", book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
return render_title_template('readpdf.html', pdffile=book_id, title=book.title)
|
2019-07-13 18:45:48 +00:00
|
|
|
elif book_format.lower() == "txt":
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"Start txt reader for %d", book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
return render_title_template('readtxt.html', txtfile=book_id, title=book.title)
|
2020-11-15 12:19:37 +00:00
|
|
|
elif book_format.lower() == "djvu":
|
|
|
|
log.debug(u"Start djvu reader for %d", book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
return render_title_template('readdjvu.html', djvufile=book_id, title=book.title)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2020-05-17 08:07:27 +00:00
|
|
|
for fileExt in constants.EXTENSIONS_AUDIO:
|
2019-07-13 18:45:48 +00:00
|
|
|
if book_format.lower() == fileExt:
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.get_filtered_book(book_id)
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"Start mp3 listening for %d", book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template('listenmp3.html', mp3file=book_id, audioformat=book_format.lower(),
|
2021-07-11 10:52:35 +00:00
|
|
|
entry=entries, bookmark=bookmark)
|
2019-07-13 18:45:48 +00:00
|
|
|
for fileExt in ["cbr", "cbt", "cbz"]:
|
|
|
|
if book_format.lower() == fileExt:
|
|
|
|
all_name = str(book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
title = book.title
|
|
|
|
if len(book.series):
|
|
|
|
title = title + " - " + book.series[0].name
|
|
|
|
if book.series_index:
|
2021-07-12 12:04:23 +00:00
|
|
|
title = title + " #" + '{0:.2f}'.format(book.series_index).rstrip('0').rstrip('.')
|
2019-12-28 15:18:21 +00:00
|
|
|
log.debug(u"Start comic reader for %d", book_id)
|
2021-07-11 10:52:35 +00:00
|
|
|
return render_title_template('readcbr.html', comicfile=all_name, title=title,
|
2019-07-13 18:45:48 +00:00
|
|
|
extension=fileExt)
|
2021-05-16 07:37:45 +00:00
|
|
|
log.debug(u"Oops! Selected book title is unavailable. File does not exist or is not accessible")
|
2022-03-13 11:34:21 +00:00
|
|
|
flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"),
|
|
|
|
category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for("web.index"))
|
2016-11-09 18:24:33 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@web.route("/book/<int:book_id>")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def show_book(book_id):
|
2021-10-24 07:48:29 +00:00
|
|
|
entries = calibre_db.get_book_read_archived(book_id, config.config_read_column, allow_show_archived=True)
|
2019-07-13 18:45:48 +00:00
|
|
|
if entries:
|
2021-10-24 07:48:29 +00:00
|
|
|
read_book = entries[1]
|
|
|
|
archived_book = entries[2]
|
|
|
|
entry = entries[0]
|
|
|
|
entry.read_status = read_book == ub.ReadBook.STATUS_FINISHED
|
|
|
|
entry.is_archived = archived_book
|
2022-03-13 11:34:21 +00:00
|
|
|
for lang_index in range(0, len(entry.languages)):
|
|
|
|
entry.languages[lang_index].language_name = isoLanguages.get_language_name(get_locale(), entry.languages[
|
|
|
|
lang_index].lang_code)
|
2020-04-19 10:50:58 +00:00
|
|
|
cc = get_cc_columns(filter_config_custom_read=True)
|
2022-03-13 11:34:21 +00:00
|
|
|
book_in_shelves = []
|
2019-07-13 18:45:48 +00:00
|
|
|
shelfs = ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).all()
|
2021-10-24 07:48:29 +00:00
|
|
|
for sh in shelfs:
|
2022-03-13 11:34:21 +00:00
|
|
|
book_in_shelves.append(sh.shelf)
|
2019-02-27 18:30:13 +00:00
|
|
|
|
2021-10-24 07:48:29 +00:00
|
|
|
entry.tags = sort(entry.tags, key=lambda tag: tag.name)
|
2016-06-05 15:41:47 +00:00
|
|
|
|
2022-03-12 15:51:50 +00:00
|
|
|
entry.ordered_authors = calibre_db.order_authors([entry])
|
2018-07-14 11:48:51 +00:00
|
|
|
|
2021-10-24 07:48:29 +00:00
|
|
|
entry.kindle_list = check_send_to_kindle(entry)
|
|
|
|
entry.reader_list = check_read_formats(entry)
|
2017-03-13 00:44:20 +00:00
|
|
|
|
2021-10-24 07:48:29 +00:00
|
|
|
entry.audioentries = []
|
|
|
|
for media_format in entry.data:
|
2019-07-13 18:45:48 +00:00
|
|
|
if media_format.format.lower() in constants.EXTENSIONS_AUDIO:
|
2021-10-24 07:48:29 +00:00
|
|
|
entry.audioentries.append(media_format.format.lower())
|
2018-08-28 23:13:04 +00:00
|
|
|
|
2020-11-22 09:03:10 +00:00
|
|
|
return render_title_template('detail.html',
|
2021-10-24 07:48:29 +00:00
|
|
|
entry=entry,
|
2020-11-22 09:03:10 +00:00
|
|
|
cc=cc,
|
2022-03-13 11:34:21 +00:00
|
|
|
is_xhr=request.headers.get('X-Requested-With') == 'XMLHttpRequest',
|
2021-10-24 07:48:29 +00:00
|
|
|
title=entry.title,
|
2022-03-13 11:34:21 +00:00
|
|
|
books_shelfs=book_in_shelves,
|
2020-11-22 09:03:10 +00:00
|
|
|
page="book")
|
2018-08-29 03:32:30 +00:00
|
|
|
else:
|
2021-05-16 07:37:45 +00:00
|
|
|
log.debug(u"Oops! Selected book title is unavailable. File does not exist or is not accessible")
|
|
|
|
flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"),
|
|
|
|
category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for("web.index"))
|