diff --git a/core/models.py b/core/models.py index 68fc5ae4..9767375d 100755 --- a/core/models.py +++ b/core/models.py @@ -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 diff --git a/frontend/views.py b/frontend/views.py index c503d64d..1b38c88b 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -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