Ticket #11084: 11084.diff

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

Patch was backwards, fixed now with passing tests.

  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index 61a903e..aefe388 100644
    a b class ImageFileDescriptor(FileDescriptor): 
    300300    assigning the width/height to the width_field/height_field, if appropriate.
    301301    """
    302302    def __set__(self, instance, value):
     303        update_dimensions = instance.__dict__.get(self.field.name) is not None
    303304        super(ImageFileDescriptor, self).__set__(instance, value)
    304305       
    305306        # The rest of this method deals with width/height fields, so we can
    306         # bail early if neither is used.
    307         if not self.field.width_field and not self.field.height_field:
     307        # bail early if neither is used or we don't have to update the
     308        # dimensons because the data is coming from the database
     309        if not (update_dimensions and (self.field.width_field or
     310                                       self.field.height_field)):
    308311            return
    309312       
    310313        # We need to call the descriptor's __get__ to coerce this assigned
  • tests/regressiontests/file_storage/models.py

    diff --git a/tests/regressiontests/file_storage/models.py b/tests/regressiontests/file_storage/models.py
    index 32af5d7..b41c924 100644
    a b import os 
    22import tempfile
    33import shutil
    44from django.db import models
     5from django.db.models.fields.files import ImageFieldFile, ImageField
    56from django.core.files.storage import FileSystemStorage
    67from django.core.files.base import ContentFile
    78
    except ImportError: 
    1718
    1819# If we have PIL, do these tests
    1920if Image:
     21    class TestImageFieldFile(ImageFieldFile):
     22        def __init__(self, *args, **kwargs):
     23            self.test_was_opened = False
     24            super(TestImageFieldFile, self).__init__(*args,**kwargs)
     25        def open(self):
     26            self.test_was_opened = True
     27            super(TestImageFieldFile, self).open()
     28    class TestImageField(ImageField):
     29        attr_class = TestImageFieldFile
     30
    2031    temp_storage_dir = tempfile.mkdtemp()
    2132    temp_storage = FileSystemStorage(temp_storage_dir)
    2233
    2334    class Person(models.Model):
    2435        name = models.CharField(max_length=50)
    25         mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
     36        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
    2637                                    height_field='mug_height',
    2738                                    width_field='mug_width')
    2839        mug_height = models.PositiveSmallIntegerField()
    True 
    7283
    7384# Get a "clean" model instance
    7485>>> p3 = Person.objects.get(name="Joan")
     86>>> p3.mugshot.test_was_opened
     87False
    7588
    7689# It won't have an opened file.
    7790>>> p3.mugshot.closed
    7891True
    7992
     93# Bug #11084: If the file is unavailable, still create the object without error.
     94>>> path = p3.mugshot.path
     95>>> shutil.move(path, path + '2')
     96>>> p4 = Person.objects.get(name="Joan")
     97>>> shutil.move(path + '2', path)
     98
     99
    80100# After asking for the size, the file should still be closed.
    81101>>> _ = p3.mugshot.size
    82102>>> p3.mugshot.closed
Back to Top