Ticket #8501: 8501v1.diff

File 8501v1.diff, 4.5 KB (added by daonb <bennydaon@…>, 16 years ago)
  • django/db/models/fields/files.py

     
    5555    url = property(_get_url)
    5656
    5757    def _get_size(self):
    58         self._require_file()
    59         return self.storage.size(self.name)
     58        if not hasattr(self, '_size'):
     59            self._require_file()
     60            self._size = self.storage.size(self.name)
     61        return self._size
    6062    size = property(_get_size)
    6163
    6264    def open(self, mode='rb'):
     
    7476        self._name = self.storage.save(name, content)
    7577        setattr(self.instance, self.field.name, self.name)
    7678
    77         # Update the filesize cache
    78         self._size = len(content)
     79        # Delete the filesize cache
     80        if hasattr(self, '_size'):
     81            del self._size
    7982
    8083        # Save the object because it has changed, unless save is False
    8184        if save:
     
    205208
    206209class ImageFieldFile(ImageFile, FieldFile):
    207210    def save(self, name, content, save=True):
    208         # Repopulate the image dimension cache.
    209         self._dimensions_cache = get_image_dimensions(content)
     211       
     212        # Clear the image dimensions cache
     213        if hasattr(self, '_dimensions_cache'):
     214            del self._dimensions_cache
    210215
     216        super(ImageFieldFile, self).save(name, content, save)
     217       
    211218        # Update width/height fields, if needed
    212219        if self.field.width_field:
    213220            setattr(self.instance, self.field.width_field, self.width)
    214221        if self.field.height_field:
    215222            setattr(self.instance, self.field.height_field, self.height)
    216223
    217         super(ImageFieldFile, self).save(name, content, save)
    218 
    219224    def delete(self, save=True):
    220225        # Clear the image dimensions cache
    221226        if hasattr(self, '_dimensions_cache'):
  • django/core/files/storage.py

     
    150150                    file_move_safe(content.temporary_file_path(), full_path)
    151151                    content.close()
    152152
    153                 # This is a normal uploadedfile that we can stream.
    154153                else:
    155154                    # This fun binary flag incantation makes os.open throw an
    156155                    # OSError if the file already exists before we open it.
    157156                    fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
    158157                    try:
    159158                        locks.lock(fd, locks.LOCK_EX)
     159                        # first try accessing content as an uploadedfile
    160160                        for chunk in content.chunks():
    161161                            os.write(fd, chunk)
     162                    except AttributeError:
     163                        # try reading the content using the read method
     164                        data = content.read()
     165                        os.write(fd, data) 
    162166                    finally:
    163167                        locks.unlock(fd)
    164168                        os.close(fd)
  • tests/modeltests/files/models.py

     
    66"""
    77
    88import tempfile
     9from urllib2 import urlopen
    910
    1011from django.db import models
    1112from django.core.files.base import ContentFile
     
    3233    random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
    3334    default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
    3435
     36    image = models.ImageField(storage=temp_storage, upload_to='tests')
     37   
    3538__test__ = {'API_TESTS':"""
    3639# An object without a file has limited functionality.
    3740
     
    109112>>> obj4.random
    110113<FieldFile: .../random_file>
    111114
     115# testing urllib
     116
     117>>> obj5 = Storage()
     118>>> image_data_len = len(urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif').read())
     119>>> obj5.image.save('django_logo.gif',urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif'))
     120>>> obj5.image
     121<ImageFieldFile: tests/django_logo.gif>
     122>>> image_data_len == obj5.image.size
     123True
     124>>> obj5.image.width
     12580L
     126>>> obj5.image.height
     12715L
     128
    112129# Clean up the temporary files.
    113130
    114131>>> obj1.normal.delete()
    115132>>> obj2.normal.delete()
    116133>>> obj3.default.delete()
    117134>>> obj4.random.delete()
     135>>> obj5.image.delete()
    118136"""}
Back to Top