provide a title that's kindle-safe

pull/1/head
eric 2015-03-03 17:39:23 -05:00
parent 9c4d609579
commit f05b276118
2 changed files with 18 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import random
import urllib
import urllib2
from urlparse import urlparse
import unicodedata
from ckeditor.fields import RichTextField
from datetime import timedelta, datetime
@ -1186,6 +1187,21 @@ class Work(models.Model):
elif self.authors().count()>2:
return "%s et al." % self.authors()[0].name
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):
# stash away the last campaign to prevent repeated lookups

View File

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