From 5a747750ec25c26a3fed6c5f93e635d1b7de15a9 Mon Sep 17 00:00:00 2001 From: Frodothedwarf Date: Tue, 14 May 2024 12:03:20 +0200 Subject: [PATCH 1/3] Fixing the problem that arises when using --clear in python manage.py collectstatic --- storages/backends/azure_storage.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 75e27b10..7c631770 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -268,6 +268,15 @@ def get_available_name(self, name, max_length=_AZURE_NAME_MAX_LEN): return super().get_available_name(name, max_length) def exists(self, name): + if not name: + try: + self.service_client.get_container_client( + self.azure_container + ) + return True + except: + return False + blob_client = self.client.get_blob_client(self._get_valid_path(name)) return blob_client.exists() @@ -390,16 +399,9 @@ def list_all(self, path=""): def listdir(self, path=""): """ - Return directories and files for a given path. - Leave the path empty to list the root. - Order of dirs and files is undefined. + Return all files for a given path. + Given that Azure can't return paths it only returns files. + Works great for our little adventure. """ - files = [] - dirs = set() - for name in self.list_all(path): - n = name[len(path) :] - if "/" in n: - dirs.add(n.split("/", 1)[0]) - else: - files.append(n) - return list(dirs), files + + return [], self.list_all(path) From a015c79f0ae9cf6465b74e8bfe425090bb5636b5 Mon Sep 17 00:00:00 2001 From: Frodothedwarf Date: Tue, 28 May 2024 09:26:23 +0200 Subject: [PATCH 2/3] Tests edited Tests edited to cover the fact that Azure can't give subdirectories. --- storages/backends/azure_storage.py | 8 +------ tests/test_azure.py | 37 +++--------------------------- 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 7c631770..54659851 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -269,13 +269,7 @@ def get_available_name(self, name, max_length=_AZURE_NAME_MAX_LEN): def exists(self, name): if not name: - try: - self.service_client.get_container_client( - self.azure_container - ) - return True - except: - return False + return True blob_client = self.client.get_blob_client(self._get_valid_path(name)) return blob_client.exists() diff --git a/tests/test_azure.py b/tests/test_azure.py index 9ae2e01c..cad33203 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -388,46 +388,15 @@ def test_storage_listdir_base(self): ) self.storage._custom_client.list_blobs.assert_not_called() - self.assertEqual(len(dirs), 2) - for directory in ["some", "other"]: - self.assertTrue( - directory in dirs, - """ "{}" not in directory list "{}".""".format(directory, dirs), - ) + self.assertEqual(len(dirs), 0) - self.assertEqual(len(files), 2) - for filename in ["2.txt", "4.txt"]: + self.assertEqual(len(files), 4) + for filename in ["2.txt", "4.txt", "other/path/3.txt", "some/path/1.txt"]: self.assertTrue( filename in files, """ "{}" not in file list "{}".""".format(filename, files), ) - def test_storage_listdir_subdir(self): - file_names = ["some/path/1.txt", "some/2.txt"] - - result = [] - for p in file_names: - obj = mock.MagicMock() - obj.name = p - result.append(obj) - self.storage._client.list_blobs.return_value = iter(result) - self.storage._custom_client.list_blobs.assert_not_called() - - dirs, files = self.storage.listdir("some/") - self.storage._client.list_blobs.assert_called_with( - name_starts_with="some/", timeout=20 - ) - - self.assertEqual(len(dirs), 1) - self.assertTrue( - "path" in dirs, """ "path" not in directory list "{}".""".format(dirs) - ) - - self.assertEqual(len(files), 1) - self.assertTrue( - "2.txt" in files, """ "2.txt" not in files list "{}".""".format(files) - ) - def test_size_of_file(self): props = BlobProperties() props.size = 12 From c8bb98e64c3e7627e7f65fc2a61f128bbbd60d20 Mon Sep 17 00:00:00 2001 From: Frodothedwarf Date: Wed, 29 May 2024 08:13:15 +0200 Subject: [PATCH 3/3] Fix whitespace --- storages/backends/azure_storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 54659851..69b02137 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -397,5 +397,5 @@ def listdir(self, path=""): Given that Azure can't return paths it only returns files. Works great for our little adventure. """ - + return [], self.list_all(path)