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 4 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
26 changes: 23 additions & 3 deletions omeroweb/webclient/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@
) % help_button


#################################################################
# Custom widget and validation for multiple file uploads
Copy link
Member

@sbesson sbesson May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having looked at how the code is organized, all custom widget/fields are declared omeroweb.webclient.custom_forms and imported here to construct the webclient forms.



class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment and/or a reference to the documentation explaining why we are subclassing these Django classes.



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


#################################################################
# Non-model Form

Expand Down Expand Up @@ -334,9 +356,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