make sure graceful degradation works, fix bugs

pull/1/head
Andromeda Yelton 2013-06-05 11:06:07 -04:00
parent f4ffd5da6f
commit 2f3f8997ad
11 changed files with 85 additions and 22 deletions

View File

@ -85,7 +85,7 @@ $j(document).ready(function() {
{% endif %}
{% if kindle_ebook_id %}
<div class="btn_support modify">
<a id="kindle" class="{{ kindle_ebook_id }}">Send to Kindle</a>
<a id="kindle" class="{{ kindle_ebook_id }}{% if request.user.is_authenticated %} authenticated{% endif %}">Send to Kindle</a>f
</div>
{% endif %}
<div class="btn_support modify">
@ -119,9 +119,20 @@ $j(document).ready(function() {
<div id="kindle_div">
<h4>Send to Kindle</h4>
{% if request.user.is_authenticated and request.user.profile.kindle_email %}
<p>Sending...<img src="/static/images/loading.gif"></p>
<p>(For large files, this may take some time.)</p>
<p>If your Kindle email has changed, you can <a href="{% url kindle_config %}">add the new one to Unglue.it here</a>.</p>
<div class="yes_js">
<p>Sending...<img src="/static/images/loading.gif"></p>
<p>(For large files, this may take some time.)</p>
<p>If your Kindle email has changed, you can <a href="{% url kindle_config %}">add the new one to Unglue.it here</a>.</p>
</div>
<div class="no_js">
<p>
<form method="POST" action="{% url send_to_kindle kindle_ebook_id 0 %}">
<input type="submit" value="Send it!">
</form>
</p>
<p>(For large files, this may take some time.)</p>
<p>If your Kindle email has changed, you can <a href="{% url kindle_config %}">add the new one to Unglue.it here</a>.</p>
</div>
{% else %}
<p>If you have a Kindle device or app, <a href="{% url kindle_config %}">add your Kindle email address to Unglue.it</a> and you'll be able to send unglued ebooks to your reader with one click.</p>
{% endif %}
@ -134,7 +145,7 @@ $j(document).ready(function() {
<ul>
<li><a href="http://itunes.apple.com/us/app/ibooks/id364709193?mt=8">Download the free iBooks app</a> from the App Store.</li>
<li>Download your book from this page using your device's web browser.</li>
<li>You can read HTML files right in the browser. For other formats, you will be given the option of opening the file in iBooks.</li>
<li>You will be given the option of opening the file in iBooks.</li>
</ul>
</div>

View File

@ -0,0 +1,5 @@
{% extends "registration/registration_base.html" %}
{% block doccontent %}
{{ message }}
{% endblock %}

View File

@ -127,7 +127,8 @@ urlpatterns = patterns(
url(r"^press/$","press", name="press"),
url(r"^press_submitterator/$","press_submitterator", name="press_submitterator"),
url(r"^accounts/edit/kindle_config/$", "kindle_config", name="kindle_config"),
url(r"^send_to_kindle/(?P<kindle_ebook_id>\d+)/$", "send_to_kindle", name="send_to_kindle"),
url(r"^send_to_kindle/(?P<kindle_ebook_id>\d+)/(?P<javascript>\d)/$", "send_to_kindle", name="send_to_kindle"),
url(r"^send_to_kindle/result/(?P<message>\d)/$", "send_to_kindle_graceful", name="send_to_kindle_graceful"),
)
if settings.DEBUG:

View File

@ -1793,7 +1793,7 @@ def wishlist(request):
return HttpResponse('added googlebooks id')
except bookloader.LookupFailure:
logger.warning("failed to load googlebooks_id %s" % googlebooks_id)
return HttpResponse('error addin googlebooks id')
return HttpResponse('error adding googlebooks id')
except Exception, e:
logger.warning("Error in wishlist adding %s" % (e))
return HttpResponse('error adding googlebooks id')
@ -2469,16 +2469,31 @@ def kindle_config(request):
form = KindleEmailForm()
return render(request, "kindle_config.html", {'form': form})
kindle_response_params = [
'This ebook is too big to be sent by email. Please download the file and then sideload it to your device using the instructions under Ereaders or Desktop.',
"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.",
'This book has been sent to your Kindle. Happy reading!'
]
@require_POST
@login_required
@csrf_exempt
def send_to_kindle(request, kindle_ebook_id):
def send_to_kindle(request, kindle_ebook_id, javascript='0'):
# make sure to gracefully communicate with both js and non-js users
def local_response(request, javascript, message):
if javascript == '1':
return HttpResponse(kindle_response_params[message])
else:
# can't pass context with HttpResponseRedirect
# must use an HttpResponse, not a render(), after POST
return HttpResponseRedirect(reverse('send_to_kindle_graceful', args=(message,)))
# don't forget to increment the download counter!
ebook = models.Ebook.objects.get(pk=kindle_ebook_id)
assert ebook.format == 'mobi' or ebook.format == 'pdf'
ebook.increment()
logger.info("ebook: {0}, user_ip: {1}".format(kindle_ebook_id, request.META['REMOTE_ADDR']))
logger.info('ebook: {0}, user_ip: {1}'.format(kindle_ebook_id, request.META['REMOTE_ADDR']))
title = ebook.edition.title
title = title.replace(' ', '_')
@ -2486,7 +2501,8 @@ def send_to_kindle(request, kindle_ebook_id):
filehandle = urllib.urlopen(ebook.url)
filesize = int(filehandle.info().getheaders("Content-Length")[0])
if filesize > 26214400:
return HttpResponse('<p>This ebook is too big to be sent by email. Please download the file and then sideload it to your device using the instructions under Ereaders or Desktop.</p>')
logger.info('ebook %s is too large to be emailed' % kindle_ebook_id)
return local_response(request, javascript, 0)
try:
email = EmailMessage(from_email='kindle@gluejar.com',
@ -2495,6 +2511,14 @@ def send_to_kindle(request, kindle_ebook_id):
email.send()
except:
logger.warning('Unexpected error:', sys.exc_info()[0])
return HttpResponse("<p>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.</p>")
return HttpResponse('<p>This book has been sent to your Kindle. Happy reading!</p>')
return local_response(request, javascript, 1)
return local_response(request, javascript, 2)
def send_to_kindle_graceful(request, message):
message = kindle_response_params[int(message)]
return render(
request,
'kindle_response_graceful_degradation.html',
{'message': message}
)

View File

@ -1 +1 @@
.buttons{display:inherit}.instructions div:not(#trythis_div),.instructions h4{display:none}
.buttons{display:inherit}.instructions>div:not(#trythis_div),.instructions h4{display:none}#kindle_div .no_js{display:none!important}#kindle_div .yes_js{display:inherit}

View File

@ -1 +1 @@
.buttons{display:inherit}.instructions div,.instructions h4{display:none}#trythis_div{display:inherit}
.buttons{display:inherit}.instructions>div,.instructions h4{display:none}#kindle_div .no_js{display:none!important}#kindle_div .yes_js{display:inherit}#trythis_div{display:inherit}

File diff suppressed because one or more lines are too long

View File

@ -21,9 +21,10 @@ $j(document).on('click', '.buttons div', function() {
activeDiv.siblings().hide();
});
$j(document).on('click', '#kindle', function() {
kindle_ebook_id = $j(this).attr('class');
$j.post('/send_to_kindle/' + kindle_ebook_id + '/', function(data) {
$j(document).on('click', '#kindle.authenticated', function() {
classes = $j(this).attr('class').split(' ');
kindle_ebook_id = classes[0];
$j.post('/send_to_kindle/' + kindle_ebook_id + '/1/', function(data) {
$j('#kindle_div').html(data);
});
});

View File

@ -2,6 +2,14 @@
display: inherit;
}
.instructions div:not(#trythis_div), .instructions h4 {
.instructions > div:not(#trythis_div), .instructions h4 {
display: none;
}
#kindle_div .no_js {
display: none !important;
}
#kindle_div .yes_js {
display: inherit;
}

View File

@ -2,10 +2,18 @@
display: inherit;
}
.instructions div, .instructions h4 {
.instructions > div, .instructions h4 {
display: none;
}
#kindle_div .no_js {
display: none !important;
}
#kindle_div .yes_js {
display: inherit;
}
/* the not selector doesn't work in IE <= 8 */
#trythis_div {
display: inherit;

View File

@ -901,6 +901,7 @@ li.checked {
}
}
/* download page */
.buttons {
display: none;
border-bottom: solid 2px @blue-grey;
@ -931,3 +932,7 @@ li.checked {
line-height: @font-size-larger*1.5;
}
}
#kindle_div .yes_js {
display: none;
}