Ticket #10196: 10196.diff

File 10196.diff, 6.7 KB (added by kmtracey, 6 years ago)
  • django/db/models/fields/files.py

     
    132132            # have the FieldFile interface added to them
    133133            file_copy = copy.copy(file)
    134134            file_copy.__class__ = type(file.__class__.__name__,
    135                                        (file.__class__, FieldFile), {})
     135                                       (file.__class__, self.field.attr_class), {})
    136136            file_copy.instance = instance
    137137            file_copy.field = self.field
    138138            file_copy.storage = self.field.storage
  • tests/modeltests/model_forms/models.py

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/modeltests/model_forms/test2.png
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
    110110        # for PyPy, you need to check for the underlying modules
    111111        # If PIL is not available, this test is equivalent to TextFile above.
    112112        from PIL import Image, _imaging
    113         image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path)
     113        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
     114                                  width_field='width', height_field='height')
     115        width = models.IntegerField(editable=False)
     116        height = models.IntegerField(editable=False)
    114117    except ImportError:
    115118        image = models.FileField(storage=temp_storage, upload_to=custom_upload_path)
    116119    path = models.CharField(max_length=16, blank=True, default='')
    117120
    118121    def __unicode__(self):
    119122        return self.description
     123   
     124class OptionalImageFile(models.Model):
     125    def custom_upload_path(self, filename):
     126        path = self.path or 'tests'
     127        return '%s/%s' % (path, filename)
     128   
     129    description = models.CharField(max_length=20)
     130    try:
     131        # If PIL is available, try testing PIL.
     132        # Checking for the existence of Image is enough for CPython, but
     133        # for PyPy, you need to check for the underlying modules
     134        # If PIL is not available, this test is equivalent to TextFile above.
     135        from PIL import Image, _imaging
     136        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
     137                                  width_field='width', height_field='height',
     138                                  blank=True, null=True)
     139        width = models.IntegerField(editable=False, null=True)
     140        height = models.IntegerField(editable=False, null=True)
     141    except ImportError:
     142        image = models.FileField(storage=temp_storage, upload_to=custom_upload_path)
     143    path = models.CharField(max_length=16, blank=True, default='')
    120144
     145    def __unicode__(self):
     146        return self.description
     147
    121148class CommaSeparatedInteger(models.Model):
    122149    field = models.CommaSeparatedIntegerField(max_length=20)
    123150
     
    10561083...         model = ImageFile
    10571084
    10581085>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
     1086>>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read()
    10591087
    10601088>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
    10611089>>> f.is_valid()
     
    10651093>>> instance = f.save()
    10661094>>> instance.image
    10671095<...FieldFile: tests/test.png>
     1096>>> instance.width
     109716
     1098>>> instance.height
     109916
    10681100
    10691101# Delete the current file since this is not done by Django.
    10701102>>> instance.image.delete()
     
    10771109>>> instance = f.save()
    10781110>>> instance.image
    10791111<...FieldFile: tests/test.png>
     1112>>> instance.width
     111316
     1114>>> instance.height
     111516
    10801116
    10811117# Edit an instance that already has the image defined in the model. This will not
    10821118# save the image again, but leave it exactly as it is.
     
    10891125>>> instance = f.save()
    10901126>>> instance.image
    10911127<...FieldFile: tests/test.png>
     1128>>> instance.height
     112916
     1130>>> instance.width
     113116
    10921132
    10931133# Delete the current image since this is not done by Django.
    10941134
     
    11021142>>> instance = f.save()
    11031143>>> instance.image
    11041144<...FieldFile: tests/test2.png>
     1145>>> instance.height
     114616
     1147>>> instance.width
     114816
    11051149
    11061150# Delete the current file since this is not done by Django.
    11071151>>> instance.image.delete()
    11081152>>> instance.delete()
    11091153
    1110 >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)})
     1154>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)})
    11111155>>> f.is_valid()
    11121156True
    11131157>>> instance = f.save()
    11141158>>> instance.image
    11151159<...FieldFile: tests/test2.png>
     1160>>> instance.height
     116132
     1162>>> instance.width
     116348
    11161164
    11171165# Delete the current file since this is not done by Django.
    11181166>>> instance.image.delete()
     
    11201168
    11211169# Test the non-required ImageField
    11221170
    1123 >>> f = ImageFileForm(data={'description': u'Test'})
    1124 >>> f.fields['image'].required = False
     1171>>> class OptionalImageFileForm(ModelForm):
     1172...     class Meta:
     1173...         model = OptionalImageFile
     1174
     1175>>> f = OptionalImageFileForm(data={'description': u'Test'})
    11251176>>> f.is_valid()
    11261177True
    11271178>>> instance = f.save()
    11281179>>> instance.image
    11291180<...FieldFile: None>
     1181>>> instance.width
     1182>>> instance.height
    11301183
    1131 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
     1184>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
    11321185>>> f.is_valid()
    11331186True
    11341187>>> instance = f.save()
    11351188>>> instance.image
    11361189<...FieldFile: tests/test3.png>
     1190>>> instance.width
     119116
     1192>>> instance.height
     119316
    11371194
    11381195# Delete the current file since this is not done by Django.
    11391196>>> instance.image.delete()
    11401197>>> instance.delete()
    11411198
    1142 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)})
     1199>>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)})
    11431200>>> f.is_valid()
    11441201True
    11451202>>> instance = f.save()
    11461203>>> instance.image
    1147 <...FieldFile: tests/test3.png>
     1204<...FieldFile: tests/test4.png>
     1205>>> instance.width
     120648
     1207>>> instance.height
     120832
    11481209>>> instance.delete()
    11491210
    11501211# Test callable upload_to behavior that's dependent on the value of another field in the model
Back to Top