Django

Code

Changeset 8638

Show
Ignore:
Timestamp:
08/27/08 16:30:47 (3 months ago)
Author:
jacob
Message:

Fixed #8534: getting the size of a file no longer opens it (at least for the built-in file-system storage). Thanks, snaury.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/files.py

    r8637 r8638  
    5454        return self.storage.url(self.name) 
    5555    url = property(_get_url) 
     56 
     57    def _get_size(self): 
     58        self._require_file() 
     59        return self.storage.size(self.name) 
     60    size = property(_get_size) 
    5661 
    5762    def open(self, mode='rb'): 
  • django/trunk/tests/regressiontests/file_storage/models.py

    r8637 r8638  
    4747>>> os.remove(p2.mugshot.path) 
    4848>>> p2.delete() 
     49 
     50# Bug #8534: FileField.size should not leave the file open. 
     51>>> p3 = Person(name="Joan") 
     52>>> p3.mugshot.save("shot", ContentFile(image_data)) 
     53 
     54# Get a "clean" model instance 
     55>>> p3 = Person.objects.get(name="Joan") 
     56 
     57# It won't have an opened file. This is a bit brittle since it depends on the 
     58# the internals of FieldFile, but there's no other way of telling if the 
     59# file's been opened or not. 
     60>>> hasattr(p3.mugshot, '_file') 
     61False 
     62 
     63# After asking for the size, the file should still be closed. 
     64>>> _ = p3.mugshot.size 
     65>>> hasattr(p3.mugshot, '_file') 
     66False 
    4967"""} 
    5068