Skip to content

Commit

Permalink
Fixed #35658 -- Initialized InMemoryFileNode instances with a name.
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasEsposito authored and sarahboyce committed Aug 9, 2024
1 parent 69aa13f commit f16a9a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 7 additions & 4 deletions django/core/files/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ class InMemoryFileNode(ContentFile, TimingMixin):
modification, and access times.
"""

def __init__(self, content="", name=""):
self.file = None
def __init__(self, content="", name=None):
super().__init__(content, name)
self._content_type = type(content)
self._initialize_stream()
self._initialize_times()

def open(self, mode):
Expand Down Expand Up @@ -142,7 +141,11 @@ def _resolve_child(self, path_segment, create_if_missing, child_cls):
if create_if_missing:
self._update_accessed_time()
self._update_modified_time()
return self._children.setdefault(path_segment, child_cls())
if child_cls is InMemoryFileNode:
child = child_cls(name=path_segment)
else:
child = child_cls()
return self._children.setdefault(path_segment, child)
return self._children.get(path_segment)

def listdir(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/file_storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,23 @@ def test_stringio(self):
with temp_storage.open("tests/stringio") as f:
self.assertEqual(f.read(), b"content")

@override_settings(
STORAGES={
DEFAULT_STORAGE_ALIAS: {
"BACKEND": "django.core.files.storage.InMemoryStorage"
}
}
)
def test_create_file_field_from_another_file_field_in_memory_storage(self):
f = ContentFile("content", "file.txt")
obj = Storage.objects.create(storage_callable_default=f)
new_obj = Storage.objects.create(
storage_callable_default=obj.storage_callable_default.file
)
storage = callable_default_storage()
with storage.open(new_obj.storage_callable_default.name) as f:
self.assertEqual(f.read(), b"content")


class FieldCallableFileStorageTests(SimpleTestCase):
def setUp(self):
Expand Down

0 comments on commit f16a9a5

Please sign in to comment.