From 5d34fd7fec0468812cbee91bca46987cc6d01166 Mon Sep 17 00:00:00 2001 From: bodybybuddha Date: Mon, 1 Oct 2018 14:19:29 -0400 Subject: [PATCH 1/4] Send to Kindle button precheck added Based on existing book formats and which converter (if any) determine if button can be seen. --- cps/helper.py | 59 +++++++++++++++++++++++++++++++++++++++ cps/templates/detail.html | 2 +- cps/web.py | 7 +++-- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index 722a6245..5aaeb988 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -108,6 +108,65 @@ def send_registration_mail(e_mail, user_name, default_password, resend=False): e_mail, user_name, _(u"Registration e-mail for user: %(name)s", name=user_name),text) return +def chk_send_to_kindle(book_id): + ''' + Used to determine if we can show the Send to Kindle button. + Specifically checks the existing book formats and the conversion options available. + + mobi = true + epub && kindlegen or ebookconvert = true + all valid 'book' format && ebookconvert = true + all other combinations = false + ''' + book = db.session.query(db.Books).filter(db.Books.id == book_id).first() + data = db.session.query(db.Data).filter(db.Data.book == book.id).all() + if data: + bookformats = get_formats_from_book(data) + + if ub.config.config_ebookconverter == 0: + # no converter - only allow for mobi and pdf formats + if 'MOBI' in bookformats or 'PDF' in bookformats: + return True + else: + return False + else: + if ub.config.config_ebookconverter == 1: + # the converter is kindlegen - only allow epub + if 'EPUB' in bookformats: + return True + else: + return False + + if ub.config.config_ebookconverter == 2: + # the converter is ebook-convert - allow for any allowable 'book' format + formatcount = 0 + for bookformat in bookformats: + if bookformat.lower() in web.EXTENSIONS_CONVERT: + formatcount += 1 + + if formatcount > 0: + return True + else: + return False + else: + return False + + return False + else: + app.logger.error(u'Cannot find book entry %d', book_id) + return False + +def get_formats_from_book(data): + ''' + data s/b the data member of db.entry + returns a list of formats + ''' + formatlist=[] + for entry in data: + formatlist.append(entry.format.upper()) + + return formatlist + # Files are processed in the following order/priority: # 1: If Mobi file is exisiting, it's directly send to kindle email, diff --git a/cps/templates/detail.html b/cps/templates/detail.html index 420b98a6..0269ad38 100644 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -40,7 +40,7 @@ {% endif %} {% endif %} - {% if g.user.kindle_mail and g.user.is_authenticated %} + {% if g.user.kindle_mail and g.user.is_authenticated and flg_kindle%} {{_('Send to Kindle')}} {% endif %} {% if entry.data|length %} diff --git a/cps/web.py b/cps/web.py index 2d1e5f3f..3673e0aa 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1586,9 +1586,11 @@ def show_book(book_id): entries.tags = sort(entries.tags, key = lambda tag: tag.name) + flg_send_to_kindle = helper.chk_send_to_kindle(book_id) + return render_title_template('detail.html', entry=entries, cc=cc, is_xhr=request.is_xhr, title=entries.title, books_shelfs=book_in_shelfs, - have_read=have_read, page="book") + have_read=have_read, flg_kindle=flg_send_to_kindle, page="book") else: flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error") return redirect(url_for("index")) @@ -3846,8 +3848,9 @@ def upload(): return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc, title=_(u"edit metadata"), page="upload") book_in_shelfs = [] + flg_send_to_kindle = helper.chk_send_to_kindle(book_id) return render_title_template('detail.html', entry=book, cc=cc, - title=book.title, books_shelfs=book_in_shelfs, page="upload") + title=book.title, books_shelfs=book_in_shelfs, flg_kindle=flg_send_to_kindle, page="upload") return redirect(url_for("index")) From 1144d97bc15425d99f68be47979af912dd969956 Mon Sep 17 00:00:00 2001 From: bodybybuddha Date: Mon, 1 Oct 2018 15:34:16 -0400 Subject: [PATCH 2/4] Fixed updating new book format to be all caps in database This keeps us inline with calibre's behavior. --- cps/worker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cps/worker.py b/cps/worker.py index 02ca742f..21914a3d 100644 --- a/cps/worker.py +++ b/cps/worker.py @@ -320,7 +320,7 @@ class WorkerThread(threading.Thread): cur_book = web.db.session.query(web.db.Books).filter(web.db.Books.id == bookid).first() if os.path.isfile(file_path + format_new_ext): new_format = web.db.Data(name=cur_book.data[0].name, - book_format=self.queue[self.current]['settings']['new_book_format'], + book_format=self.queue[self.current]['settings']['new_book_format'].upper(), book=bookid, uncompressed_size=os.path.getsize(file_path + format_new_ext)) cur_book.data.append(new_format) web.db.session.commit() From 46b2c9919a0fa0c4bf4857f11d7771978e01163b Mon Sep 17 00:00:00 2001 From: bodybybuddha Date: Mon, 1 Oct 2018 15:35:51 -0400 Subject: [PATCH 3/4] Fix for #295 Allow for azw and azw3 files to be "sent" to kindle - these books are actually converted to mobi first --- cps/helper.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cps/helper.py b/cps/helper.py index 5aaeb988..aafb696f 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -185,6 +185,10 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id): formats["epub"] = entry.name + ".epub" if entry.format == "PDF": formats["pdf"] = entry.name + ".pdf" + if entry.format == "AZW": + formats["azw"] = entry.name + ".azw" + if entry.format == "AZW3": + formats["azw3"] = entry.name + ".azw3" if len(formats) == 0: return _(u"Could not find any formats suitable for sending by e-mail") @@ -194,6 +198,12 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id): elif 'epub' in formats: # returns None if sucess, otherwise errormessage return convert_book_format(book_id, calibrepath, u'epub', u'mobi', user_id, kindle_mail) + elif 'azw3' in formats: + # returns None if sucess, otherwise errormessage + return convert_book_format(book_id, calibrepath, u'azw3', u'mobi', user_id, kindle_mail) + elif 'azw' in formats: + # returns None if sucess, otherwise errormessage + return convert_book_format(book_id, calibrepath, u'azw', u'mobi', user_id, kindle_mail) elif 'pdf' in formats: result = formats['pdf'] # worker.get_attachment() else: From d8107fb50ffbecb90b3afa54148fbb43c02c09cc Mon Sep 17 00:00:00 2001 From: bodybybuddha Date: Tue, 2 Oct 2018 10:22:01 -0400 Subject: [PATCH 4/4] Addressed Codacy trailing space items ... again --- cps/helper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index aafb696f..e621679f 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -142,16 +142,16 @@ def chk_send_to_kindle(book_id): formatcount = 0 for bookformat in bookformats: if bookformat.lower() in web.EXTENSIONS_CONVERT: - formatcount += 1 + formatcount += 1 - if formatcount > 0: + if formatcount > 0: return True else: return False else: return False - return False + return False else: app.logger.error(u'Cannot find book entry %d', book_id) return False