mock ebook for stk

pull/1/head
eric 2015-07-01 22:06:33 -04:00
parent 030e914277
commit df2706993d
2 changed files with 17 additions and 17 deletions

View File

@ -1,4 +1,4 @@
{% load url from future %}{% ifequal message 0 %}It turns out that this ebook is too big for us to send by email. Using a computer, <a href="{{ebook_url}}">download the file</a>. Plug your Kindle into your computer using a USB cable. It will mount as a filesystem. Drag the downloaded {{ebook_format}} file into the documents folder on the Kindle, and then unmount the Kindle. (Kindle 1 owners: youll also need to unplug the USB cable to get your Kindle back into book-reading mode. Kindle 2 owners can leave the cable connected).
{% load url from future %}{% ifequal message 0 %}It turns out that this ebook is too big for us to send by email. Using a computer, <a href="{{ebook.url}}">download the file</a>. Plug your Kindle into your computer using a USB cable. It will mount as a filesystem. Drag the downloaded {{ebook.format}} file into the documents folder on the Kindle, and then unmount the Kindle. (Kindle 1 owners: youll also need to unplug the USB cable to get your Kindle back into book-reading mode. Kindle 2 owners can leave the cable connected).
{% endifequal %}
{% ifequal message 1 %}Well, this is awkward. We can't seem to email that. Please download it using the instructions for your device, and we'll look into the error.
{% endifequal %}

View File

@ -3217,11 +3217,15 @@ def send_to_kindle(request, work_id, javascript='0'):
break
if acq:
ebook_url = acq.get_mobi_url()
ebook_format = 'mobi'
filesize = None
class ebook:
# mock ebook class
# TODO save a filesize so that send to kindle is more graceful
url = acq.get_mobi_url()
format = 'mobi'
def save(self):
return true
filesize = 0
title = acq.work.kindle_safe_title()
ebook=None
else:
non_google_ebooks = work.ebooks().exclude(provider='Google Books')
@ -3235,13 +3239,9 @@ def send_to_kindle(request, work_id, javascript='0'):
# don't forget to increment the download counter!
ebook.increment()
ebook_url = ebook.url
ebook_format = ebook.format
filesize = ebook.filesize
logger.info('ebook: {0}, user_ip: {1}'.format(work_id, request.META['REMOTE_ADDR']))
title = ebook.edition.work.kindle_safe_title()
context['ebook_url']=ebook_url
context['ebook_format']=ebook_format
context['ebook'] = ebook
if request.POST.has_key('kindle_email'):
kindle_email = request.POST['kindle_email']
@ -3268,24 +3268,24 @@ def send_to_kindle(request, work_id, javascript='0'):
This won't perfectly measure size of email, but should be safe, and is much faster than doing the check after download.
"""
filehandle = urllib.urlopen(ebook_url)
if not filesize:
if not ebook.filesize:
try:
filesize = int(filehandle.info().getheaders("Content-Length")[0])
if ebook:
ebook.filesize = filesize if filesize < 2147483647 else 2147483647 # largest safe positive integer
ebook.filesize = int(filehandle.info().getheaders("Content-Length")[0])
if ebook.save:
ebook.filesize = ebook.filesize if ebook.filesize < 2147483647 else 2147483647 # largest safe positive integer
ebook.save()
except IndexError:
# response has no Content-Length header probably a bad link
logger.error('Bad link error: %s', ebook_url)
logger.error('Bad link error: %s', ebook.url)
return local_response(request, javascript, context, 4)
if filesize > models.send_to_kindle_limit:
if ebook.filesize > models.send_to_kindle_limit:
logger.info('ebook %s is too large to be emailed' % work.id)
return local_response(request, javascript, context, 0)
try:
email = EmailMessage(from_email='notices@gluejar.com',
to=[kindle_email])
email.attach(title + '.' + ebook_format, filehandle.read())
email.attach(title + '.' + ebook.format, filehandle.read())
email.send()
except:
logger.error('Unexpected error: %s', sys.exc_info())