Merge pull request #460 from Gluejar/fix_kindle_bugs

provide a title that's kindle-safe
pull/1/head
Raymond Yee 2015-03-06 15:06:21 -08:00
commit 387c8d43e2
2 changed files with 18 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import random
import urllib import urllib
import urllib2 import urllib2
from urlparse import urlparse from urlparse import urlparse
import unicodedata
from ckeditor.fields import RichTextField from ckeditor.fields import RichTextField
from datetime import timedelta, datetime from datetime import timedelta, datetime
@ -1187,6 +1188,21 @@ class Work(models.Model):
return "%s et al." % self.authors()[0].name return "%s et al." % self.authors()[0].name
return '' return ''
def kindle_safe_title(self):
safe = u''
nkfd_form = unicodedata.normalize('NFKD', self.title) #unaccent accented letters
for c in nkfd_form:
ccat = unicodedata.category(c)
#print ccat
if ccat.startswith('L') or ccat.startswith('N'): # only letters and numbers
if ord(c) > 127:
safe = safe + '#' #a non latin script letter or number
else:
safe = safe + c
elif not unicodedata.combining(c): #not accents (combining forms)
safe = safe + '_' #punctuation
return safe
def last_campaign(self): def last_campaign(self):
# stash away the last campaign to prevent repeated lookups # stash away the last campaign to prevent repeated lookups
if hasattr(self, '_last_campaign_'): if hasattr(self, '_last_campaign_'):

View File

@ -3185,7 +3185,7 @@ def send_to_kindle(request, work_id, javascript='0'):
if acq: if acq:
ebook_url = acq.get_mobi_url() ebook_url = acq.get_mobi_url()
ebook_format = 'mobi' ebook_format = 'mobi'
title = acq.work.title title = acq.work.kindle_safe_title()
else: else:
non_google_ebooks = work.ebooks().exclude(provider='Google Books') non_google_ebooks = work.ebooks().exclude(provider='Google Books')
try: try:
@ -3201,8 +3201,7 @@ def send_to_kindle(request, work_id, javascript='0'):
ebook_url = ebook.url ebook_url = ebook.url
ebook_format = ebook.format ebook_format = ebook.format
logger.info('ebook: {0}, user_ip: {1}'.format(work_id, request.META['REMOTE_ADDR'])) logger.info('ebook: {0}, user_ip: {1}'.format(work_id, request.META['REMOTE_ADDR']))
title = ebook.edition.title title = ebook.edition.work.kindle_safe_title()
title = title.replace(' ', '_')
context['ebook_url']=ebook_url context['ebook_url']=ebook_url
context['ebook_format']=ebook_format context['ebook_format']=ebook_format