From 896ca9f4237b2bc548f348f0b56b48e97c0d4e93 Mon Sep 17 00:00:00 2001 From: dhakim87 Date: Fri, 25 Sep 2020 18:34:39 -0700 Subject: [PATCH 1/3] Uses vioscreen comms in microsetta-private-api to pull a pdf and show it embedded in an html page --- microsetta_interface/implementation.py | 57 ++++++++++++++++--- microsetta_interface/routes.yaml | 50 ++++++++++++++++ .../templates/embedded_pdf.jinja2 | 24 ++++++++ microsetta_interface/templates/source.jinja2 | 4 +- 4 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 microsetta_interface/templates/embedded_pdf.jinja2 diff --git a/microsetta_interface/implementation.py b/microsetta_interface/implementation.py index 0f166648..8e5e09b3 100644 --- a/microsetta_interface/implementation.py +++ b/microsetta_interface/implementation.py @@ -1,5 +1,5 @@ import flask -from flask import render_template, session, redirect +from flask import render_template, session, redirect, make_response import jwt import requests from requests.auth import AuthBase @@ -804,6 +804,44 @@ def get_to_save_vioscreen_remote_sample_survey(*, return _refresh_state_and_route_to_sink(account_id, source_id) +@prerequisite([SOURCE_PREREQS_MET]) +def top_food_report(*, + account_id=None, + source_id=None, + survey_id=None): + return _render_with_defaults( + "embedded_pdf.jinja2", + page_title="Top Food Report", + link_to_pdf='/accounts/%s' + '/sources/%s' + '/surveys/%s' + '/reports/topfoodreport/pdf' + % (account_id, source_id, survey_id)) + + +@prerequisite([SOURCE_PREREQS_MET]) +def top_food_report_pdf(*, + account_id=None, + source_id=None, + survey_id=None): + has_error, pdf_bytes, _ = ApiRequest.get( + '/accounts/%s/sources/%s/surveys/%s/reports/topfoodreport' % + (account_id, source_id, survey_id), + parse_json=False + ) + if has_error: + return pdf_bytes + + response = make_response(pdf_bytes) + response.headers.set("Content-Type", "application/pdf") + # TODO: Do we want it to download a file or be embedded in the html? + # response.headers.set('Content-Disposition', + # 'attachment', + # filename='top-food-report.pdf') + + return response + + @prerequisite([SOURCE_PREREQS_MET]) def get_source(*, account_id=None, source_id=None): # Retrieve the account to determine which kit it was created with @@ -864,7 +902,7 @@ def get_source(*, account_id=None, source_id=None): # Identify answered surveys for the samples for sample in samples_output: - sample['ffq'] = False + sample['ffq'] = None sample_id = sample['sample_id'] # TODO: This is a really awkward and slow way to get this information has_error, per_sample_answers, _ = ApiRequest.get( @@ -875,7 +913,7 @@ def get_source(*, account_id=None, source_id=None): for answer in per_sample_answers: if answer['survey_template_id'] == VIOSCREEN_ID: - sample['ffq'] = True + sample['ffq'] = answer['survey_id'] # prettify datetime needs_assignment = False @@ -1164,7 +1202,7 @@ def build_params(cls, params): return all_params @classmethod - def _check_response(cls, response): + def _check_response(cls, response, parse_json=True): error_code = response.status_code output = None headers = None @@ -1178,20 +1216,23 @@ def _check_response(cls, response): else: error_code = 0 # there is a response code but no *error* code headers = response.headers - if response.text: - output = response.json() + if parse_json: + if response.text: + output = response.json() + else: + output = response.content return error_code, output, headers @classmethod - def get(cls, input_path, params=None): + def get(cls, input_path, parse_json=True, params=None): response = requests.get( ApiRequest.API_URL + input_path, auth=BearerAuth(session[TOKEN_KEY_NAME]), verify=ApiRequest.CAfile, params=cls.build_params(params)) - return cls._check_response(response) + return cls._check_response(response, parse_json=parse_json) @classmethod def put(cls, input_path, params=None, json=None): diff --git a/microsetta_interface/routes.yaml b/microsetta_interface/routes.yaml index e68c3aeb..65536923 100644 --- a/microsetta_interface/routes.yaml +++ b/microsetta_interface/routes.yaml @@ -494,6 +494,45 @@ paths: '302': description: Redirecting to necessary action + '/accounts/{account_id}/sources/{source_id}/surveys/{survey_id}/reports/topfoodreport': + # Disambiguation page for view/download pdf + get: + operationId: microsetta_interface.implementation.top_food_report + tags: + - Survey + summary: Generate vioscreen top food report + description: Generate vioscreen top food report + parameters: + - $ref: '#/components/parameters/account_id' + - $ref: '#/components/parameters/source_id' + - $ref: '#/components/parameters/survey_id' + + responses: + '200': + description: Top Food Report pdf + content: + text/html: + schema: + type: string + '302': + description: Redirecting to necessary action + + '/accounts/{account_id}/sources/{source_id}/surveys/{survey_id}/reports/topfoodreport/pdf': + # Attempts to generate the vioscreen top food report pdf from this vioscreen survey + get: + operationId: microsetta_interface.implementation.top_food_report_pdf + tags: + - Survey + summary: Generate vioscreen top food report + description: Generate vioscreen top food report + parameters: + - $ref: '#/components/parameters/account_id' + - $ref: '#/components/parameters/source_id' + - $ref: '#/components/parameters/survey_id' + responses: + '200': + description: Top Food Report pdf + '/check_kit_valid': get: operationId: microsetta_interface.implementation.get_ajax_check_kit_valid @@ -629,6 +668,13 @@ components: schema: $ref: '#/components/schemas/sample_id' required: true + survey_id: + name: survey_id + in: path + description: Unique id specifying a sample associated with a source + schema: + $ref: '#/components/schemas/survey_id' + required: true survey_template_id: name: survey_template_id in: query @@ -671,6 +717,10 @@ components: type: string readOnly: true # sent in GET, not in POST/PUT/PATCH example: "dae21127-27bb-4f52-9fd3-a2aa5eb5b86f" + survey_id: + type: string + readOnly: true # sent in GET, not in POST/PUT/PATCH + example: "1234567890abcdef" survey_template_id: type: integer example: 3 diff --git a/microsetta_interface/templates/embedded_pdf.jinja2 b/microsetta_interface/templates/embedded_pdf.jinja2 new file mode 100644 index 00000000..e5861ea8 --- /dev/null +++ b/microsetta_interface/templates/embedded_pdf.jinja2 @@ -0,0 +1,24 @@ +{% extends "sitebase.jinja2" %} +{% set page_title = page_title %} + +{% block head %} + + +{% endblock %} +{% block content %} + + +{% endblock %} \ No newline at end of file diff --git a/microsetta_interface/templates/source.jinja2 b/microsetta_interface/templates/source.jinja2 index 16ab3b0a..ce065ddb 100644 --- a/microsetta_interface/templates/source.jinja2 +++ b/microsetta_interface/templates/source.jinja2 @@ -179,9 +179,9 @@
{% if sample.ffq %} - Food Frequency Questionnaire + View Top Food Report {% else %} - Food Frequency Questionnaire + Food Frequency Questionnaire {% endif %}
{% endif %} From 475551e28402d82d5972a39b91359a79b8d4d1d2 Mon Sep 17 00:00:00 2001 From: dhakim87 Date: Tue, 20 Oct 2020 13:51:01 -0700 Subject: [PATCH 2/3] Alert box explains missing fields --- microsetta_interface/templates/embedded_pdf.jinja2 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/microsetta_interface/templates/embedded_pdf.jinja2 b/microsetta_interface/templates/embedded_pdf.jinja2 index e5861ea8..b27e319a 100644 --- a/microsetta_interface/templates/embedded_pdf.jinja2 +++ b/microsetta_interface/templates/embedded_pdf.jinja2 @@ -20,5 +20,8 @@ html, body, embed { {% endblock %} {% block content %} + {% endblock %} \ No newline at end of file From 8941dc0e8c5564336311cb20267d7b729790aa2d Mon Sep 17 00:00:00 2001 From: dhakim87 Date: Thu, 29 Oct 2020 11:21:55 -0700 Subject: [PATCH 3/3] Added 404s to yaml for retrieving ffq and changed text above ffq pdf --- microsetta_interface/routes.yaml | 6 ++++++ microsetta_interface/templates/embedded_pdf.jinja2 | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/microsetta_interface/routes.yaml b/microsetta_interface/routes.yaml index 65536923..6f5d93d9 100644 --- a/microsetta_interface/routes.yaml +++ b/microsetta_interface/routes.yaml @@ -516,6 +516,9 @@ paths: type: string '302': description: Redirecting to necessary action + '404': + description: No such pdf + '/accounts/{account_id}/sources/{source_id}/surveys/{survey_id}/reports/topfoodreport/pdf': # Attempts to generate the vioscreen top food report pdf from this vioscreen survey @@ -532,6 +535,9 @@ paths: responses: '200': description: Top Food Report pdf + '404': + description: No such pdf + '/check_kit_valid': get: diff --git a/microsetta_interface/templates/embedded_pdf.jinja2 b/microsetta_interface/templates/embedded_pdf.jinja2 index b27e319a..03d2002a 100644 --- a/microsetta_interface/templates/embedded_pdf.jinja2 +++ b/microsetta_interface/templates/embedded_pdf.jinja2 @@ -21,7 +21,7 @@ html, body, embed { {% block content %} {% endblock %} \ No newline at end of file