Ticket #8501: 8501v1.diff
File 8501v1.diff, 4.5 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/files.py
55 55 url = property(_get_url) 56 56 57 57 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 60 62 size = property(_get_size) 61 63 62 64 def open(self, mode='rb'): … … 74 76 self._name = self.storage.save(name, content) 75 77 setattr(self.instance, self.field.name, self.name) 76 78 77 # Update the filesize cache 78 self._size = len(content) 79 # Delete the filesize cache 80 if hasattr(self, '_size'): 81 del self._size 79 82 80 83 # Save the object because it has changed, unless save is False 81 84 if save: … … 205 208 206 209 class ImageFieldFile(ImageFile, FieldFile): 207 210 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 210 215 216 super(ImageFieldFile, self).save(name, content, save) 217 211 218 # Update width/height fields, if needed 212 219 if self.field.width_field: 213 220 setattr(self.instance, self.field.width_field, self.width) 214 221 if self.field.height_field: 215 222 setattr(self.instance, self.field.height_field, self.height) 216 223 217 super(ImageFieldFile, self).save(name, content, save)218 219 224 def delete(self, save=True): 220 225 # Clear the image dimensions cache 221 226 if hasattr(self, '_dimensions_cache'): -
django/core/files/storage.py
150 150 file_move_safe(content.temporary_file_path(), full_path) 151 151 content.close() 152 152 153 # This is a normal uploadedfile that we can stream.154 153 else: 155 154 # This fun binary flag incantation makes os.open throw an 156 155 # OSError if the file already exists before we open it. 157 156 fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0)) 158 157 try: 159 158 locks.lock(fd, locks.LOCK_EX) 159 # first try accessing content as an uploadedfile 160 160 for chunk in content.chunks(): 161 161 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) 162 166 finally: 163 167 locks.unlock(fd) 164 168 os.close(fd) -
tests/modeltests/files/models.py
6 6 """ 7 7 8 8 import tempfile 9 from urllib2 import urlopen 9 10 10 11 from django.db import models 11 12 from django.core.files.base import ContentFile … … 32 33 random = models.FileField(storage=temp_storage, upload_to=random_upload_to) 33 34 default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt') 34 35 36 image = models.ImageField(storage=temp_storage, upload_to='tests') 37 35 38 __test__ = {'API_TESTS':""" 36 39 # An object without a file has limited functionality. 37 40 … … 109 112 >>> obj4.random 110 113 <FieldFile: .../random_file> 111 114 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 123 True 124 >>> obj5.image.width 125 80L 126 >>> obj5.image.height 127 15L 128 112 129 # Clean up the temporary files. 113 130 114 131 >>> obj1.normal.delete() 115 132 >>> obj2.normal.delete() 116 133 >>> obj3.default.delete() 117 134 >>> obj4.random.delete() 135 >>> obj5.image.delete() 118 136 """}