diff --git a/akarpov/common/forms.py b/akarpov/common/forms.py new file mode 100644 index 0000000..07a650c --- /dev/null +++ b/akarpov/common/forms.py @@ -0,0 +1,18 @@ +from django import forms + +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 \ No newline at end of file diff --git a/akarpov/music/forms.py b/akarpov/music/forms.py index 0dc7fc6..3d61ffd 100644 --- a/akarpov/music/forms.py +++ b/akarpov/music/forms.py @@ -1,9 +1,12 @@ from django import forms +from akarpov.common.forms import MultipleFileField + class TracksLoadForm(forms.Form): address = forms.CharField(max_length=500) + class FileUploadForm(forms.Form): - file = forms.FileField(widget=forms.ClearableFileInput(attrs={"multiple": True})) + file = MultipleFileField()