Skip to content

Commit 7e7656d

Browse files
committed
Fix -- Add multi mimetype support for accept attr
1 parent dba918f commit 7e7656d

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

s3file/forms.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def client(self):
2828
def build_attrs(self, *args, **kwargs):
2929
attrs = super().build_attrs(*args, **kwargs)
3030

31-
mime_type = attrs.get('accept', None)
31+
accept = attrs.get('accept')
3232
response = self.client.generate_presigned_post(
3333
self.bucket_name, os.path.join(self.upload_folder, '${filename}'),
34-
Conditions=self.get_conditions(mime_type),
34+
Conditions=self.get_conditions(accept),
3535
ExpiresIn=self.expires,
3636
)
3737

@@ -48,18 +48,21 @@ def build_attrs(self, *args, **kwargs):
4848
defaults['class'] = 's3file'
4949
return defaults
5050

51-
def get_conditions(self, mime_type):
51+
def get_conditions(self, accept):
5252
conditions = [
5353
{"bucket": self.bucket_name},
5454
["starts-with", "$key", self.upload_folder],
5555
{"success_action_status": "201"},
5656
]
57-
if mime_type:
58-
top_type, sub_type = mime_type.split('/', 1)
59-
if sub_type == '*':
60-
conditions.append(["starts-with", "$Content-Type", "%s/" % top_type])
61-
else:
62-
conditions.append({"Content-Type": mime_type})
57+
if accept:
58+
accept = accept.replace(' ', '') # remove whitespaces
59+
mime_types = accept.split(',') if accept else [] # catch empty string
60+
for mime_type in mime_types:
61+
top_type, sub_type = mime_type.split('/', 1)
62+
if sub_type == '*':
63+
conditions.append(["starts-with", "$Content-Type", "%s/" % top_type])
64+
else:
65+
conditions.append({"Content-Type": mime_type})
6366
else:
6467
conditions.append(["starts-with", "$Content-Type", ""])
6568

tests/test_forms.py

+14
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def test_accept(self):
117117
assert 'accept="image/jpeg"' in widget.render(name='file', value='test.jpg')
118118
assert {"Content-Type": 'image/jpeg'} in widget.get_conditions('image/jpeg')
119119

120+
widget = ClearableFileInput(attrs={'accept': 'application/pdf,image/*'})
121+
assert 'accept="application/pdf,image/*"' in widget.render(name='file', value='test.jpg')
122+
assert ["starts-with", "$Content-Type", "image/"] in widget.get_conditions(
123+
'application/pdf,image/*')
124+
assert {"Content-Type": 'application/pdf'} in widget.get_conditions(
125+
'application/pdf,image/*')
126+
127+
widget = ClearableFileInput(attrs={'accept': 'application/pdf, image/*'})
128+
assert 'accept="application/pdf, image/*"' in widget.render(name='file', value='test.jpg')
129+
assert ["starts-with", "$Content-Type", "image/"] in widget.get_conditions(
130+
'application/pdf, image/*')
131+
assert {"Content-Type": 'application/pdf'} in widget.get_conditions(
132+
'application/pdf, image/*')
133+
120134
def test_no_js_error(self, driver, live_server):
121135
driver.get(live_server + self.url)
122136

0 commit comments

Comments
 (0)