Ticket #15181: filepath_to_uri.patch
File filepath_to_uri.patch, 3.2 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/file_storage/tests.py
204 204 self.assertEqual(self.storage.url('test.file'), 205 205 '%s%s' % (self.storage.base_url, 'test.file')) 206 206 207 # should encode special chars except ~!*()' 208 # like encodeURIComponent() JavaScript function do 209 self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""), 210 """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""") 211 212 # should stanslate os path separator(s) to the url path separator 213 self.assertEqual(self.storage.url("""a/b\\c.file"""), 214 """/test_media_url/a/b/c.file""") 215 207 216 self.storage.base_url = None 208 217 self.assertRaises(ValueError, self.storage.url, 'test.file') 209 218 -
django/core/files/storage.py
8 8 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation 9 9 from django.core.files import locks, File 10 10 from django.core.files.move import file_move_safe 11 from django.utils.encoding import force_unicode 11 from django.utils.encoding import force_unicode, filepath_to_uri 12 12 from django.utils.functional import LazyObject 13 13 from django.utils.importlib import import_module 14 14 from django.utils.text import get_valid_filename … … 240 240 def url(self, name): 241 241 if self.base_url is None: 242 242 raise ValueError("This file is not accessible via a URL.") 243 return urlparse.urljoin(self.base_url, name).replace('\\', '/')243 return urlparse.urljoin(self.base_url, filepath_to_uri(name)) 244 244 245 245 def accessed_time(self, name): 246 246 return datetime.fromtimestamp(os.path.getatime(self.path(name))) -
django/utils/encoding.py
156 156 return iri 157 157 return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~") 158 158 159 def filepath_to_uri(path): 160 """Convert an file system path to a URI portion that is suitable for 161 inclusion in a URL. 159 162 163 We are assuming input is either UTF-8 or unicode already. 164 165 This method will encode certain chars that would normally be recognized as 166 special chars for URIs. Note that this method does not encode the ' 167 character, as it is a valid character within URIs. See 168 encodeURIComponent() JavaScript function for more details. 169 170 Returns an ASCII string containing the encoded result. 171 """ 172 if path is None: 173 return path 174 # I know about `os.sep` and `os.altsep` but I want to leave 175 # some flexibility for hardcoding separators. 176 return urllib.quote(smart_str(path).replace("\\", "/"), safe="/~!*()'") 177 160 178 # The encoding of the default system locale but falls back to the 161 179 # given fallback encoding if the encoding is unsupported by python or could 162 180 # not be determined. See tickets #10335 and #5846