filepicker / filepicker-django

A django plugin to make integrating with Filepicker.io even easier
Other
93 stars 29 forks source link

File retrieval code is duplicated #23

Closed btbonval closed 10 years ago

btbonval commented 10 years ago

Retrieving a file from Filepicker is written in two different places. Appears to be copy/paste.

It seems like FPFileField.to_python should be rewritten something like this:

def to_python(self, data):
    try:
        fd = FilepickerFile(data)
    except ValueError, e:
        if 'Not a filepicker.io URL' in str(e):
            # return None if data isn't a valid Filepicker URL
            return None
        else:
            # otherwise pass the buck
            raise e
    else:
        return fd.get_file()

The elses in the above code are not strictly necessary.

btbonval commented 10 years ago

I'm seeing a problem where FilepickerFile.get_file returns the Python file object wrapped in a Django File object. At that point, FilepickerFile is not needed, but the Django File might yet get passed around. Created ticket #25

Temporary workaround: create a os.tmpfile and write the contents of FilepickerFile.get_file().read() into it. Return the os.tmpfile object and let FilepickerFile get garbage collected with its own temporary file.