Code

Ticket #11084: 11084.diff

File 11084.diff, 3.1 KB (added by jdunck, 5 years ago)

Patch was backwards, fixed now with passing tests.

Line 
1diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
2index 61a903e..aefe388 100644
3--- a/django/db/models/fields/files.py
4+++ b/django/db/models/fields/files.py
5@@ -300,11 +300,14 @@ class ImageFileDescriptor(FileDescriptor):
6     assigning the width/height to the width_field/height_field, if appropriate.
7     """
8     def __set__(self, instance, value):
9+        update_dimensions = instance.__dict__.get(self.field.name) is not None
10         super(ImageFileDescriptor, self).__set__(instance, value)
11         
12         # The rest of this method deals with width/height fields, so we can
13-        # bail early if neither is used.
14-        if not self.field.width_field and not self.field.height_field:
15+        # bail early if neither is used or we don't have to update the
16+        # dimensons because the data is coming from the database
17+        if not (update_dimensions and (self.field.width_field or
18+                                       self.field.height_field)):
19             return
20         
21         # We need to call the descriptor's __get__ to coerce this assigned
22diff --git a/tests/regressiontests/file_storage/models.py b/tests/regressiontests/file_storage/models.py
23index 32af5d7..b41c924 100644
24--- a/tests/regressiontests/file_storage/models.py
25+++ b/tests/regressiontests/file_storage/models.py
26@@ -2,6 +2,7 @@ import os
27 import tempfile
28 import shutil
29 from django.db import models
30+from django.db.models.fields.files import ImageFieldFile, ImageField
31 from django.core.files.storage import FileSystemStorage
32 from django.core.files.base import ContentFile
33 
34@@ -17,12 +18,22 @@ except ImportError:
35 
36 # If we have PIL, do these tests
37 if Image:
38+    class TestImageFieldFile(ImageFieldFile):
39+        def __init__(self, *args, **kwargs):
40+            self.test_was_opened = False
41+            super(TestImageFieldFile, self).__init__(*args,**kwargs)
42+        def open(self):
43+            self.test_was_opened = True
44+            super(TestImageFieldFile, self).open()
45+    class TestImageField(ImageField):
46+        attr_class = TestImageFieldFile
47+
48     temp_storage_dir = tempfile.mkdtemp()
49     temp_storage = FileSystemStorage(temp_storage_dir)
50 
51     class Person(models.Model):
52         name = models.CharField(max_length=50)
53-        mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
54+        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
55                                     height_field='mug_height',
56                                     width_field='mug_width')
57         mug_height = models.PositiveSmallIntegerField()
58@@ -72,11 +83,20 @@ True
59 
60 # Get a "clean" model instance
61 >>> p3 = Person.objects.get(name="Joan")
62+>>> p3.mugshot.test_was_opened
63+False
64 
65 # It won't have an opened file.
66 >>> p3.mugshot.closed
67 True
68 
69+# Bug #11084: If the file is unavailable, still create the object without error.
70+>>> path = p3.mugshot.path
71+>>> shutil.move(path, path + '2')
72+>>> p4 = Person.objects.get(name="Joan")
73+>>> shutil.move(path + '2', path)
74+
75+
76 # After asking for the size, the file should still be closed.
77 >>> _ = p3.mugshot.size
78 >>> p3.mugshot.closed