Skip to content

Commit

Permalink
Merge pull request #49 from biocore/st_dh_vioscreen_surveys
Browse files Browse the repository at this point in the history
WIP Show vioscreen pdf to user
  • Loading branch information
wasade authored Nov 12, 2020
2 parents 314efca + 8941dc0 commit 2b19b04
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
57 changes: 49 additions & 8 deletions microsetta_interface/implementation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -1189,7 +1227,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
Expand All @@ -1203,20 +1241,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):
Expand Down
56 changes: 56 additions & 0 deletions microsetta_interface/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,51 @@ 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
'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
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
'404':
description: No such pdf


'/check_kit_valid':
get:
operationId: microsetta_interface.implementation.get_ajax_check_kit_valid
Expand Down Expand Up @@ -646,6 +691,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
Expand Down Expand Up @@ -688,6 +740,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
Expand Down
27 changes: 27 additions & 0 deletions microsetta_interface/templates/embedded_pdf.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "sitebase.jinja2" %}
{% set page_title = page_title %}

{% block head %}

<style>
html, body, embed {
margin: 0;
height: 100%;
}
.content {
margin: 0;
height: 100%;
}
#app {
margin: 0;
height: 100%;
}
</style>
{% endblock %}
{% block content %}
<!-- TODO: Needs an animated loading bar -->
<div class="alert alert-info" role="alert">
This report is generated by a third party without information regarding age, height or weight.
</div>
<embed class="m-3 w-100 h-100" src="{{link_to_pdf}}"/>
{% endblock %}
2 changes: 1 addition & 1 deletion microsetta_interface/templates/source.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
</div>
<div class="col-sm">
{% if sample.ffq %}
Food Frequency Questionnaire<span class="fa fa-check"/>
<a class="btn btn-outline-success" href="/accounts/{{account_id}}/sources/{{source_id}}/surveys/{{sample.ffq}}/reports/topfoodreport">View Top Food Report</a><span class="fa fa-check"/>
{% else %}
<a class="btn btn-outline-primary" href="/accounts/{{account_id}}/sources/{{source_id}}/samples/{{sample.sample_id}}/survey_templates/{{vioscreen_id}}">Food Frequency Questionnaire</a><span class="fa fa-unchecked"/>
{% endif %} <!-- end of if sample.ffq -->
Expand Down

0 comments on commit 2b19b04

Please sign in to comment.