Author: jacob
Date: 2008-08-27 16:30:47 -0500 (Wed, 27 Aug 2008)
New Revision: 8638

Modified:
   django/trunk/django/db/models/fields/files.py
   django/trunk/tests/regressiontests/file_storage/models.py
Log:
Fixed #8534: getting the size of a file no longer opens it (at least for the 
built-in file-system storage). Thanks, snaury.

Modified: django/trunk/django/db/models/fields/files.py
===================================================================
--- django/trunk/django/db/models/fields/files.py       2008-08-27 21:18:45 UTC 
(rev 8637)
+++ django/trunk/django/db/models/fields/files.py       2008-08-27 21:30:47 UTC 
(rev 8638)
@@ -54,6 +54,11 @@
         return self.storage.url(self.name)
     url = property(_get_url)
 
+    def _get_size(self):
+        self._require_file()
+        return self.storage.size(self.name)
+    size = property(_get_size)
+
     def open(self, mode='rb'):
         self._require_file()
         return super(FieldFile, self).open(mode)

Modified: django/trunk/tests/regressiontests/file_storage/models.py
===================================================================
--- django/trunk/tests/regressiontests/file_storage/models.py   2008-08-27 
21:18:45 UTC (rev 8637)
+++ django/trunk/tests/regressiontests/file_storage/models.py   2008-08-27 
21:30:47 UTC (rev 8638)
@@ -46,5 +46,23 @@
 >>> p2.mugshot.save("shot", ContentFile(image_data))
 >>> os.remove(p2.mugshot.path)
 >>> p2.delete()
+
+# Bug #8534: FileField.size should not leave the file open.
+>>> p3 = Person(name="Joan")
+>>> p3.mugshot.save("shot", ContentFile(image_data))
+
+# Get a "clean" model instance
+>>> p3 = Person.objects.get(name="Joan")
+
+# It won't have an opened file. This is a bit brittle since it depends on the
+# the internals of FieldFile, but there's no other way of telling if the
+# file's been opened or not.
+>>> hasattr(p3.mugshot, '_file')
+False
+
+# After asking for the size, the file should still be closed.
+>>> _ = p3.mugshot.size
+>>> hasattr(p3.mugshot, '_file')
+False
 """}
     
\ No newline at end of file


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to