Commit 5d34fd7f authored by bodybybuddha's avatar bodybybuddha

Send to Kindle button precheck added

  Based on existing book formats and which converter (if any) determine if button can be seen.
parent 5a2ed58d
...@@ -108,6 +108,65 @@ def send_registration_mail(e_mail, user_name, default_password, resend=False): ...@@ -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) e_mail, user_name, _(u"Registration e-mail for user: %(name)s", name=user_name),text)
return 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: # Files are processed in the following order/priority:
# 1: If Mobi file is exisiting, it's directly send to kindle email, # 1: If Mobi file is exisiting, it's directly send to kindle email,
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
</div> </div>
{% endif %} {% endif %}
{% 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%}
<a href="{{url_for('send_to_kindle', book_id=entry.id)}}" id="sendbtn" class="btn btn-primary" role="button"><span class="glyphicon glyphicon-send"></span> {{_('Send to Kindle')}}</a> <a href="{{url_for('send_to_kindle', book_id=entry.id)}}" id="sendbtn" class="btn btn-primary" role="button"><span class="glyphicon glyphicon-send"></span> {{_('Send to Kindle')}}</a>
{% endif %} {% endif %}
{% if entry.data|length %} {% if entry.data|length %}
......
...@@ -1586,9 +1586,11 @@ def show_book(book_id): ...@@ -1586,9 +1586,11 @@ def show_book(book_id):
entries.tags = sort(entries.tags, key = lambda tag: tag.name) 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, return render_title_template('detail.html', entry=entries, cc=cc, is_xhr=request.is_xhr,
title=entries.title, books_shelfs=book_in_shelfs, 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: else:
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error") flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
return redirect(url_for("index")) return redirect(url_for("index"))
...@@ -3846,8 +3848,9 @@ def upload(): ...@@ -3846,8 +3848,9 @@ def upload():
return render_title_template('book_edit.html', book=book, authors=author_names, return render_title_template('book_edit.html', book=book, authors=author_names,
cc=cc, title=_(u"edit metadata"), page="upload") cc=cc, title=_(u"edit metadata"), page="upload")
book_in_shelfs = [] book_in_shelfs = []
flg_send_to_kindle = helper.chk_send_to_kindle(book_id)
return render_title_template('detail.html', entry=book, cc=cc, 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")) return redirect(url_for("index"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment