From 17752003a8c115ff79f5f21655f5e9b8b2af67f4 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Mon, 21 Oct 2019 18:03:48 +0200 Subject: [PATCH] Refs #28428 -- Made FileSystemStorage.save() to support pathlib.Path. --- django/core/files/storage.py | 2 +- docs/ref/files/storage.txt | 5 +++++ docs/releases/3.1.txt | 2 +- tests/file_storage/tests.py | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 4c27fce605..e9166d94ff 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -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." diff --git a/docs/ref/files/storage.txt b/docs/ref/files/storage.txt index b2710062df..542eb2dc7e 100644 --- a/docs/ref/files/storage.txt +++ b/docs/ref/files/storage.txt @@ -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 ===================== diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index d928abd205..ef2ab9b173 100644 --- a/docs/releases/3.1.txt +++ b/docs/releases/3.1.txt @@ -124,7 +124,7 @@ Email File Storage ~~~~~~~~~~~~ -* ... +* ``FileSystemStorage.save()`` method now supports :class:`pathlib.Path`. File Uploads ~~~~~~~~~~~~ diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 779116b3bd..be6e11de93 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -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