Ticket #3119: store_filename_as_punycode_for_trunk_4462.diff
File store_filename_as_punycode_for_trunk_4462.diff, 4.0 KB (added by , 18 years ago) |
---|
-
django/conf/global_settings.py
199 199 # Example: "/home/media/media.lawrence.com/" 200 200 MEDIA_ROOT = '' 201 201 202 # If you set this to True, Django stores filename as punycode. 203 STORE_FILENAME_AS_PUNYCODE = False 204 202 205 # URL that handles the media served from MEDIA_ROOT. 203 206 # Example: "http://media.lawrence.com" 204 207 MEDIA_URL = '' … … 315 318 # The name of the database to use for testing purposes. 316 319 # If None, a name of 'test_' + DATABASE_NAME will be assumed 317 320 TEST_DATABASE_NAME = None 321 -
django/db/models/fields/__init__.py
656 656 return os.path.normpath(datetime.datetime.now().strftime(self.upload_to)) 657 657 658 658 def get_filename(self, filename): 659 from django.utils.text import get_valid_filename 659 if settings.STORE_FILENAME_AS_PUNYCODE : 660 from django.utils.text import get_valid_filename_as_puny as get_valid_filename 661 else : 662 from django.utils.text import get_valid_filename 660 663 f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename))) 661 664 return os.path.normpath(f) 662 665 -
django/utils/text.py
1 #encoding: utf-8 2 1 3 import re 2 4 3 5 from django.conf import settings … … 53 55 s = s.strip().replace(' ', '_') 54 56 return re.sub(r'[^-A-Za-z0-9_.]', '', s) 55 57 58 def get_valid_filename_as_puny(s): 59 """ 60 Encodes the given string in punycode except the extension. 61 Then generate a clean file name by calling get_valid_filename and return it. 62 >>> get_valid_filename_as_puny("développement image.jpg") 63 'dveloppement_image-kwa33c.jpg' 64 >>> get_valid_filename_as_puny("日本語ファイル名.jpg") 65 'caabc4cr5ao9bu0bzbf5exag15gcacc8bdc4c.jpg' 66 >>> get_valid_filename_as_puny("multidot.tar.gz") 67 'multidot.tar.gz' 68 >>> get_valid_filename_as_puny("developpement image.jpg") 69 'developpement_image.jpg' 70 >>> get_valid_filename_as_puny("john's portrait in 2004.jpg") 71 'johns_portrait_in_2004.jpg' 72 >>> get_valid_filename_as_puny("multidot.tar.gz") 73 'multidot.tar.gz' 74 >>> get_valid_filename_as_puny("複数ドット.tar.gz") 75 'daaaaqkd8g7f4e7ycc7a1a.tar.gz' 76 """ 77 try: 78 dot_index = s.index('.') 79 except ValueError: # filename has no dot 80 filename = s.encode('punycode') 81 if len(filename) > 0 and filename[:-1] == s: 82 filenamme = s 83 else: 84 filename = s[:dot_index].encode('punycode') + s[dot_index:] 85 if len(filename[:filename.index('.')]) > 0 and filename[:filename.index('.') - 1] == s[:dot_index]: 86 filename = s 87 return get_valid_filename(filename) 88 56 89 def get_text_list(list_, last_word='or'): 57 90 """ 58 91 >>> get_text_list(['a', 'b', 'c', 'd']) -
docs/settings.txt
711 711 712 712 .. _site framework docs: ../sites/ 713 713 714 STORE_FILENAME_AS_PUNYCODE 715 -------------------------- 716 717 Default: False 718 719 Whether to encode filename with punycode. 720 By default, FileField and its subtype calls django.utils.text.get_valid_filename() to 721 remove all "filename-unsafe" characters from given filename. 722 The resulting filename consists of alphabets, numbers, hyphens and underscores. 723 Set this True, if you might use multi-byte charcters for your filename. 724 FileField encode their filename with punycode except extension. 725 Then it remove all "filename-unsafe" characters. 726 727 714 728 TEMPLATE_CONTEXT_PROCESSORS 715 729 --------------------------- 716 730