|
|
|
@ -33,27 +33,9 @@ from . import calibre_db, config, db, logger, ub
|
|
|
|
|
from .render_template import render_title_template
|
|
|
|
|
from .usermanagement import login_required_if_no_ano
|
|
|
|
|
|
|
|
|
|
shelf = Blueprint('shelf', __name__)
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {} not allowed to edit shelf: {}".format(current_user.id, cur_shelf.name))
|
|
|
|
|
return False
|
|
|
|
|
if cur_shelf.is_public and not current_user.role_edit_shelfs():
|
|
|
|
|
log.info("User {} not allowed to edit public shelves".format(current_user.id))
|
|
|
|
|
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: {}".format(cur_shelf.name))
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
shelf = Blueprint('shelf', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/add/<int:shelf_id>/<int:book_id>", methods=["POST"])
|
|
|
|
@ -238,6 +220,89 @@ def edit_shelf(shelf_id):
|
|
|
|
|
return create_edit_shelf(shelf, page_title=_(u"Edit a shelf"), page="shelfedit", shelf_id=shelf_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/delete/<int:shelf_id>", methods=["POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def delete_shelf(shelf_id):
|
|
|
|
|
cur_shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
|
try:
|
|
|
|
|
if not delete_shelf_helper(cur_shelf):
|
|
|
|
|
flash(_("Error deleting Shelf"), category="error")
|
|
|
|
|
else:
|
|
|
|
|
flash(_("Shelf successfully deleted"), category="success")
|
|
|
|
|
except InvalidRequestError as e:
|
|
|
|
|
ub.session.rollback()
|
|
|
|
|
log.error_or_exception("Settings Database error: {}".format(e))
|
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
|
|
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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>")
|
|
|
|
|
@login_required_if_no_ano
|
|
|
|
|
def show_shelf(shelf_id, sort_param, page):
|
|
|
|
|
return render_show_shelf(1, shelf_id, page, sort_param)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/order/<int:shelf_id>", methods=["GET", "POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def order_shelf(shelf_id):
|
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
|
if shelf and check_shelf_view_permissions(shelf):
|
|
|
|
|
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
|
|
|
|
|
# if order diffrent from before -> shelf.last_modified = datetime.utcnow()
|
|
|
|
|
try:
|
|
|
|
|
ub.session.commit()
|
|
|
|
|
except (OperationalError, InvalidRequestError) as e:
|
|
|
|
|
ub.session.rollback()
|
|
|
|
|
log.error_or_exception("Settings Database error: {}".format(e))
|
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
|
|
|
|
|
|
|
|
|
|
result = list()
|
|
|
|
|
if shelf:
|
|
|
|
|
result = calibre_db.session.query(db.Books) \
|
|
|
|
|
.join(ub.BookShelf, ub.BookShelf.book_id == db.Books.id, isouter=True) \
|
|
|
|
|
.add_columns(calibre_db.common_filters().label("visible")) \
|
|
|
|
|
.filter(ub.BookShelf.shelf == shelf_id).order_by(ub.BookShelf.order.asc()).all()
|
|
|
|
|
return render_title_template('shelf_order.html', entries=result,
|
|
|
|
|
title=_(u"Change order of Shelf: '%(name)s'", name=shelf.name),
|
|
|
|
|
shelf=shelf, page="shelforder")
|
|
|
|
|
else:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {} not allowed to edit shelf: {}".format(current_user.id, cur_shelf.name))
|
|
|
|
|
return False
|
|
|
|
|
if cur_shelf.is_public and not current_user.role_edit_shelfs():
|
|
|
|
|
log.info("User {} not allowed to edit public shelves".format(current_user.id))
|
|
|
|
|
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: {}".format(cur_shelf.name))
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if shelf ID is set, we are editing a shelf
|
|
|
|
|
def create_edit_shelf(shelf, page_title, page, shelf_id=False):
|
|
|
|
|
sync_only_selected_shelves = current_user.kobo_only_shelves_sync
|
|
|
|
@ -255,7 +320,7 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
|
|
|
|
|
ub.ShelfArchive.uuid == shelf.uuid).delete()
|
|
|
|
|
ub.session_commit()
|
|
|
|
|
shelf_title = to_save.get("title", "")
|
|
|
|
|
if check_shelf_is_unique(shelf, shelf_title, is_public, shelf_id):
|
|
|
|
|
if check_shelf_is_unique(shelf_title, is_public, shelf_id):
|
|
|
|
|
shelf.name = shelf_title
|
|
|
|
|
shelf.is_public = is_public
|
|
|
|
|
if not shelf_id:
|
|
|
|
@ -288,7 +353,7 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
|
|
|
|
|
sync_only_selected_shelves=sync_only_selected_shelves)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_shelf_is_unique(shelf, title, is_public, shelf_id=False):
|
|
|
|
|
def check_shelf_is_unique(title, is_public, shelf_id=False):
|
|
|
|
|
if shelf_id:
|
|
|
|
|
ident = ub.Shelf.id != shelf_id
|
|
|
|
|
else:
|
|
|
|
@ -328,70 +393,6 @@ def delete_shelf_helper(cur_shelf):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/delete/<int:shelf_id>", methods=["POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def delete_shelf(shelf_id):
|
|
|
|
|
cur_shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
|
try:
|
|
|
|
|
if not delete_shelf_helper(cur_shelf):
|
|
|
|
|
flash(_("Error deleting Shelf"), category="error")
|
|
|
|
|
else:
|
|
|
|
|
flash(_("Shelf successfully deleted"), category="success")
|
|
|
|
|
except InvalidRequestError as e:
|
|
|
|
|
ub.session.rollback()
|
|
|
|
|
log.error_or_exception("Settings Database error: {}".format(e))
|
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
|
|
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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>")
|
|
|
|
|
@login_required_if_no_ano
|
|
|
|
|
def show_shelf(shelf_id, sort_param, page):
|
|
|
|
|
return render_show_shelf(1, shelf_id, page, sort_param)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shelf.route("/shelf/order/<int:shelf_id>", methods=["GET", "POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def order_shelf(shelf_id):
|
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first()
|
|
|
|
|
if shelf and check_shelf_view_permissions(shelf):
|
|
|
|
|
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
|
|
|
|
|
# if order diffrent from before -> shelf.last_modified = datetime.utcnow()
|
|
|
|
|
try:
|
|
|
|
|
ub.session.commit()
|
|
|
|
|
except (OperationalError, InvalidRequestError) as e:
|
|
|
|
|
ub.session.rollback()
|
|
|
|
|
log.error_or_exception("Settings Database error: {}".format(e))
|
|
|
|
|
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
|
|
|
|
|
|
|
|
|
|
result = list()
|
|
|
|
|
if shelf:
|
|
|
|
|
result = calibre_db.session.query(db.Books) \
|
|
|
|
|
.join(ub.BookShelf, ub.BookShelf.book_id == db.Books.id, isouter=True) \
|
|
|
|
|
.add_columns(calibre_db.common_filters().label("visible")) \
|
|
|
|
|
.filter(ub.BookShelf.shelf == shelf_id).order_by(ub.BookShelf.order.asc()).all()
|
|
|
|
|
return render_title_template('shelf_order.html', entries=result,
|
|
|
|
|
title=_(u"Change order of Shelf: '%(name)s'", name=shelf.name),
|
|
|
|
|
shelf=shelf, page="shelforder")
|
|
|
|
|
else:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def change_shelf_order(shelf_id, order):
|
|
|
|
|
result = calibre_db.session.query(db.Books).outerjoin(db.books_series_link,
|
|
|
|
|
db.Books.id == db.books_series_link.c.book)\
|
|
|
|
|