Skip to content

Commit

Permalink
Fix codingjoe#309 -- handle submit buttons form* attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gary02 committed Oct 14, 2024
1 parent b852632 commit ddc1835
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ target/
node_modules

*.min.js
.python-version
14 changes: 12 additions & 2 deletions s3file/static/s3file/js/s3file.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
form.appendChild(submitInput)
}

function uploadS3Inputs (form) {
function uploadS3Inputs (form, submitter) {
window.uploading = 0
form.loaded = 0
form.total = 0
Expand All @@ -136,6 +136,15 @@
window.uploading += 1
uploadFiles(form, input, input.name)
})
// override form attributes with submit button attributes
if (submitter) {
form.action = submitter.getAttribute('formaction') || form.action
form.method = submitter.getAttribute('formmethod') || form.method
form.enctype = submitter.getAttribute('formEnctype') || form.enctype
form.formnovalidate = submitter.getAttribute('formnovalidate') || form.novalidate
form.target = submitter.getAttribute('formtarget') || form.target
}

waitForAllFiles(form)
}

Expand All @@ -147,7 +156,8 @@
forms.forEach(function (form) {
form.addEventListener('submit', function (e) {
e.preventDefault()
uploadS3Inputs(e.target)
console.log(e.submitter)
uploadS3Inputs(e.target, e.submitter)
})
var submitButtons = form.querySelectorAll('input[type=submit], button[type=submit]')
Array.from(submitButtons).forEach(function (submitButton) {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_file_insert_submit_value(
with wait_for_page_load(driver, timeout=10):
save_button.click()
assert "save" in driver.page_source
assert "formaction" not in driver.page_source

driver.get(live_server + self.create_url)
file_input = driver.find_element(By.XPATH, "//input[@name='file']")
Expand All @@ -197,6 +198,7 @@ def test_file_insert_submit_value(
save_button.click()
assert "save_continue" in driver.page_source
assert "continue_value" in driver.page_source
assert "formaction" in driver.page_source

@pytest.mark.selenium
def test_progress(self, driver, live_server, upload_file, freeze_upload_folder):
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" name="save" value="Save"/>
<button type="submit" name="save_continue" value="continue_value">Save &amp; continue</button>
<input type="submit" name="save" value="Save" />
<button type="submit" name="save_continue" value="continue_value" formaction="?formaction=1">Save &amp; continue</button>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0"
aria-valuemax="100">0%
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def form_valid(self, form):
"file": self.request.FILES.getlist("file"),
"other_file": self.request.FILES.getlist("other_file"),
},
"QUERY_STRING": self.request.GET,
},
status=201,
encoder=FileEncoder,
Expand All @@ -45,6 +46,7 @@ def form_valid(self, form):
"file": self.request.FILES.getlist("file"),
"other_file": self.request.FILES.getlist("other_file"),
},
"QUERY_STRING": self.request.GET,
},
status=201,
encoder=FileEncoder,
Expand Down

0 comments on commit ddc1835

Please sign in to comment.