Refs #28428 -- Made FileSystemStorage.save() to support pathlib.Path.

This commit is contained in:
Hasan Ramezani 2019-10-21 18:03:48 +02:00 committed by Mariusz Felisiak
parent 0f2975534d
commit 17752003a8
4 changed files with 10 additions and 5 deletions

View File

@ -291,7 +291,7 @@ class FileSystemStorage(Storage):
os.chmod(full_path, self.file_permissions_mode)
# Store filenames with forward slashes, even on Windows.
return name.replace('\\', '/')
return str(name).replace('\\', '/')
def delete(self, name):
assert name, "The name argument is not allowed to be empty."

View File

@ -69,6 +69,11 @@ The ``FileSystemStorage`` class
time of the last metadata change, and on others (like Windows), it's
the creation time of the file.
.. versionchanged:: 3.1
Support for :class:`pathlib.Path` was added to the
``FileSystemStorage.save()`` method.
The ``Storage`` class
=====================

View File

@ -124,7 +124,7 @@ Email
File Storage
~~~~~~~~~~~~
* ...
* ``FileSystemStorage.save()`` method now supports :class:`pathlib.Path`.
File Uploads
~~~~~~~~~~~~

View File

@ -545,7 +545,7 @@ class FileStorageTests(SimpleTestCase):
p = Path('test.file')
self.assertFalse(self.storage.exists(p))
f = ContentFile('custom contents')
f_name = self.storage.save(str(p), f)
f_name = self.storage.save(p, f)
# Storage basic methods.
self.assertEqual(self.storage.path(p), os.path.join(self.temp_dir, p))
self.assertEqual(self.storage.size(p), 15)
@ -560,10 +560,10 @@ class CustomStorage(FileSystemStorage):
"""
Append numbers to duplicate files rather than underscores, like Trac.
"""
basename, *ext = name.split('.')
basename, *ext = os.path.splitext(name)
number = 2
while self.exists(name):
name = '.'.join([basename, str(number)] + ext)
name = ''.join([basename, '.', str(number)] + ext)
number += 1
return name