Skip to content

Fix content-type POST conditions #83

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

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions s3file/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ def get_conditions(self, accept):
["starts-with", "$key", self.upload_folder],
{"success_action_status": "201"},
]
if accept:
accept = accept.replace(' ', '') # remove whitespaces
mime_types = accept.split(',') if accept else [] # catch empty string
for mime_type in mime_types:
top_type, sub_type = mime_type.split('/', 1)
if sub_type == '*':
conditions.append(["starts-with", "$Content-Type", "%s/" % top_type])
else:
conditions.append({"Content-Type": mime_type})
if accept and ',' not in accept:
top_type, sub_type = accept.split('/', 1)
if sub_type == '*':
conditions.append(["starts-with", "$Content-Type", "%s/" % top_type])
else:
conditions.append({"Content-Type": accept})
else:
conditions.append(["starts-with", "$Content-Type", ""])

Expand Down
11 changes: 2 additions & 9 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,11 @@ def test_accept(self):

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

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

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

Expand Down