Skip to content

Commit

Permalink
Merge pull request #465 from will-moore/MultipleFileField
Browse files Browse the repository at this point in the history
Add MultipleFileField to handle and clean multiple files
  • Loading branch information
jburel committed May 10, 2023
2 parents 4dde940 + c76f377 commit 2e1b020
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
21 changes: 21 additions & 0 deletions omeroweb/webclient/custom_forms.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2e1b020

Please sign in to comment.