1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-26 01:50:31 +00:00

Improvements for sqlalchemy 2

This commit is contained in:
Ozzie Isaacs 2023-04-13 19:01:53 +02:00
parent 6c6841f8b0
commit 1c8bc78b48
4 changed files with 49 additions and 46 deletions

View File

@ -48,7 +48,7 @@ import requests
from . import config, logger, kobo_auth, db, calibre_db, helper, shelf as shelf_lib, ub, csrf, kobo_sync_status from . import config, logger, kobo_auth, db, calibre_db, helper, shelf as shelf_lib, ub, csrf, kobo_sync_status
from . import isoLanguages from . import isoLanguages
from .epub import get_epub_layout from .epub import get_epub_layout
from .constants import sqlalchemy_version2, COVER_THUMBNAIL_SMALL from .constants import COVER_THUMBNAIL_SMALL #, sqlalchemy_version2
from .helper import get_download_link from .helper import get_download_link
from .services import SyncToken as SyncToken from .services import SyncToken as SyncToken
from .web import download_required from .web import download_required
@ -165,16 +165,16 @@ def HandleSyncRequest():
only_kobo_shelves = current_user.kobo_only_shelves_sync only_kobo_shelves = current_user.kobo_only_shelves_sync
if only_kobo_shelves: if only_kobo_shelves:
if sqlalchemy_version2: #if sqlalchemy_version2:
changed_entries = select(db.Books, # changed_entries = select(db.Books,
ub.ArchivedBook.last_modified, # ub.ArchivedBook.last_modified,
ub.BookShelf.date_added, # ub.BookShelf.date_added,
ub.ArchivedBook.is_archived) # ub.ArchivedBook.is_archived)
else: #else:
changed_entries = calibre_db.session.query(db.Books, changed_entries = calibre_db.session.query(db.Books,
ub.ArchivedBook.last_modified, ub.ArchivedBook.last_modified,
ub.BookShelf.date_added, ub.BookShelf.date_added,
ub.ArchivedBook.is_archived) ub.ArchivedBook.is_archived)
changed_entries = (changed_entries changed_entries = (changed_entries
.join(db.Data).outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id, .join(db.Data).outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id,
ub.ArchivedBook.user_id == current_user.id)) ub.ArchivedBook.user_id == current_user.id))
@ -191,12 +191,12 @@ def HandleSyncRequest():
.filter(ub.Shelf.kobo_sync) .filter(ub.Shelf.kobo_sync)
.distinct()) .distinct())
else: else:
if sqlalchemy_version2: #if sqlalchemy_version2:
changed_entries = select(db.Books, ub.ArchivedBook.last_modified, ub.ArchivedBook.is_archived) # changed_entries = select(db.Books, ub.ArchivedBook.last_modified, ub.ArchivedBook.is_archived)
else: #else:
changed_entries = calibre_db.session.query(db.Books, changed_entries = calibre_db.session.query(db.Books,
ub.ArchivedBook.last_modified, ub.ArchivedBook.last_modified,
ub.ArchivedBook.is_archived) ub.ArchivedBook.is_archived)
changed_entries = (changed_entries changed_entries = (changed_entries
.join(db.Data).outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id, .join(db.Data).outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id,
ub.ArchivedBook.user_id == current_user.id)) ub.ArchivedBook.user_id == current_user.id))
@ -208,10 +208,10 @@ def HandleSyncRequest():
.order_by(db.Books.id)) .order_by(db.Books.id))
reading_states_in_new_entitlements = [] reading_states_in_new_entitlements = []
if sqlalchemy_version2: #if sqlalchemy_version2:
books = calibre_db.session.execute(changed_entries.limit(SYNC_ITEM_LIMIT)) # books = calibre_db.session.execute(changed_entries.limit(SYNC_ITEM_LIMIT))
else: #else:
books = changed_entries.limit(SYNC_ITEM_LIMIT) books = changed_entries.limit(SYNC_ITEM_LIMIT)
log.debug("Books to Sync: {}".format(len(books.all()))) log.debug("Books to Sync: {}".format(len(books.all())))
for book in books: for book in books:
formats = [data.format for data in book.Books.data] formats = [data.format for data in book.Books.data]
@ -229,7 +229,7 @@ def HandleSyncRequest():
new_reading_state_last_modified = max(new_reading_state_last_modified, kobo_reading_state.last_modified) new_reading_state_last_modified = max(new_reading_state_last_modified, kobo_reading_state.last_modified)
reading_states_in_new_entitlements.append(book.Books.id) reading_states_in_new_entitlements.append(book.Books.id)
ts_created = book.Books.timestamp ts_created = book.Books.timestamp.replace(tzinfo=None)
try: try:
ts_created = max(ts_created, book.date_added) ts_created = max(ts_created, book.date_added)
@ -242,7 +242,7 @@ def HandleSyncRequest():
sync_results.append({"ChangedEntitlement": entitlement}) sync_results.append({"ChangedEntitlement": entitlement})
new_books_last_modified = max( new_books_last_modified = max(
book.Books.last_modified, new_books_last_modified book.Books.last_modified.replace(tzinfo=None), new_books_last_modified
) )
try: try:
new_books_last_modified = max( new_books_last_modified = max(
@ -254,27 +254,27 @@ def HandleSyncRequest():
new_books_last_created = max(ts_created, new_books_last_created) new_books_last_created = max(ts_created, new_books_last_created)
kobo_sync_status.add_synced_books(book.Books.id) kobo_sync_status.add_synced_books(book.Books.id)
if sqlalchemy_version2: '''if sqlalchemy_version2:
max_change = calibre_db.session.execute(changed_entries max_change = calibre_db.session.execute(changed_entries
.filter(ub.ArchivedBook.is_archived) .filter(ub.ArchivedBook.is_archived)
.filter(ub.ArchivedBook.user_id == current_user.id) .filter(ub.ArchivedBook.user_id == current_user.id)
.order_by(func.datetime(ub.ArchivedBook.last_modified).desc()))\ .order_by(func.datetime(ub.ArchivedBook.last_modified).desc()))\
.columns(db.Books).first() .columns(db.Books).first()
else: else:'''
max_change = changed_entries.from_self().filter(ub.ArchivedBook.is_archived)\ max_change = changed_entries.filter(ub.ArchivedBook.is_archived)\
.filter(ub.ArchivedBook.user_id == current_user.id) \ .filter(ub.ArchivedBook.user_id == current_user.id) \
.order_by(func.datetime(ub.ArchivedBook.last_modified).desc()).first() .order_by(func.datetime(ub.ArchivedBook.last_modified).desc()).first()
max_change = max_change.last_modified if max_change else new_archived_last_modified max_change = max_change.last_modified if max_change else new_archived_last_modified
new_archived_last_modified = max(new_archived_last_modified, max_change) new_archived_last_modified = max(new_archived_last_modified, max_change)
# no. of books returned # no. of books returned
if sqlalchemy_version2: '''if sqlalchemy_version2:
entries = calibre_db.session.execute(changed_entries).all() entries = calibre_db.session.execute(changed_entries).all()
book_count = len(entries) book_count = len(entries)
else: else:'''
book_count = changed_entries.count() book_count = changed_entries.count()
# last entry: # last entry:
cont_sync = bool(book_count) cont_sync = bool(book_count)
log.debug("Remaining books to Sync: {}".format(book_count)) log.debug("Remaining books to Sync: {}".format(book_count))
@ -716,20 +716,20 @@ def sync_shelves(sync_token, sync_results, only_kobo_shelves=False):
}) })
extra_filters.append(ub.Shelf.kobo_sync) extra_filters.append(ub.Shelf.kobo_sync)
if sqlalchemy_version2: '''if sqlalchemy_version2:
shelflist = ub.session.execute(select(ub.Shelf).outerjoin(ub.BookShelf).filter( shelflist = ub.session.execute(select(ub.Shelf).outerjoin(ub.BookShelf).filter(
or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified, or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified,
func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified), func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified),
ub.Shelf.user_id == current_user.id, ub.Shelf.user_id == current_user.id,
*extra_filters *extra_filters
).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc())).columns(ub.Shelf) ).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc())).columns(ub.Shelf)
else: else:'''
shelflist = ub.session.query(ub.Shelf).outerjoin(ub.BookShelf).filter( shelflist = ub.session.query(ub.Shelf).outerjoin(ub.BookShelf).filter(
or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified, or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified,
func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified), func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified),
ub.Shelf.user_id == current_user.id, ub.Shelf.user_id == current_user.id,
*extra_filters *extra_filters
).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc()) ).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc())
for shelf in shelflist: for shelf in shelflist:
if not shelf_lib.check_shelf_view_permissions(shelf): if not shelf_lib.check_shelf_view_permissions(shelf):

