jazzband / django-formtools

A set of high-level abstractions for Django forms
https://django-formtools.readthedocs.io
BSD 3-Clause "New" or "Revised" License
794 stars 135 forks source link

Optional file inputs are not cleared if the user deselects the file #191

Closed ShaheedHaque closed 2 years ago

ShaheedHaque commented 3 years ago

Let say you have a with steps Form1 and Form2, where Form1 has a file input, which is optional. If the user takes the following actions:

At this point, the expected behaviour is that:

  1. self.storage.data[self.storage.step_files_key]['Form1'] should be empty.
  2. self.storage.file_storage.delete() should have been called for the file for x.txt.

However, what actually happens is that the code in https://github.com/jazzband/django-formtools/blob/3b161670549503ae4ed81f9e0a43f2d12fc73e41/formtools/wizard/storage/base.py#L105 can only add selected files, and does not remove deselected files.

ShaheedHaque commented 3 years ago

Here is a prototypical fix. Change https://github.com/jazzband/django-formtools/blob/3b161670549503ae4ed81f9e0a43f2d12fc73e41/formtools/wizard/views.py#L297 to look something like this:

    #
    # The files storage is sticky w.r.t. optional files.
    #
    for file in list(self.storage.current_step_files or []):
        if file not in form.files:
            #
            # Remove the previously selected file.
            #
            file = self.storage.data[self.storage.step_files_key][self.steps.current].pop(file)
            self.storage.file_storage.delete(file['tmp_name'])
    self.storage.set_step_files(self.steps.current, self.process_step_files(form))
ShaheedHaque commented 2 years ago

Obsoleted in favour of #207.