def test_string_to_ascii(self): test_cases = [ (u'30 \U0001d5c4\U0001d5c6/\U0001d5c1', '30 km/h'), (u'\u5317\u4EB0', 'BeiJing'), ('ぁ あ ぃ い ぅ う ぇ', 'a a i i u u e'), ('Ա Բ Գ Դ Ե Զ Է Ը Թ Ժ Ի Լ Խ Ծ Կ Հ Ձ Ղ Ճ Մ Յ Ն', 'A B G D E Z E Y T\' Zh I L Kh Ts K H Dz Gh Ch M Y N'), ('Спорт!', 'Sport!'), ('Straßenbahn', 'Strassenbahn'), ('Hello world', 'Hello world'), ('Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ', 'A a A a A a C c C c C c C c D d D'), ('〔山脈〕', '[ShanMai]'), ] for (original, expected_result) in test_cases: self.assertEqual(string_to_ascii(original), expected_result)
def get_upload_to(self, filename): folder_name = 'original_images' filename = self.file.field.storage.get_valid_name(filename) # do a unidecode in the filename and then # replace non-ascii characters in filename with _ , to sidestep issues with filesystem encoding filename = "".join((i if ord(i) < 128 else '_') for i in string_to_ascii(filename)) # Truncate filename so it fits in the 100 character limit # https://code.djangoproject.com/ticket/9893 full_path = os.path.join(folder_name, filename) if len(full_path) >= 95: chars_to_trim = len(full_path) - 94 prefix, extension = os.path.splitext(filename) filename = prefix[:-chars_to_trim] + extension full_path = os.path.join(folder_name, filename) return full_path
def test_string_to_ascii(self): test_cases = [ ("30 \U0001d5c4\U0001d5c6/\U0001d5c1", "30 km/h"), ("\u5317\u4EB0", "BeiJing"), ("ぁ あ ぃ い ぅ う ぇ", "a a i i u u e"), ( "Ա Բ Գ Դ Ե Զ Է Ը Թ Ժ Ի Լ Խ Ծ Կ Հ Ձ Ղ Ճ Մ Յ Ն", "A B G D E Z E Y T' Zh I L Kh Ts K H Dz Gh Ch M Y N", ), ("Спорт!", "Sport!"), ("Straßenbahn", "Strassenbahn"), ("Hello world", "Hello world"), ("Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ", "A a A a A a C c C c C c C c D d D"), ("〔山脈〕", "[ShanMai]"), ] for (original, expected_result) in test_cases: self.assertEqual(string_to_ascii(original), expected_result)
def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None, backend=None): ''' create a response to send file using backend configured in SENDFILE_BACKEND If attachment is True the content-disposition header will be set. This will typically prompt the user to download the file, rather than view it. The content-disposition filename depends on the value of attachment_filename: None (default): Same as filename False: No content-disposition filename String: Value used as filename If no mimetype or encoding are specified, then they will be guessed via the filename (using the standard python mimetypes module) ''' _sendfile = backend or _get_sendfile() if not os.path.exists(filename): from django.http import Http404 raise Http404('"%s" does not exist' % filename) guessed_mimetype, guessed_encoding = guess_type(filename) if mimetype is None: if guessed_mimetype: mimetype = guessed_mimetype else: mimetype = 'application/octet-stream' response = _sendfile(request, filename, mimetype=mimetype) if attachment: if attachment_filename is None: attachment_filename = os.path.basename(filename) parts = ['attachment'] if attachment_filename: from django.utils.encoding import force_str from wagtail.core.utils import string_to_ascii attachment_filename = force_str(attachment_filename) ascii_filename = string_to_ascii(attachment_filename) parts.append('filename="%s"' % ascii_filename) if ascii_filename != attachment_filename: from django.utils.http import urlquote quoted_filename = urlquote(attachment_filename) parts.append('filename*=UTF-8\'\'%s' % quoted_filename) response['Content-Disposition'] = '; '.join(parts) else: response['Content-Disposition'] = 'inline' response['Content-length'] = os.path.getsize(filename) response['Content-Type'] = mimetype response['Content-Encoding'] = encoding or guessed_encoding return response