Display and overwrite task order uploads correctly.

- Populate the POSTed form with additional data from the existing TO
  model. This way the pdf attachment has to be explicitly overwritten.
- Adjust the frontend so that if there is an existing PDF, it only sends
  a file input back if the user removes the existing PDF.
This commit is contained in:
dandds 2019-06-11 09:58:32 -04:00
parent 49332c5d6e
commit 0e89a55c07
4 changed files with 18 additions and 7 deletions

View File

@ -42,7 +42,12 @@ def edit(portfolio_id, task_order_id=None):
def update(portfolio_id, task_order_id=None):
form_data = {**http_request.form, **http_request.files}
form = TaskOrderForm(form_data)
form = None
if task_order_id:
task_order = TaskOrders.get(task_order_id)
form = TaskOrderForm(form_data, obj=task_order)
else:
form = TaskOrderForm(form_data)
if form.validate():
task_order = None

View File

@ -26,24 +26,27 @@ export default {
data: function() {
return {
hasInitialData: !!this.initialData,
attachment: this.initialData || null,
showErrors: this.initialErrors,
changed: false,
}
},
methods: {
showUploadInput: function() {
this.showUpload = true
},
addAttachment: function(e) {
this.attachment = e.target.value
this.showErrors = false
this.changed = true
},
removeAttachment: function(e) {
e.preventDefault()
this.attachment = null
this.$refs.attachmentInput.value = null
if (this.$refs.attachmentInput) {
this.$refs.attachmentInput.value = null
}
this.showErrors = false
this.changed = true
},
},
@ -56,5 +59,8 @@ export default {
hasAttachment: function() {
return !!this.attachment
},
hideInput: function() {
return this.hasInitialData && !this.changed
},
},
}

View File

@ -20,7 +20,7 @@
{{ field.label }}
{% endif %}
{{ field.description }}
<div class="upload-widget">
<div v-if="!hideInput" class="upload-widget">
<label class="upload-label" for="{{ field.name }}">
<span class="upload-button">
Browse

View File

@ -145,7 +145,7 @@ def test_task_orders_update_pdf(
def test_task_orders_update_delete_pdf(client, user_session, portfolio, pdf_upload):
user_session(portfolio.owner)
task_order = TaskOrderFactory.create(pdf=pdf_upload)
data = {"number": "0123456789", "pdf": None}
data = {"number": "0123456789", "pdf": ""}
response = client.post(
url_for(
"task_orders.update", portfolio_id=portfolio.id, task_order_id=task_order.id