Added get_proxy_object helper to filefields (#391)

This commit is contained in:
Ross Lawley 2013-07-10 21:19:11 +00:00
parent 1785ced655
commit d9f538170b
3 changed files with 35 additions and 10 deletions

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.8.3 Changes in 0.8.3
================ ================
- Added get_proxy_object helper to filefields (#391)
- Added QuerySetNoCache and QuerySet.no_cache() for lower memory consumption (#365) - Added QuerySetNoCache and QuerySet.no_cache() for lower memory consumption (#365)
- Fixed sum and average mapreduce dot notation support (#375, #376, #393) - Fixed sum and average mapreduce dot notation support (#375, #376, #393)
- Fixed as_pymongo to return the id (#386) - Fixed as_pymongo to return the id (#386)

View File

@ -1190,7 +1190,7 @@ class FileField(BaseField):
# Check if a file already exists for this model # Check if a file already exists for this model
grid_file = instance._data.get(self.name) grid_file = instance._data.get(self.name)
if not isinstance(grid_file, self.proxy_class): if not isinstance(grid_file, self.proxy_class):
grid_file = self.get_proxy_obj(key=key, instance=instance) grid_file = self.get_proxy_obj(key=self.name, instance=instance)
instance._data[self.name] = grid_file instance._data[self.name] = grid_file
if not grid_file.key: if not grid_file.key:
@ -1225,9 +1225,9 @@ class FileField(BaseField):
if collection_name is None: if collection_name is None:
collection_name = self.collection_name collection_name = self.collection_name
return self.proxy_class(key=key, instance=instance, return self.proxy_class(key=key, instance=instance,
db_alias=db_alias, db_alias=db_alias,
collection_name=collection_name) collection_name=collection_name)
def to_mongo(self, value): def to_mongo(self, value):
# Store the GridFS file id in MongoDB # Store the GridFS file id in MongoDB
@ -1261,10 +1261,8 @@ class ImageGridFsProxy(GridFSProxy):
applying field properties (size, thumbnail_size) applying field properties (size, thumbnail_size)
""" """
field = self.instance._fields[self.key] field = self.instance._fields[self.key]
# if the field from the instance has an attribute field # Handle nested fields
# we use that one and hope for the best. Usually only container if hasattr(field, 'field') and isinstance(field.field, FileField):
# fields have a field attribute.
if hasattr(field, 'field'):
field = field.field field = field.field
try: try:

View File

@ -455,5 +455,31 @@ class FileTest(unittest.TestCase):
self.assertEqual(1, TestImage.objects(Q(image1=grid_id) self.assertEqual(1, TestImage.objects(Q(image1=grid_id)
or Q(image2=grid_id)).count()) or Q(image2=grid_id)).count())
def test_complex_field_filefield(self):
"""Ensure you can add meta data to file"""
class Animal(Document):
genus = StringField()
family = StringField()
photos = ListField(FileField())
Animal.drop_collection()
marmot = Animal(genus='Marmota', family='Sciuridae')
marmot_photo = open(TEST_IMAGE_PATH, 'rb') # Retrieve a photo from disk
photos_field = marmot._fields['photos'].field
new_proxy = photos_field.get_proxy_obj('photos', marmot)
new_proxy.put(marmot_photo, content_type='image/jpeg', foo='bar')
marmot_photo.close()
marmot.photos.append(new_proxy)
marmot.save()
marmot = Animal.objects.get()
self.assertEqual(marmot.photos[0].content_type, 'image/jpeg')
self.assertEqual(marmot.photos[0].foo, 'bar')
self.assertEqual(marmot.photos[0].get().length, 8313)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()