Skip to content

Commit 875555c

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 875555c

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
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-8
Original file line numberDiff line numberDiff line change
@@ -119,17 +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/*')
133127

134128
def test_no_js_error(self, driver, live_server):
135129
driver.get(live_server + self.url)

0 commit comments

Comments
 (0)