@@ -39,14 +39,18 @@ def _get_unique_filename(name):
39
39
# The awesome Mongoengine ImageGridFsProxy wants to pull a field
40
40
# from a document to get necessary data. Trouble is that this doesn't work
41
41
# if the ImageField is stored on List or MapField. So we pass a nice fake
42
- # document to
42
+ # document to the proxy to get saving the file done. Yeah it really is that ugly.
43
43
class FakeDocument (object ):
44
+ _fields = {}
45
+
44
46
def __init__ (self , key , field ):
45
47
super (FakeDocument , self ).__init__ ()
46
- if not hasattr (self , '_fields' ):
47
- self ._fields = {}
48
- self ._fields .update ({key : field })
49
-
48
+
49
+ self ._fields [key ] = field
50
+
51
+ # don't care if anything gets marked on this
52
+ # we do update a real field later though. That should
53
+ # trigger the same thing on the real document though.
50
54
def _mark_as_changed (self , key ):
51
55
pass
52
56
@@ -90,30 +94,36 @@ def construct_instance(form, instance, fields=None, exclude=None, ignore=None):
90
94
file_data = map_field .get (key , None )
91
95
uploaded_file .seek (0 )
92
96
filename = _get_unique_filename (uploaded_file .name )
93
- # add a file
94
- _fake_document = FakeDocument (f .name , f .field )
97
+ fake_document = FakeDocument (f .name , f .field )
95
98
overwrote_instance = False
96
99
overwrote_key = False
100
+ # save a new file
97
101
if file_data is None :
98
- proxy = f .field .proxy_class (instance = _fake_document , key = f .name )
102
+ proxy = f .field .proxy_class (instance = fake_document , key = f .name ,
103
+ db_alias = f .field .db_alias ,
104
+ collection_name = f .field .collection_name )
99
105
proxy .put (uploaded_file , content_type = uploaded_file .content_type , filename = filename )
106
+ proxy .close ()
100
107
proxy .instance = None
101
108
proxy .key = None
102
109
map_field [key ] = proxy
110
+ # overwrite an existing file
103
111
else :
104
112
if file_data .instance is None :
113
+ file_data .instance = fake_document
105
114
overwrote_instance = True
106
- file_data .instance = _fake_document
107
115
if file_data .key is None :
108
116
file_data .key = f .name
109
117
overwrote_key = True
110
118
file_data .delete ()
111
119
file_data .put (uploaded_file , content_type = uploaded_file .content_type , filename = filename )
120
+ file_data .close ()
112
121
if overwrote_instance :
113
122
file_data .instance = None
114
123
if overwrote_key :
115
124
file_data .key = None
116
125
map_field [key ] = file_data
126
+ print map_field
117
127
setattr (instance , f .name , map_field )
118
128
else :
119
129
field = getattr (instance , f .name )
0 commit comments