From 49332c5d6e81033ce84f01d7dcf12daac3c7f614 Mon Sep 17 00:00:00 2001 From: dandds Date: Mon, 10 Jun 2019 19:26:44 -0400 Subject: [PATCH 1/2] Only raise FileLength validation error on files --- atst/forms/validators.py | 7 +++++-- tests/forms/test_validators.py | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/atst/forms/validators.py b/atst/forms/validators.py index 99265769..82dc1182 100644 --- a/atst/forms/validators.py +++ b/atst/forms/validators.py @@ -1,7 +1,10 @@ +from datetime import datetime import re + +from werkzeug.datastructures import FileStorage from wtforms.validators import ValidationError, StopValidation import pendulum -from datetime import datetime + from atst.utils.localization import translate @@ -103,7 +106,7 @@ def RequiredIf(criteria_function, message=translate("forms.validators.is_require def FileLength(max_length=50000000, message=None): def _file_length(_form, field): - if field.data is None: + if field.data is None or not isinstance(field.data, FileStorage): return True content = field.data.read() diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index c57fe5e0..8f0ae0e5 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -100,3 +100,6 @@ class TestFileLength: with pytest.raises(ValidationError): validator(dummy_form, dummy_field) + + dummy_field.data = "random string" + assert validator(dummy_form, dummy_field) From 0e89a55c07612631ff8667dfe47635da4e034e2f Mon Sep 17 00:00:00 2001 From: dandds Date: Tue, 11 Jun 2019 09:58:32 -0400 Subject: [PATCH 2/2] 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. --- atst/routes/task_orders/new.py | 7 ++++++- js/components/upload_input.js | 14 ++++++++++---- templates/components/upload_input.html | 2 +- tests/routes/task_orders/test_new.py | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/atst/routes/task_orders/new.py b/atst/routes/task_orders/new.py index 1f6a271b..bfce2c26 100644 --- a/atst/routes/task_orders/new.py +++ b/atst/routes/task_orders/new.py @@ -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 diff --git a/js/components/upload_input.js b/js/components/upload_input.js index 843bdf62..a94d394a 100644 --- a/js/components/upload_input.js +++ b/js/components/upload_input.js @@ -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 + }, }, } diff --git a/templates/components/upload_input.html b/templates/components/upload_input.html index f2835baf..8eaa43dd 100644 --- a/templates/components/upload_input.html +++ b/templates/components/upload_input.html @@ -20,7 +20,7 @@ {{ field.label }} {% endif %} {{ field.description }} -
+