View File

@ -20,7 +20,7 @@
import sys import sys
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from jsonschema import validate, exceptions, __version__ from jsonschema import validate, exceptions, __version__
from datetime import datetime from datetime import datetime, timezone
from urllib.parse import unquote from urllib.parse import unquote

View File

@ -295,11 +295,14 @@ def check_shelf_edit_permissions(cur_shelf):
def check_shelf_view_permissions(cur_shelf): def check_shelf_view_permissions(cur_shelf):
if cur_shelf.is_public: try:
return True if cur_shelf.is_public:
if current_user.is_anonymous or cur_shelf.user_id != current_user.id: return True
log.error("User is unauthorized to view non-public shelf: {}".format(cur_shelf.name)) if current_user.is_anonymous or cur_shelf.user_id != current_user.id:
return False log.error("User is unauthorized to view non-public shelf: {}".format(cur_shelf.name))
return False
except Exception as e:
log.error(e)
return True return True

View File

@ -34,7 +34,7 @@
<div class="col-xs-10 col-sm-10 col-md-11"><a id="list_{{loop.index0}}" href="{% if entry.format %}{{url_for('web.books_list', data=data, sort_param='stored', book_id=entry.format )}}{% else %}{{url_for('web.books_list', data=data, sort_param='stored', book_id=entry[0].id )}}{% endif %}"> <div class="col-xs-10 col-sm-10 col-md-11"><a id="list_{{loop.index0}}" href="{% if entry.format %}{{url_for('web.books_list', data=data, sort_param='stored', book_id=entry.format )}}{% else %}{{url_for('web.books_list', data=data, sort_param='stored', book_id=entry[0].id )}}{% endif %}">
{% if entry.name %} {% if entry.name %}
<div class="rating"> <div class="rating">
{% for number in range(entry.name) %} {% for number in range(entry.name|int) %}
<span class="glyphicon glyphicon-star good"></span> <span class="glyphicon glyphicon-star good"></span>
{% if loop.last and loop.index < 5 %} {% if loop.last and loop.index < 5 %}
{% for numer in range(5 - loop.index) %} {% for numer in range(5 - loop.index) %}