2019-02-08 19:11:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
import sys
|
2021-02-06 23:21:51 +00:00
|
|
|
from datetime import datetime
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2021-02-06 23:21:51 +00:00
|
|
|
from flask import Blueprint, flash, redirect, request, url_for
|
2019-02-08 19:11:44 +00:00
|
|
|
from flask_babel import gettext as _
|
2021-02-06 23:21:51 +00:00
|
|
|
from flask_login import current_user, login_required
|
|
|
|
from sqlalchemy.exc import InvalidRequestError, OperationalError
|
2021-01-03 10:53:06 +00:00
|
|
|
from sqlalchemy.sql.expression import func, true
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2021-02-06 23:21:51 +00:00
|
|
|
from . import calibre_db, config, db, logger, ub
|
2020-12-12 10:23:17 +00:00
|
|
|
from .render_template import render_title_template
|
|
|
|
from .usermanagement import login_required_if_no_ano
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
shelf = Blueprint('shelf', __name__)
|
2019-06-11 16:35:20 +00:00
|
|
|
log = logger.create()
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
def check_shelf_edit_permissions(cur_shelf):
|
|
|
|
if not cur_shelf.is_public and not cur_shelf.user_id == int(current_user.id):
|
|
|
|
log.error("User %s not allowed to edit shelf %s", current_user, cur_shelf)
|
|
|
|
return False
|
|
|
|
if cur_shelf.is_public and not current_user.role_edit_shelfs():
|
|
|
|
log.info("User %s not allowed to edit public shelves", current_user)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def check_shelf_view_permissions(cur_shelf):
|
|
|
|
if cur_shelf.is_public:
|
|
|
|
return True
|
|
|
|
if current_user.is_anonymous or cur_shelf.user_id != current_user.id:
|
|
|
|
log.error("User is unauthorized to view non-public shelf: %s", cur_shelf)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
@shelf.route("/shelf/add/<int:shelf_id>/<int:book_id>")
|
|
|
|
@login_required
|
|
|
|
def add_to_shelf(shelf_id, book_id):
|
2020-02-08 13:39:46 +00:00
|
|
|
xhr = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
|
2019-02-08 19:11:44 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
if shelf is None:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.error("Invalid shelf specified: %s", shelf_id)
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Invalid shelf specified"), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "Invalid shelf specified", 400
|
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
if not check_shelf_edit_permissions(shelf):
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2021-07-24 03:49:16 +00:00
|
|
|
flash(_(u"Sorry you are not allowed to add a book to that shelf"), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2021-07-22 04:05:11 +00:00
|
|
|
return "Sorry you are not allowed to add a book to the that shelf", 403
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
book_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id,
|
2020-04-19 17:08:58 +00:00
|
|
|
ub.BookShelf.book_id == book_id).first()
|
2019-02-08 19:11:44 +00:00
|
|
|
if book_in_shelf:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.error("Book %s is already part of %s", book_id, shelf)
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Book is already part of the shelf: %(shelfname)s", shelfname=shelf.name), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "Book is already part of the shelf: %s" % shelf.name, 400
|
|
|
|
|
|
|
|
maxOrder = ub.session.query(func.max(ub.BookShelf.order)).filter(ub.BookShelf.shelf == shelf_id).first()
|
|
|
|
if maxOrder[0] is None:
|
|
|
|
maxOrder = 0
|
|
|
|
else:
|
|
|
|
maxOrder = maxOrder[0]
|
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
shelf.books.append(ub.BookShelf(shelf=shelf.id, book_id=book_id, order=maxOrder + 1))
|
2020-04-24 14:56:08 +00:00
|
|
|
shelf.last_modified = datetime.utcnow()
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
ub.session.merge(shelf)
|
|
|
|
ub.session.commit()
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
2020-06-27 10:31:26 +00:00
|
|
|
flash(_(u"Settings DB is not Writeable"), category="error")
|
|
|
|
if "HTTP_REFERER" in request.environ:
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
else:
|
|
|
|
return redirect(url_for('web.index'))
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.debug("Book has been added to shelf: {}".format(shelf.name))
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Book has been added to shelf: %(sname)s", sname=shelf.name), category="success")
|
|
|
|
if "HTTP_REFERER" in request.environ:
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
else:
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "", 204
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/massadd/<int:shelf_id>")
|
|
|
|
@login_required
|
|
|
|
def search_to_shelf(shelf_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
if shelf is None:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.error("Invalid shelf specified: %s", shelf_id)
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Invalid shelf specified"), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
if not check_shelf_edit_permissions(shelf):
|
2021-11-20 11:17:03 +00:00
|
|
|
log.warning("You are not allowed to add a book to the shelf".format(shelf.name))
|
|
|
|
flash(_(u"You are not allowed to add a book to the shelf"), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
|
2020-10-10 08:32:53 +00:00
|
|
|
if current_user.id in ub.searched_ids and ub.searched_ids[current_user.id]:
|
2019-02-08 19:11:44 +00:00
|
|
|
books_for_shelf = list()
|
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).all()
|
|
|
|
if books_in_shelf:
|
|
|
|
book_ids = list()
|
|
|
|
for book_id in books_in_shelf:
|
|
|
|
book_ids.append(book_id.book_id)
|
2020-10-10 08:32:53 +00:00
|
|
|
for searchid in ub.searched_ids[current_user.id]:
|
2019-03-16 15:53:22 +00:00
|
|
|
if searchid not in book_ids:
|
|
|
|
books_for_shelf.append(searchid)
|
2019-02-08 19:11:44 +00:00
|
|
|
else:
|
2020-10-10 08:32:53 +00:00
|
|
|
books_for_shelf = ub.searched_ids[current_user.id]
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
if not books_for_shelf:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Books are already part of {}".format(shelf.name))
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Books are already part of the shelf: %(name)s", name=shelf.name), category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
|
2020-12-13 12:54:09 +00:00
|
|
|
maxOrder = ub.session.query(func.max(ub.BookShelf.order)).filter(ub.BookShelf.shelf == shelf_id).first()[0] or 0
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
for book in books_for_shelf:
|
2020-12-13 12:54:09 +00:00
|
|
|
maxOrder += 1
|
2020-03-12 23:31:42 +00:00
|
|
|
shelf.books.append(ub.BookShelf(shelf=shelf.id, book_id=book, order=maxOrder))
|
2020-04-24 14:56:08 +00:00
|
|
|
shelf.last_modified = datetime.utcnow()
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
ub.session.merge(shelf)
|
|
|
|
ub.session.commit()
|
|
|
|
flash(_(u"Books have been added to shelf: %(sname)s", sname=shelf.name), category="success")
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2019-02-08 19:11:44 +00:00
|
|
|
else:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Could not add books to shelf: {}".format(shelf.name))
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Could not add books to shelf: %(sname)s", sname=shelf.name), category="error")
|
2019-02-09 18:01:57 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/remove/<int:shelf_id>/<int:book_id>")
|
|
|
|
@login_required
|
|
|
|
def remove_from_shelf(shelf_id, book_id):
|
2020-02-08 13:39:46 +00:00
|
|
|
xhr = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
|
2019-02-08 19:11:44 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
if shelf is None:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Invalid shelf specified: {}".format(shelf_id))
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "Invalid shelf specified", 400
|
|
|
|
|
|
|
|
# if shelf is public and use is allowed to edit shelfs, or if shelf is private and user is owner
|
|
|
|
# allow editing shelfs
|
|
|
|
# result shelf public user allowed user owner
|
|
|
|
# false 1 0 x
|
|
|
|
# true 1 1 x
|
|
|
|
# true 0 x 1
|
|
|
|
# false 0 x 0
|
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
if check_shelf_edit_permissions(shelf):
|
2019-02-08 19:11:44 +00:00
|
|
|
book_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id,
|
|
|
|
ub.BookShelf.book_id == book_id).first()
|
|
|
|
|
|
|
|
if book_shelf is None:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.error("Book %s already removed from %s", book_id, shelf)
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "Book already removed from shelf", 410
|
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
ub.session.delete(book_shelf)
|
|
|
|
shelf.last_modified = datetime.utcnow()
|
|
|
|
ub.session.commit()
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2020-06-27 10:31:26 +00:00
|
|
|
if "HTTP_REFERER" in request.environ:
|
|
|
|
return redirect(request.environ["HTTP_REFERER"])
|
|
|
|
else:
|
|
|
|
return redirect(url_for('web.index'))
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"Book has been removed from shelf: %(sname)s", sname=shelf.name), category="success")
|
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'))
|
2019-02-08 19:11:44 +00:00
|
|
|
return "", 204
|
|
|
|
else:
|
2020-02-08 13:39:46 +00:00
|
|
|
if not xhr:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.warning("You are not allowed to remove a book from shelf: {}".format(shelf.name))
|
2021-11-20 11:17:03 +00:00
|
|
|
flash(_(u"Sorry you are not allowed to remove a book from this shelf"),
|
2019-02-08 19:11:44 +00:00
|
|
|
category="error")
|
2019-02-16 06:23:08 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2021-11-20 11:17:03 +00:00
|
|
|
return "Sorry you are not allowed to remove a book from this shelf", 403
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/create", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def create_shelf():
|
2021-11-21 15:21:27 +00:00
|
|
|
shelf = ub.Shelf()
|
|
|
|
return create_edit_shelf(shelf, page_title=_(u"Create a Shelf"), page="shelfcreate")
|
2021-11-20 12:00:54 +00:00
|
|
|
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/edit/<int:shelf_id>", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def edit_shelf(shelf_id):
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2021-07-22 03:41:07 +00:00
|
|
|
if not check_shelf_edit_permissions(shelf):
|
2021-07-22 04:05:11 +00:00
|
|
|
flash(_(u"Sorry you are not allowed to edit this shelf"), category="error")
|
2021-07-22 03:41:07 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2021-07-24 03:49:16 +00:00
|
|
|
return create_edit_shelf(shelf, page_title=_(u"Edit a shelf"), page="shelfedit", shelf_id=shelf_id)
|
2020-04-02 16:23:24 +00:00
|
|
|
|
|
|
|
|
2021-01-03 10:53:06 +00:00
|
|
|
# if shelf ID is set, we are editing a shelf
|
2021-07-24 03:49:16 +00:00
|
|
|
def create_edit_shelf(shelf, page_title, page, shelf_id=False):
|
2021-05-15 08:45:51 +00:00
|
|
|
sync_only_selected_shelves = current_user.kobo_only_shelves_sync
|
|
|
|
# calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id).filter(ub.Shelf.kobo_sync).count()
|
2021-01-03 10:53:06 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2021-11-21 15:21:27 +00:00
|
|
|
if not current_user.role_edit_shelfs() and to_save.get("is_public") == "on":
|
|
|
|
flash(_(u"Sorry you are not allowed to create a public shelf"), category="error")
|
|
|
|
return redirect(url_for('web.index'))
|
2021-05-02 08:05:25 +00:00
|
|
|
shelf.is_public = 1 if to_save.get("is_public") else 0
|
2021-02-06 23:21:51 +00:00
|
|
|
if config.config_kobo_sync:
|
2021-05-02 08:05:25 +00:00
|
|
|
shelf.kobo_sync = True if to_save.get("kobo_sync") else False
|
2021-07-24 03:49:16 +00:00
|
|
|
shelf_title = to_save.get("title", "")
|
|
|
|
if check_shelf_is_unique(shelf, shelf_title, shelf_id):
|
|
|
|
shelf.name = shelf_title
|
2021-01-03 10:53:06 +00:00
|
|
|
if not shelf_id:
|
|
|
|
shelf.user_id = int(current_user.id)
|
2021-01-03 15:41:38 +00:00
|
|
|
ub.session.add(shelf)
|
|
|
|
shelf_action = "created"
|
2021-07-24 03:49:16 +00:00
|
|
|
flash_text = _(u"Shelf %(title)s created", title=shelf_title)
|
2021-01-03 15:41:38 +00:00
|
|
|
else:
|
|
|
|
shelf_action = "changed"
|
2021-07-24 03:49:16 +00:00
|
|
|
flash_text = _(u"Shelf %(title)s changed", title=shelf_title)
|
2019-02-08 19:11:44 +00:00
|
|
|
try:
|
|
|
|
ub.session.commit()
|
2021-07-24 03:49:16 +00:00
|
|
|
log.info(u"Shelf {} {}".format(shelf_title, shelf_action))
|
2021-01-03 10:53:06 +00:00
|
|
|
flash(flash_text, category="success")
|
|
|
|
return redirect(url_for('shelf.show_shelf', shelf_id=shelf.id))
|
2021-04-04 17:40:34 +00:00
|
|
|
except (OperationalError, InvalidRequestError) as ex:
|
2020-06-27 10:31:26 +00:00
|
|
|
ub.session.rollback()
|
2021-04-04 17:40:34 +00:00
|
|
|
log.debug_or_exception(ex)
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
2020-06-27 10:31:26 +00:00
|
|
|
ub.session.rollback()
|
2021-04-04 17:40:34 +00:00
|
|
|
log.debug_or_exception(ex)
|
2019-02-08 19:11:44 +00:00
|
|
|
flash(_(u"There was an error"), category="error")
|
2021-02-06 23:19:24 +00:00
|
|
|
return render_title_template('shelf_edit.html',
|
|
|
|
shelf=shelf,
|
2021-07-24 03:49:16 +00:00
|
|
|
title=page_title,
|
2021-02-06 23:19:24 +00:00
|
|
|
page=page,
|
2021-05-02 13:59:40 +00:00
|
|
|
kobo_sync_enabled=config.config_kobo_sync,
|
2021-05-15 08:45:51 +00:00
|
|
|
sync_only_selected_shelves=sync_only_selected_shelves)
|
2021-01-03 10:53:06 +00:00
|
|
|
|
|
|
|
|
2021-07-24 03:49:16 +00:00
|
|
|
def check_shelf_is_unique(shelf, title, shelf_id=False):
|
2021-01-03 10:53:06 +00:00
|
|
|
if shelf_id:
|
|
|
|
ident = ub.Shelf.id != shelf_id
|
2019-02-08 19:11:44 +00:00
|
|
|
else:
|
2021-01-03 10:53:06 +00:00
|
|
|
ident = true()
|
|
|
|
if shelf.is_public == 1:
|
|
|
|
is_shelf_name_unique = ub.session.query(ub.Shelf) \
|
2021-07-24 03:49:16 +00:00
|
|
|
.filter((ub.Shelf.name == title) & (ub.Shelf.is_public == 1)) \
|
2021-01-03 10:53:06 +00:00
|
|
|
.filter(ident) \
|
|
|
|
.first() is None
|
|
|
|
|
|
|
|
if not is_shelf_name_unique:
|
2021-07-24 03:49:16 +00:00
|
|
|
log.error("A public shelf with the name '{}' already exists.".format(title))
|
|
|
|
flash(_(u"A public shelf with the name '%(title)s' already exists.", title=title),
|
2021-01-03 10:53:06 +00:00
|
|
|
category="error")
|
|
|
|
else:
|
|
|
|
is_shelf_name_unique = ub.session.query(ub.Shelf) \
|
2021-07-24 03:49:16 +00:00
|
|
|
.filter((ub.Shelf.name == title) & (ub.Shelf.is_public == 0) &
|
2021-01-03 10:53:06 +00:00
|
|
|
(ub.Shelf.user_id == int(current_user.id))) \
|
|
|
|
.filter(ident) \
|
|
|
|
.first() is None
|
|
|
|
|
|
|
|
if not is_shelf_name_unique:
|
2021-07-24 03:49:16 +00:00
|
|
|
log.error("A private shelf with the name '{}' already exists.".format(title))
|
|
|
|
flash(_(u"A private shelf with the name '%(title)s' already exists.", title=title),
|
2021-01-03 10:53:06 +00:00
|
|
|
category="error")
|
|
|
|
return is_shelf_name_unique
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
|
2020-03-12 23:31:42 +00:00
|
|
|
def delete_shelf_helper(cur_shelf):
|
|
|
|
if not cur_shelf or not check_shelf_edit_permissions(cur_shelf):
|
|
|
|
return
|
|
|
|
shelf_id = cur_shelf.id
|
|
|
|
ub.session.delete(cur_shelf)
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).delete()
|
2020-04-24 14:56:08 +00:00
|
|
|
ub.session.add(ub.ShelfArchive(uuid=cur_shelf.uuid, user_id=cur_shelf.user_id))
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("successfully deleted Shelf {}".format(cur_shelf.name))
|
2020-03-12 23:31:42 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
|
2019-02-08 19:11:44 +00:00
|
|
|
@shelf.route("/shelf/delete/<int:shelf_id>")
|
|
|
|
@login_required
|
|
|
|
def delete_shelf(shelf_id):
|
|
|
|
cur_shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
delete_shelf_helper(cur_shelf)
|
2020-12-13 12:54:09 +00:00
|
|
|
except InvalidRequestError:
|
2020-06-27 10:31:26 +00:00
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2019-02-09 18:01:57 +00:00
|
|
|
return redirect(url_for('web.index'))
|
2019-02-08 19:11:44 +00:00
|
|
|
|
2021-01-03 14:40:17 +00:00
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
@shelf.route("/simpleshelf/<int:shelf_id>")
|
|
|
|
@login_required_if_no_ano
|
|
|
|
def show_simpleshelf(shelf_id):
|
|
|
|
return render_show_shelf(2, shelf_id, 1, None)
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2021-01-03 14:40:17 +00:00
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
@shelf.route("/shelf/<int:shelf_id>", defaults={"sort_param": "order", 'page': 1})
|
|
|
|
@shelf.route("/shelf/<int:shelf_id>/<sort_param>", defaults={'page': 1})
|
|
|
|
@shelf.route("/shelf/<int:shelf_id>/<sort_param>/<int:page>")
|
2020-09-01 17:25:57 +00:00
|
|
|
@login_required_if_no_ano
|
2020-12-13 17:32:19 +00:00
|
|
|
def show_shelf(shelf_id, sort_param, page):
|
|
|
|
return render_show_shelf(1, shelf_id, page, sort_param)
|
2019-02-08 19:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/order/<int:shelf_id>", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def order_shelf(shelf_id):
|
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).order_by(
|
|
|
|
ub.BookShelf.order.asc()).all()
|
|
|
|
counter = 0
|
|
|
|
for book in books_in_shelf:
|
|
|
|
setattr(book, 'order', to_save[str(book.book_id)])
|
|
|
|
counter += 1
|
2020-04-24 14:56:08 +00:00
|
|
|
# if order diffrent from before -> shelf.last_modified = datetime.utcnow()
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
ub.session.commit()
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2020-03-12 23:31:42 +00:00
|
|
|
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
2019-02-08 19:11:44 +00:00
|
|
|
result = list()
|
2020-03-12 23:31:42 +00:00
|
|
|
if shelf and check_shelf_view_permissions(shelf):
|
2021-02-06 23:21:51 +00:00
|
|
|
result = calibre_db.session.query(db.Books) \
|
|
|
|
.join(ub.BookShelf, ub.BookShelf.book_id == db.Books.id, isouter=True) \
|
2020-12-13 12:54:09 +00:00
|
|
|
.add_columns(calibre_db.common_filters().label("visible")) \
|
|
|
|
.filter(ub.BookShelf.shelf == shelf_id).order_by(ub.BookShelf.order.asc()).all()
|
2019-02-08 19:11:44 +00:00
|
|
|
return render_title_template('shelf_order.html', entries=result,
|
|
|
|
title=_(u"Change order of Shelf: '%(name)s'", name=shelf.name),
|
|
|
|
shelf=shelf, page="shelforder")
|
2020-12-13 12:54:09 +00:00
|
|
|
|
2021-01-03 14:40:17 +00:00
|
|
|
|
2020-12-27 09:24:51 +00:00
|
|
|
def change_shelf_order(shelf_id, order):
|
2021-07-26 05:52:01 +00:00
|
|
|
result = calibre_db.session.query(db.Books).outerjoin(db.books_series_link,
|
|
|
|
db.Books.id == db.books_series_link.c.book)\
|
|
|
|
.outerjoin(db.Series).join(ub.BookShelf, ub.BookShelf.book_id == db.Books.id) \
|
2020-12-27 09:24:51 +00:00
|
|
|
.filter(ub.BookShelf.shelf == shelf_id).order_by(*order).all()
|
|
|
|
for index, entry in enumerate(result):
|
|
|
|
book = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id) \
|
|
|
|
.filter(ub.BookShelf.book_id == entry.id).first()
|
|
|
|
book.order = index
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("Shelf-id:{} - Order changed".format(shelf_id))
|
2020-12-13 12:54:09 +00:00
|
|
|
|
2021-01-03 14:40:17 +00:00
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
def render_show_shelf(shelf_type, shelf_id, page_no, sort_param):
|
2020-12-13 12:54:09 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
|
|
|
|
# check user is allowed to access shelf
|
|
|
|
if shelf and check_shelf_view_permissions(shelf):
|
2020-12-27 09:24:51 +00:00
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
if shelf_type == 1:
|
2020-12-27 09:24:51 +00:00
|
|
|
# order = [ub.BookShelf.order.asc()]
|
|
|
|
if sort_param == 'pubnew':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.pubdate.desc()])
|
|
|
|
if sort_param == 'pubold':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.pubdate])
|
|
|
|
if sort_param == 'abc':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.sort])
|
|
|
|
if sort_param == 'zyx':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.sort.desc()])
|
|
|
|
if sort_param == 'new':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.timestamp.desc()])
|
|
|
|
if sort_param == 'old':
|
|
|
|
change_shelf_order(shelf_id, [db.Books.timestamp])
|
|
|
|
if sort_param == 'authaz':
|
2021-07-26 05:52:01 +00:00
|
|
|
change_shelf_order(shelf_id, [db.Books.author_sort.asc(), db.Series.name, db.Books.series_index])
|
2020-12-27 09:24:51 +00:00
|
|
|
if sort_param == 'authza':
|
2021-07-26 05:52:01 +00:00
|
|
|
change_shelf_order(shelf_id, [db.Books.author_sort.desc(),
|
|
|
|
db.Series.name.desc(),
|
|
|
|
db.Books.series_index.desc()])
|
2020-12-13 17:32:19 +00:00
|
|
|
page = "shelf.html"
|
|
|
|
pagesize = 0
|
|
|
|
else:
|
|
|
|
pagesize = sys.maxsize
|
|
|
|
page = 'shelfdown.html'
|
|
|
|
|
|
|
|
result, __, pagination = calibre_db.fill_indexpage(page_no, pagesize,
|
2021-02-06 23:21:51 +00:00
|
|
|
db.Books,
|
|
|
|
ub.BookShelf.shelf == shelf_id,
|
|
|
|
[ub.BookShelf.order.asc()],
|
|
|
|
ub.BookShelf, ub.BookShelf.book_id == db.Books.id)
|
2020-12-13 12:54:09 +00:00
|
|
|
# delete chelf entries where book is not existent anymore, can happen if book is deleted outside calibre-web
|
2021-02-06 23:21:51 +00:00
|
|
|
wrong_entries = calibre_db.session.query(ub.BookShelf) \
|
|
|
|
.join(db.Books, ub.BookShelf.book_id == db.Books.id, isouter=True) \
|
2020-12-13 17:32:19 +00:00
|
|
|
.filter(db.Books.id == None).all()
|
2020-12-13 12:54:09 +00:00
|
|
|
for entry in wrong_entries:
|
|
|
|
log.info('Not existing book {} in {} deleted'.format(entry.book_id, shelf))
|
|
|
|
try:
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == entry.book_id).delete()
|
|
|
|
ub.session.commit()
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2020-12-13 12:54:09 +00:00
|
|
|
|
2020-12-13 17:32:19 +00:00
|
|
|
return render_title_template(page,
|
|
|
|
entries=result,
|
|
|
|
pagination=pagination,
|
|
|
|
title=_(u"Shelf: '%(name)s'", name=shelf.name),
|
|
|
|
shelf=shelf,
|
|
|
|
page="shelf")
|
2020-12-13 12:54:09 +00:00
|
|
|
else:
|
|
|
|
flash(_(u"Error opening shelf. Shelf does not exist or is not accessible"), category="error")
|
|
|
|
return redirect(url_for("web.index"))
|