if hasattr file_name_or_handle read return SimpleUploadedFile self gen

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if hasattr(file_name_or_handle, 'read'):
return SimpleUploadedFile(self.generate_file_name(), file_name_or_handle.read())
if os.path.exists(file_name_or_handle):
# if string is path
with open(file_name_or_handle, 'rb') as fh:
obj = fh.read()
else:
try:
# if string is b64 string
obj = base64.b64decode(file_name_or_handle)
except TypeError:
raise ValidationError(__('Неверный формат загружаемой картинки'))
return SimpleUploadedFile(self.generate_file_name(), obj)