diff --git a/cps/db.py b/cps/db.py index 32dd022b..28b21c45 100644 --- a/cps/db.py +++ b/cps/db.py @@ -648,9 +648,10 @@ class CalibreDB(): # read search results from calibre-database and return it (function is used for feed and simple search def get_search_results(self, term, offset=None, order=None, limit=None): order = order or [Books.sort] + pagination = None if offset != None and limit != None: offset = int(offset) - limit = offset + int(limit) + limit_all = offset + int(limit) term.strip().lower() self.session.connection().connection.connection.create_function("lower", 1, lcase) q = list() @@ -665,7 +666,9 @@ class CalibreDB(): func.lower(Books.title).ilike("%" + term + "%") )).order_by(*order).all() result_count = len(result) - return result[offset:limit], result_count + if offset != None and limit != None: + pagination = Pagination((offset / (int(limit)) + 1), limit, result_count) + return result[offset:limit_all], result_count, pagination # Creates for all stored languages a translated speaking name in the array for the UI def speaking_language(self, languages=None): diff --git a/cps/helper.py b/cps/helper.py index 5178a522..b0ea7ff7 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -74,40 +74,33 @@ log = logger.create() def convert_book_format(book_id, calibrepath, old_book_format, new_book_format, user_id, kindle_mail=None): book = calibre_db.get_book(book_id) data = calibre_db.get_book_format(book.id, old_book_format) + file_path = os.path.join(calibrepath, book.path, data.name) if not data: error_message = _(u"%(format)s format not found for book id: %(book)d", format=old_book_format, book=book_id) log.error("convert_book_format: %s", error_message) return error_message if config.config_use_google_drive: - df = gd.getFileFromEbooksFolder(book.path, data.name + "." + old_book_format.lower()) - if df: - datafile = os.path.join(calibrepath, book.path, data.name + u"." + old_book_format.lower()) - if not os.path.exists(os.path.join(calibrepath, book.path)): - os.makedirs(os.path.join(calibrepath, book.path)) - df.GetContentFile(datafile) - else: + if not gd.getFileFromEbooksFolder(book.path, data.name + "." + old_book_format.lower()): error_message = _(u"%(format)s not found on Google Drive: %(fn)s", format=old_book_format, fn=data.name + "." + old_book_format.lower()) return error_message - file_path = os.path.join(calibrepath, book.path, data.name) - if os.path.exists(file_path + "." + old_book_format.lower()): - # read settings and append converter task to queue - if kindle_mail: - settings = config.get_mail_settings() - settings['subject'] = _('Send to Kindle') # pretranslate Subject for e-mail - settings['body'] = _(u'This e-mail has been sent via Calibre-Web.') - # text = _(u"%(format)s: %(book)s", format=new_book_format, book=book.title) - else: - settings = dict() - txt = (u"%s -> %s: %s" % (old_book_format, new_book_format, book.title)) - settings['old_book_format'] = old_book_format - settings['new_book_format'] = new_book_format - WorkerThread.add(user_id, TaskConvert(file_path, book.id, txt, settings, kindle_mail)) - return None else: - error_message = _(u"%(format)s not found: %(fn)s", - format=old_book_format, fn=data.name + "." + old_book_format.lower()) - return error_message + if not os.path.exists(file_path + "." + old_book_format.lower()): + error_message = _(u"%(format)s not found: %(fn)s", + format=old_book_format, fn=data.name + "." + old_book_format.lower()) + return error_message + # read settings and append converter task to queue + if kindle_mail: + settings = config.get_mail_settings() + settings['subject'] = _('Send to Kindle') # pretranslate Subject for e-mail + settings['body'] = _(u'This e-mail has been sent via Calibre-Web.') + else: + settings = dict() + txt = (u"%s -> %s: %s" % (old_book_format, new_book_format, book.title)) + settings['old_book_format'] = old_book_format + settings['new_book_format'] = new_book_format + WorkerThread.add(user_id, TaskConvert(file_path, book.id, txt, settings, kindle_mail, user_id)) + return None def send_test_mail(kindle_mail, user_name): diff --git a/cps/jinjia.py b/cps/jinjia.py index a6df156d..87ba4159 100644 --- a/cps/jinjia.py +++ b/cps/jinjia.py @@ -44,6 +44,8 @@ log = logger.create() def url_for_other_page(page): args = request.view_args.copy() args['page'] = page + for get, val in request.args.items(): + args[get] = val return url_for(request.endpoint, **args) diff --git a/cps/services/worker.py b/cps/services/worker.py index 369522d6..c2ea594c 100644 --- a/cps/services/worker.py +++ b/cps/services/worker.py @@ -210,7 +210,7 @@ class CalibreTask: self._progress = x def _handleError(self, error_message): - log.error(error_message) + log.exception(error_message) self.stat = STAT_FAIL self.progress = 1 self.error = error_message diff --git a/cps/static/js/table.js b/cps/static/js/table.js index cbfe58e7..62f7e220 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -109,6 +109,7 @@ $(function() { $("#books-table").bootstrapTable({ sidePagination: "server", pagination: true, + paginationLoop: false, paginationDetailHAlign: " hidden", paginationHAlign: "left", idField: "id", diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index b6093dbe..e3857389 100644 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -20,34 +20,52 @@ log = logger.create() class TaskConvert(CalibreTask): - def __init__(self, file_path, bookid, taskMessage, settings, kindle_mail): + def __init__(self, file_path, bookid, taskMessage, settings, kindle_mail, user=None): super(TaskConvert, self).__init__(taskMessage) self.file_path = file_path self.bookid = bookid self.settings = settings self.kindle_mail = kindle_mail + self.user = user self.results = dict() def run(self, worker_thread): self.worker_thread = worker_thread + if config.config_use_google_drive: + cur_book = calibre_db.get_book(self.bookid) + data = calibre_db.get_book_format(self.bookid, self.settings['old_book_format']) + df = gdriveutils.getFileFromEbooksFolder(cur_book.path, + data.name + "." + self.settings['old_book_format'].lower()) + if df: + datafile = os.path.join(config.config_calibre_dir, + cur_book.path, + data.name + u"." + self.settings['old_book_format'].lower()) + if not os.path.exists(os.path.join(config.config_calibre_dir, cur_book.path)): + os.makedirs(os.path.join(config.config_calibre_dir, cur_book.path)) + df.GetContentFile(datafile) + else: + error_message = _(u"%(format)s not found on Google Drive: %(fn)s", + format=self.settings['old_book_format'], + fn=data.name + "." + self.settings['old_book_format'].lower()) + return error_message + filename = self._convert_ebook_format() + if config.config_use_google_drive: + os.remove(self.file_path + u'.' + self.settings['old_book_format'].lower()) if filename: if config.config_use_google_drive: + # Upload files to gdrive gdriveutils.updateGdriveCalibreFromLocal() + self._handleSuccess() if self.kindle_mail: # if we're sending to kindle after converting, create a one-off task and run it immediately # todo: figure out how to incorporate this into the progress try: - task = TaskEmail(self.settings['subject'], self.results["path"], + worker_thread.add(self.user, TaskEmail(self.settings['subject'], self.results["path"], filename, self.settings, self.kindle_mail, - self.settings['subject'], self.settings['body'], internal=True) - task.start(worker_thread) - - # even though the convert task might be finished, if this task fails, fail the whole thing - if task.stat != STAT_FINISH_SUCCESS: - raise Exception(task.error) + self.settings['subject'], self.settings['body'], internal=True)) except Exception as e: return self._handleError(str(e)) @@ -101,9 +119,8 @@ class TaskConvert(CalibreTask): return self.results['path'] = cur_book.path self.results['title'] = cur_book.title - if config.config_use_google_drive: - os.remove(file_path + format_old_ext) - self._handleSuccess() + if not config.config_use_google_drive: + self._handleSuccess() return os.path.basename(file_path + format_new_ext) else: error_message = _('%(format)s format not found on disk', format=format_new_ext.upper()) @@ -179,6 +196,8 @@ class TaskConvert(CalibreTask): progress = re.search(r"(\d+)%\s.*", nextline) if progress: self.progress = int(progress.group(1)) / 100 + if config.config_use_google_drive: + self.progress *= 0.9 # process returncode check = p.returncode diff --git a/cps/tasks/mail.py b/cps/tasks/mail.py index f83cc4fa..ac3ec424 100644 --- a/cps/tasks/mail.py +++ b/cps/tasks/mail.py @@ -169,7 +169,7 @@ class TaskEmail(CalibreTask): except (MemoryError) as e: log.exception(e) self._handleError(u'MemoryError sending email: ' + str(e)) - return None + # return None except (smtplib.SMTPException, smtplib.SMTPAuthenticationError) as e: if hasattr(e, "smtp_error"): text = e.smtp_error.decode('utf-8').replace("\n", '. ') @@ -181,10 +181,11 @@ class TaskEmail(CalibreTask): log.exception(e) text = '' self._handleError(u'Smtplib Error sending email: ' + text) - return None + # return None except (socket.error) as e: self._handleError(u'Socket Error sending email: ' + e.strerror) - return None + # return None + @property def progress(self): diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 01f6772d..16c02fae 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -12,7 +12,9 @@ {% if book.data|length > 1 %}