Skip to content

Commit

Permalink
Avoid over-explaining in comments.
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Johnson <[email protected]>

Fix tests.
  • Loading branch information
john-parton committed Apr 29, 2024
1 parent b21b364 commit 767d6f1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
8 changes: 2 additions & 6 deletions django/db/models/fields/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ def open(self, mode="rb"):
def save(self, name, content, save=True):
name = self.field.generate_filename(self.instance, name)
self.name = self.storage.save(name, content, max_length=self.field.max_length)

# Set the file-like content using the descriptors __set__ method.
setattr(self.instance, self.field.attname, content)

# Update the name in case generate_filename or the storage backend
# changed the name. Avoids re-running the descriptor logic.
# Update the name in case generate_filename() or storage.save() changed
# it, but bypass the descriptor to avoid re-reading the file.
self.instance.__dict__[self.field.attname] = self.name

self._committed = True

# Save the object because it has changed, unless save is False
Expand Down
6 changes: 1 addition & 5 deletions tests/model_fields/storage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from django.core.files.storage.filesystem import FileSystemStorage


class NoReadException(Exception):
pass


class NoReadFileSystemStorage(FileSystemStorage):
def open(self, *args, **kwargs):
raise NoReadException("This storage class does not support reading.")
raise AssertionError("This storage class does not support reading.")
6 changes: 2 additions & 4 deletions tests/model_fields/test_imagefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,8 @@ def test_dimensions(self):

@skipIf(Image is None, "Pillow is required to test ImageField")
class NoReadTests(ImageFieldTestMixin, TestCase):
PersonNoReadImage = PersonNoReadImage

def test_width_height_correct_name_mangling_correct(self):
instance1 = self.PersonNoReadImage()
instance1 = PersonNoReadImage()

instance1.mugshot.save("mug", self.file1)

Expand All @@ -489,7 +487,7 @@ def test_width_height_correct_name_mangling_correct(self):
self.assertEqual(instance1.mugshot_width, 4)
self.assertEqual(instance1.mugshot_height, 8)

instance2 = self.PersonNoReadImage()
instance2 = PersonNoReadImage()
instance2.mugshot.save("mug", self.file1)
instance2.save()

Expand Down

0 comments on commit 767d6f1

Please sign in to comment.