juliomalegria / django-chunked-upload

Upload large files to Django in multiple chunks, with the ability to resume if the upload is interrupted.
MIT No Attribution
218 stars 73 forks source link

Fix 'The 'file' attribute has no file associated with it.' on delete #24

Closed dtrudg closed 8 years ago

dtrudg commented 8 years ago

If something goes wrong, and a ChunkedUpload object is created, but the FileField does not have an underlying file on storage then delete() is impossible. Any attempt to delete the ChunkedUpload from the DB will raise an error:

   upload.delete()
  File "/home2/dtrudgian/Git/astrocyte/.venv/lib/python2.7/site-packages/chunked_upload/models.py", line 62, in delete
    storage, path = self.file.storage, self.file.path
  File "/home2/dtrudgian/Git/astrocyte/.venv/lib/python2.7/site-packages/django/db/models/fields/files.py", line 63, in _get_path
    self._require_file()
  File "/home2/dtrudgian/Git/astrocyte/.venv/lib/python2.7/site-packages/django/db/models/fields/files.py", line 46, in _require_file
    raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'file' attribute has no file associated with it.

This change checks to see if there is a valid file, before attempting to access file.storage, file.path and remove the file.

juliomalegria commented 8 years ago

I like it! Thanks for contributing :+1: