Skip to content
Snippets Groups Projects
Commit f59a2e84 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Totally rework how the multifile upload works

parent 7354ea71
No related branches found
No related tags found
No related merge requests found
from django.forms import FileInput, FileField from django.forms import ClearableFileInput, FileField, CheckboxInput
class MultiFileInput(FileInput): class MultiFileInput(ClearableFileInput):
""" """
File Input only returns one file from its clean method. File Input only returns one file from its clean method.
This passes all files through the clean method and means we have a list of This passes all files through the clean method and means we have a list of
files available for post processing files available for post processing
""" """
def __init__(self, *args, attrs={}, **kwargs): template_name = 'stream_forms/fields/multi_file_field.html'
attrs['multiple'] = True
super().__init__(*args, attrs=attrs, **kwargs) input_text = ''
def __init__(self, *args, **kwargs):
self.multiple = kwargs.pop('multiple', True)
super().__init__(*args, **kwargs)
def is_initial(self, value):
is_initial = super().is_initial
return all(
is_initial(file) for file in value
)
def render(self, name, value, attrs=None):
if self.multiple:
attrs['multiple'] = 'multiple'
return super().render(name, value, attrs)
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
return files.getlist(name) if hasattr(files, 'getlist'):
upload = files.getlist(name)
else:
upload = files.get(name)
if not isinstance(upload, list):
upload = [upload]
checkbox_name = self.clear_checkbox_name(name)
checkboxes = {k for k in data if checkbox_name in k}
cleared = {
i for i, checkbox in enumerate(checkboxes)
if CheckboxInput().value_from_datadict(data, files, checkbox)
}
return {
'files': upload,
'cleared': cleared,
}
class MultiFileField(FileField): class MultiFileField(FileField):
widget = MultiFileInput widget = MultiFileInput
def clean(self, value, initial): def clean(self, value, initial):
if not value: files = value['files']
cleared = value['cleared']
if not files and not cleared:
return initial return initial
return [FileField().clean(file, initial) for file in value] new = [FileField().clean(file, initial) for file in files]
old = [file for i, file in enumerate(initial) if i not in cleared]
return old + new
{% if widget.is_initial %}{{ widget.initial_text }}:
<p>
{{ widget.clear_checkbox_label }}
</p>
{% for file in widget.value %}
<p>
<input type="checkbox" name="{{ widget.checkbox_name }}-{{ forloop.counter }}" id="{{ widget.checkbox_id }}-{{ forloop.counter }}">
<label for="{{ widget.checkbox_id }}-{{ forloop.counter }}"></label>
<a href="{{ file.url }}">{{ file.filename }}</a>
</p>
{% endfor %}
{{ widget.input_text }}{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment