From ed1d51bf6bcebb397c181247f3d3309760019ce1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 13 Jun 2016 09:05:10 +0530 Subject: [PATCH] Re-render the book on the server when reloading --- src/calibre/srv/books.py | 3 +++ src/pyj/read_book/db.pyj | 4 ---- src/pyj/read_book/ui.pyj | 20 +++++++++++--------- src/pyj/read_book/view.pyj | 1 + 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/calibre/srv/books.py b/src/calibre/srv/books.py index 8819815548..10a2c672bd 100644 --- a/src/calibre/srv/books.py +++ b/src/calibre/srv/books.py @@ -117,6 +117,7 @@ def job_done(job): @endpoint('/book-manifest/{book_id}/{fmt}', postprocess=json, types={'book_id':int}) def book_manifest(ctx, rd, book_id, fmt): db, library_id = get_library_data(ctx, rd)[:2] + force_reload = rd.query.get('force_reload') == '1' if plugin_for_input_format(fmt) is None: raise HTTPNotFound('The format %s cannot be viewed' % fmt.upper()) if book_id not in ctx.allowed_book_ids(rd, db): @@ -129,6 +130,8 @@ def book_manifest(ctx, rd, book_id, fmt): bhash = book_hash(db.library_id, book_id, fmt, size, mtime) with cache_lock: mpath = abspath(os.path.join(books_cache_dir(), 'f', bhash, 'calibre-book-manifest.json')) + if force_reload: + safe_remove(mpath, True) try: os.utime(mpath, None) with lopen(mpath, 'rb') as f: diff --git a/src/pyj/read_book/db.pyj b/src/pyj/read_book/db.pyj index 509f318ee9..cbf379e68c 100644 --- a/src/pyj/read_book/db.pyj +++ b/src/pyj/read_book/db.pyj @@ -5,8 +5,6 @@ from __python__ import hash_literals from gettext import gettext as _ from encodings import base64encode, base64decode -FORCE_BOOK_RELOAD = False # set to True to force book reloading - def upgrade_schema(idb, old_version, new_version): print('upgrade_schema:', old_version, new_version) if not idb.objectStoreNames.contains('books'): @@ -82,8 +80,6 @@ class DB: # refer to non-serializable objects like functions. key = v'[self.interface_data.library_id, book_id, fmt]' self.do_op(['books'], key, _('Failed to read from the books database'), def(result): - if FORCE_BOOK_RELOAD: - result = None proceed(result or { 'key':key, 'is_complete':False, diff --git a/src/pyj/read_book/ui.pyj b/src/pyj/read_book/ui.pyj index 769579a8bc..46efbe3564 100644 --- a/src/pyj/read_book/ui.pyj +++ b/src/pyj/read_book/ui.pyj @@ -95,17 +95,17 @@ class ReadUI: div = document.getElementById(self.progress_id) div.lastChild.textContent = msg or '' - def load_book(self, book_id, fmt, metadata): + def load_book(self, book_id, fmt, metadata, force_reload): self.base_url_data = {'book_id':book_id, 'fmt':fmt} if self.db is None: - self.pending_load = [book_id, fmt, metadata] + self.pending_load = [book_id, fmt, metadata, force_reload] return - self.start_load(book_id, fmt, metadata) + self.start_load(book_id, fmt, metadata, force_reload) def reload_book(self): book_id = self.base_url_data.book_id metadata = self.metadata or self.interface_data.metadata[book_id] - self.load_book(book_id, self.base_url_data.fmt, metadata) + self.load_book(book_id, self.base_url_data.fmt, metadata, True) @property def url_data(self): @@ -121,7 +121,7 @@ class ReadUI: pl, self.pending_load = self.pending_load, None self.start_load(*pl) - def start_load(self, book_id, fmt, metadata): + def start_load(self, book_id, fmt, metadata, force_reload): self.current_book_id = book_id metadata = metadata or self.interface_data.metadata[book_id] self.current_metadata = metadata or {'title':_('Book id #') + book_id} @@ -130,21 +130,23 @@ class ReadUI: if type(self.db) is 'string': self.show_error(_('Cannot read book'), self.db) return - self.db.get_book(book_id, fmt, metadata, self.got_book.bind(self)) + self.db.get_book(book_id, fmt, metadata, self.got_book.bind(self, force_reload)) - def got_book(self, book): + def got_book(self, force_reload, book): if not book.manifest or book.manifest.version is not RENDER_VERSION or not book.is_complete: # We re-download the manifest when the book is not complete to ensure we have the # correct manifest, even though doing so is not strictly necessary - self.get_manifest(book) + self.get_manifest(book, force_reload) else: self.display_book(book) - def get_manifest(self, book): + def get_manifest(self, book, force_reload): library_id, book_id, fmt = book.key if self.manifest_xhr: self.manifest_xhr.abort() query = {'library_id': library_id} + if force_reload: + query.force_reload = '1' self.manifest_xhr = ajax(('book-manifest/' + encodeURIComponent(book_id) + '/' + encodeURIComponent(fmt)), self.got_manifest.bind(self, book), query=query) self.manifest_xhr.send() diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 38f340aaff..084c3a3429 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -157,6 +157,7 @@ class View: def display_book(self, book): self.book = book self.ui.db.update_last_read_time(book) + self.loaded_resources = {} pos = {'replace_history':True} unkey = username_key(self.ui.interface_data.username) name = book.manifest.spine[0]