Skip to content

Commit 03fa62b

Browse files
committed
Fix content-type POST conditions
POST conditions seem to be exclusive, so all conditions have to match not only one of them. Therefore we need to remove the condition if multiple content types are expected.
1 parent 5b978c1 commit 03fa62b

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

s3file/forms.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ def get_conditions(self, accept):
5454
["starts-with", "$key", self.upload_folder],
5555
{"success_action_status": "201"},
5656
]
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})
57+
if accept and ',' not in accept:
58+
top_type, sub_type = accept.split('/', 1)
59+
if sub_type == '*':
60+
conditions.append(["starts-with", "$Content-Type", "%s/" % top_type])
61+
else:
62+
conditions.append({"Content-Type": accept})
6663
else:
6764
conditions.append(["starts-with", "$Content-Type", ""])
6865

tests/test_forms.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,11 @@ def test_accept(self):
119119

120120
widget = ClearableFileInput(attrs={'accept': 'application/pdf,image/*'})
121121
assert 'accept="application/pdf,image/*"' in widget.render(name='file', value='test.jpg')
122-
assert ["starts-with", "$Content-Type", "image/"] in widget.get_conditions(
122+
assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(
123123
'application/pdf,image/*')
124-
assert {"Content-Type": 'application/pdf'} in widget.get_conditions(
124+
assert {"Content-Type": 'application/pdf'} not in widget.get_conditions(
125125
'application/pdf,image/*')
126126

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-
134127
def test_no_js_error(self, driver, live_server):
135128
driver.get(live_server + self.url)
136129

0 commit comments

Comments
 (0)