Skip to content

Commit 149ce54

Browse files
authored
Added ability to pickle FilePreference
1 parent 3007198 commit 149ce54

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

dynamic_preferences/serializers.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,30 @@ def to_python(self, value, **kwargs):
275275
raise self.exception("Array {0} cannot be converted to int".format(value))
276276

277277

278+
# FieldFile also needs a model instance to save changes.
279+
class FakeInstance(object):
280+
"""
281+
FieldFile needs a model instance to update when file is persisted
282+
or deleted
283+
"""
284+
285+
def save(self):
286+
return
287+
288+
class FakeField(object):
289+
"""
290+
FieldFile needs a field object to generate a filename, persist
291+
and delete files, so we are effectively mocking that.
292+
"""
293+
294+
name = "noop"
295+
attname = "noop"
296+
max_length = 10000
297+
298+
def generate_filename(field, instance, name):
299+
return os.path.join(self.preference.get_upload_path(), f.name)
300+
301+
278302
class PreferenceFieldFile(FieldFile):
279303
"""
280304
In order to have the same API that we have with models.FileField,
@@ -285,32 +309,7 @@ class PreferenceFieldFile(FieldFile):
285309

286310
def __init__(self, preference, storage, name):
287311
super(FieldFile, self).__init__(None, name)
288-
289-
# FieldFile also needs a model instance to save changes.
290-
class FakeInstance(object):
291-
"""
292-
FieldFile needs a model instance to update when file is persisted
293-
or deleted
294-
"""
295-
296-
def save(self):
297-
return
298-
299312
self.instance = FakeInstance()
300-
301-
class FakeField(object):
302-
"""
303-
FieldFile needs a field object to generate a filename, persist
304-
and delete files, so we are effectively mocking that.
305-
"""
306-
307-
name = "noop"
308-
attname = "noop"
309-
max_length = 10000
310-
311-
def generate_filename(field, instance, name):
312-
return os.path.join(self.preference.get_upload_path(), f.name)
313-
314313
self.field = FakeField()
315314
self.storage = storage
316315
self._committed = True

0 commit comments

Comments
 (0)