From e51a9012fdbadb95333e49a49eeda50d64a949ff Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Wed, 23 Jan 2019 10:30:30 -0500 Subject: [PATCH 1/3] Add custom JSON encoder to handle attachment objects --- atst/utils/json.py | 9 +++++++++ tests/utils/test_json.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 atst/utils/json.py create mode 100644 tests/utils/test_json.py diff --git a/atst/utils/json.py b/atst/utils/json.py new file mode 100644 index 00000000..a489fcaa --- /dev/null +++ b/atst/utils/json.py @@ -0,0 +1,9 @@ +from flask.json import JSONEncoder +from atst.models.attachment import Attachment + + +class CustomJSONEncoder(JSONEncoder): + def default(self, obj): + if isinstance(obj, Attachment): + return obj.filename + return JSONEncoder.default(self, obj) diff --git a/tests/utils/test_json.py b/tests/utils/test_json.py new file mode 100644 index 00000000..5db25c17 --- /dev/null +++ b/tests/utils/test_json.py @@ -0,0 +1,15 @@ +import json +from atst.utils.json import CustomJSONEncoder + +from tests.factories import AttachmentFactory + + +encoder = CustomJSONEncoder() + + +def test_custom_encoder_serializes_attachments(): + filename = "jar_jar_is_secretly_a_sith_lord.pdf" + attachment = AttachmentFactory.create(filename=filename) + encoded = encoder.encode({"file": attachment}) + expected = json.dumps({"file": filename}) + assert encoded == expected From 849238f2181425651fefb376f73b1b17500fe5fa Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Wed, 23 Jan 2019 10:31:01 -0500 Subject: [PATCH 2/3] Use custom JSON encoder in flask app --- atst/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atst/app.py b/atst/app.py index 465d89a4..7d5cc0c6 100644 --- a/atst/app.py +++ b/atst/app.py @@ -26,6 +26,7 @@ from atst.models.permissions import Permissions from atst.eda_client import MockEDAClient from atst.utils import mailer from atst.utils.form_cache import FormCache +from atst.utils.json import CustomJSONEncoder from atst.queue import queue @@ -41,6 +42,7 @@ def make_app(config): template_folder=parent_dir.child("templates").absolute(), static_folder=parent_dir.child("static").absolute(), ) + app.json_encoder = CustomJSONEncoder make_redis(app, config) csrf = CSRFProtect() From b3270a46cf520ddd51ca9da5e919358589259981 Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Wed, 23 Jan 2019 10:31:35 -0500 Subject: [PATCH 3/3] Remove custom JSON filter --- atst/filters.py | 14 -------------- templates/requests/financial_verification.html | 2 +- templates/task_orders/new/funding.html | 2 +- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/atst/filters.py b/atst/filters.py index da7047b3..6eeb4229 100644 --- a/atst/filters.py +++ b/atst/filters.py @@ -49,19 +49,6 @@ def getOptionLabel(value, options): return -def mixedContentToJson(value): - """ - This coerces the file upload in form data to its filename - so that the data can be JSON serialized. - """ - if isinstance(value, dict): - for k, v in value.items(): - if hasattr(v, "filename"): - value[k] = v.filename - - return app.jinja_env.filters["tojson"](value) - - def findFilter(value, filter_name, filter_args=[]): if not filter_name: return value @@ -124,7 +111,6 @@ def register_filters(app): app.jinja_env.filters["usPhone"] = usPhone app.jinja_env.filters["readableInteger"] = readableInteger app.jinja_env.filters["getOptionLabel"] = getOptionLabel - app.jinja_env.filters["mixedContentToJson"] = mixedContentToJson app.jinja_env.filters["findFilter"] = findFilter app.jinja_env.filters["renderList"] = renderList app.jinja_env.filters["formattedDate"] = formattedDate diff --git a/templates/requests/financial_verification.html b/templates/requests/financial_verification.html index a9d3ea83..b66ab719 100644 --- a/templates/requests/financial_verification.html +++ b/templates/requests/financial_verification.html @@ -21,7 +21,7 @@ {{ Alert(("requests.financial_verification.pending_financial_verification" | translate), fragment="fragments/pending_financial_verification.html") }} {% endif %} - +
{% if extended %} {{ Alert(("requests.financial_verification.manually_enter_task_information_label" | translate), diff --git a/templates/task_orders/new/funding.html b/templates/task_orders/new/funding.html index 3f026272..79efa6d6 100644 --- a/templates/task_orders/new/funding.html +++ b/templates/task_orders/new/funding.html @@ -14,7 +14,7 @@