Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultipleFileField to handle and clean multiple files #465

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions omeroweb/webclient/custom_forms.py
Expand Up @@ -379,3 +379,24 @@ def clean(self, value):
else:
final_values.append(val)
return final_values


# Custom widget and validation for multiple file uploads
# See https://docs.djangoproject.com/en/3.2/topics/http/
# file-uploads/#uploading-multiple-files
class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True


class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)

def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result
5 changes: 2 additions & 3 deletions omeroweb/webclient/forms.py
Expand Up @@ -34,6 +34,7 @@

from omeroweb.custom_forms import NonASCIIForm
from .custom_forms import MetadataModelChoiceField
from .custom_forms import MultipleFileField
from .custom_forms import AnnotationModelMultipleChoiceField
from .custom_forms import ObjectModelMultipleChoiceField
from omeroweb.webadmin.custom_forms import ExperimenterModelMultipleChoiceField
Expand Down Expand Up @@ -334,9 +335,7 @@ def __init__(self, *args, **kwargs):
required=False,
)

annotation_file = forms.FileField(
widget=forms.ClearableFileInput(attrs={"multiple": True}), required=False
)
annotation_file = MultipleFileField(required=False)


class CommentAnnotationForm(BaseAnnotationForm):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -54,7 +54,7 @@ def read(fname):
"omero-py>=5.7.0",
# minimum requirements for `omero web start`
"concurrent-log-handler>=0.9.20",
"Django==3.2.18,<4.0",
"Django>=3.2.19,<4.0",
"django-pipeline==2.0.7",
"django-cors-headers==3.7.0",
"whitenoise>=5.3.0",
Expand Down