Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul #502

Merged
merged 72 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
46fab7f
Initial commit for testing
cassidysymons Jan 12, 2023
3b2319f
Adjustment for testing
cassidysymons Jan 12, 2023
5daccbc
Survey & consent adjustments
cassidysymons Jan 17, 2023
3e4df72
Initial kit testing
cassidysymons Jan 19, 2023
62f4231
Debug
cassidysymons Jan 20, 2023
91183bf
Survey updates
cassidysymons Jan 20, 2023
dc6dd0e
Survey adjustments
cassidysymons Jan 23, 2023
bbf8131
Survey adjustments
cassidysymons Jan 24, 2023
d8222fc
Adjustments
cassidysymons Jan 25, 2023
704d84b
Adjustments
cassidysymons Jan 25, 2023
984a691
Adjustments
cassidysymons Jan 25, 2023
e48fc14
Adjustments
cassidysymons Jan 26, 2023
6f054c3
Adjustments
cassidysymons Jan 30, 2023
9c706f0
Adjustments
cassidysymons Feb 1, 2023
f45d8f5
Adjustments
cassidysymons Feb 1, 2023
6aff808
Adjustments
cassidysymons Feb 1, 2023
2bd9d50
Adjustments
cassidysymons Feb 1, 2023
4326d16
Adjustments
cassidysymons Feb 1, 2023
1d5d520
Adjustments
cassidysymons Feb 1, 2023
90b5fb9
Adjustments
cassidysymons Feb 1, 2023
501a694
Adjustments
cassidysymons Feb 2, 2023
5440e7a
Adjustments
cassidysymons Feb 3, 2023
da4dc7b
Merge branch 'master-overhaul' into csymons_overhaul_api
cassidysymons Feb 7, 2023
caae5df
Update microsetta_private_api/api/microsetta_private_api.yaml
cassidysymons Feb 14, 2023
3e12273
Update microsetta_private_api/db/patches/0109.sql
cassidysymons Feb 14, 2023
66d2b78
Update microsetta_private_api/repo/consent_repo.py
cassidysymons Feb 14, 2023
c0ca17b
Update microsetta_private_api/repo/sample_repo.py
cassidysymons Feb 14, 2023
fa12f3a
Code review changes
cassidysymons Feb 14, 2023
41a2be4
Code review changes
cassidysymons Feb 14, 2023
f83f16c
Code review changes
cassidysymons Feb 15, 2023
fdc267d
Test adjustments
cassidysymons Feb 15, 2023
2b0490e
Test adjustments
cassidysymons Feb 15, 2023
8e53a85
Test adjustments
cassidysymons Feb 15, 2023
2786344
Test adjustments
cassidysymons Feb 15, 2023
b5305b5
Test adjustments
cassidysymons Feb 15, 2023
3bb97bd
Test adjustments
cassidysymons Feb 15, 2023
7071a33
Test adjustments
cassidysymons Feb 15, 2023
5ceadc6
Test adjustments
cassidysymons Feb 15, 2023
e2e340f
Test adjustments
cassidysymons Feb 15, 2023
0086554
Test adjustments
cassidysymons Feb 15, 2023
a38eb97
Test adjustments
cassidysymons Feb 15, 2023
3b4ebd6
Test adjustments
cassidysymons Feb 15, 2023
b1ed548
Test adjustments
cassidysymons Feb 15, 2023
fa1174c
Test adjustments
cassidysymons Feb 15, 2023
2731c07
Test adjustments
cassidysymons Feb 15, 2023
fb0c58b
Test adjustments
cassidysymons Feb 15, 2023
30e610a
Test adjustments
cassidysymons Feb 15, 2023
901799d
Test adjustments
cassidysymons Feb 15, 2023
7eda22d
Test adjustments
cassidysymons Feb 15, 2023
ba1592c
Test adjustments
cassidysymons Feb 15, 2023
65e7d5b
Test adjustments
cassidysymons Feb 15, 2023
5a13935
Test adjustments
cassidysymons Feb 15, 2023
6709e37
Test adjustments
cassidysymons Feb 15, 2023
13fd456
Test adjustments
cassidysymons Feb 15, 2023
088bd48
Test adjustments
cassidysymons Feb 15, 2023
32a7c82
Test adjustments
cassidysymons Feb 15, 2023
04ec724
Test adjustments
cassidysymons Feb 15, 2023
9c02304
Test adjustments
cassidysymons Feb 15, 2023
80c0140
Test adjustments
cassidysymons Feb 15, 2023
1b1df99
Gender question adjustment
cassidysymons Feb 17, 2023
20befd7
Correct format for height and weight
cassidysymons Feb 21, 2023
84c55d3
Test fix
cassidysymons Feb 21, 2023
6fb00d0
Source scrub/delete adjustments
cassidysymons Feb 22, 2023
2ea05b8
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
b4bcaf4
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
835cfed
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
7afe8d8
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
dce550f
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
9042f45
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
a7f5926
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
684ef82
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
7fa3689
Source scrub/delete adjustments - fixes
cassidysymons Feb 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions microsetta_private_api/admin/admin_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,20 @@ def generate_activation_codes(body, token_info):
return jsonify(results), 200


def generate_ffq_codes(body, token_info):
validate_admin_access(token_info)

quantity = body.get("code_quantity", 1)
with Transaction() as t:
admin_repo = AdminRepo(t)
code_list = []
for i in range(quantity):
code_list.append(admin_repo.create_ffq_code())
t.commit()

return jsonify(code_list), 200


def list_barcode_query_fields(token_info):
validate_admin_access(token_info)

Expand Down
12 changes: 8 additions & 4 deletions microsetta_private_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from ._consent import (
render_consent_doc,
check_consent_signature,
sign_consent_doc
sign_consent_doc,
get_signed_consents
)
from ._source import (
create_source, read_source, update_source, delete_source,
Expand Down Expand Up @@ -45,7 +46,7 @@
get_vioscreen_dietary_scores_descriptions,
get_vioscreen_food_components_by_code,
get_vioscreen_food_components_descriptions,
get_vioscreen_sessions
get_vioscreen_sessions, get_vioscreen_registry_entries, check_ffq_code
)

from ._campaign import (
Expand Down Expand Up @@ -89,6 +90,7 @@
'create_human_source_from_consent',
'check_consent_signature',
'sign_consent_doc',
'get_signed_consents',
'read_survey_template',
'read_survey_templates',
'read_answered_survey',
Expand Down Expand Up @@ -124,7 +126,9 @@
'create_interested_user',
'get_interested_user_address_update',
'put_interested_user_address_update',
'get_vioscreen_sessions',
'get_vioscreen_registry_entries',
'check_ffq_code',
'get_opt_out',
'put_opt_out',
'get_vioscreen_sessions'
'put_opt_out'
]
3 changes: 2 additions & 1 deletion microsetta_private_api/api/_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def update_account(account_id, body, token_info):
body['address']['city'],
body['address']['state'],
body['address']['post_code'],
body['address']['country_code']
body['address']['country_code'],
body['address']['street2']
)
acc.language = body['language']

Expand Down
53 changes: 50 additions & 3 deletions microsetta_private_api/api/_consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def check_consent_signature(account_id, source_id, consent_type, token_info):


def sign_consent_doc(account_id, source_id, consent_type, body, token_info):

_validate_account_access(token_info, account_id)

with Transaction() as t:
Expand All @@ -62,12 +61,60 @@ def sign_consent_doc(account_id, source_id, consent_type, body, token_info):
try:
consent_repo.sign_consent(account_id, consent_sign)
t.commit()
except Exception:
return jsonify(code=404, message=CONSENT_DOC_NOT_FOUND_MSG), 404
except Exception as e:
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
raise Exception(e)
return jsonify(code=404, message=e), 404
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

response = jsonify({"result": True})
response.status_code = 201
response.headers['Location'] = '/api/accounts/%s' % \
(account_id)

return response


def get_signed_consents(account_id, source_id, token_info):
_validate_account_access(token_info, account_id)

ret_val = {}
with Transaction() as t:
consent_repo = ConsentRepo(t)
survey_consent = consent_repo.get_latest_signed_consent(
source_id,
"data"
)
if survey_consent is None:
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
ret_val['survey'] = None
else:
survey_consent = survey_consent.to_api()
survey_doc = consent_repo.get_consent_document(
survey_consent['consent_id']
)
survey_consent['consent_content'] = survey_doc.consent_content
if survey_consent['assent_id'] is not None:
assent_doc = consent_repo.get_consent_document(
survey_consent['assent_id']
)
survey_consent['assent_content'] = assent_doc.consent_content
ret_val['survey'] = survey_consent
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

sample_consent = consent_repo.get_latest_signed_consent(
source_id,
"biospecimen"
)
if sample_consent is None:
ret_val['sample'] = None
else:
sample_consent = sample_consent.to_api()
sample_doc = consent_repo.get_consent_document(
sample_consent['consent_id']
)
sample_consent['consent_content'] = sample_doc.consent_content
if sample_consent['assent_id'] is not None:
assent_doc = consent_repo.get_consent_document(
sample_consent['assent_id']
)
sample_consent['assent_content'] = assent_doc.consent_content
ret_val['sample'] = sample_consent

return jsonify(ret_val), 200
6 changes: 1 addition & 5 deletions microsetta_private_api/api/_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,12 @@ def create_human_source_from_consent(account_id, body, token_info):
}
}

deceased_parent_key = 'deceased_parent'
child_keys = {'parent_1_name', 'parent_2_name', deceased_parent_key,
'obtainer_name'}
child_keys = {'parent_1_name', 'obtainer_name'}

intersection = child_keys.intersection(body)
if intersection:
source['consent']['child_info'] = {}
for key in intersection:
if key == deceased_parent_key:
body[deceased_parent_key] = body[deceased_parent_key] == 'true'
source['consent']['child_info'][key] = body[key]

# NB: Don't expect to handle errors 404, 422 in this function; expect to
Expand Down
85 changes: 64 additions & 21 deletions microsetta_private_api/api/_survey.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flask
from flask import jsonify, make_response
from werkzeug.exceptions import NotFound
from flask_babel import format_datetime, force_locale

from microsetta_private_api.api._account import \
_validate_account_access
Expand Down Expand Up @@ -38,23 +39,22 @@ def read_survey_templates(account_id, source_id, language_tag, token_info):
template_repo = SurveyTemplateRepo(t)
if source.source_type == Source.SOURCE_TYPE_HUMAN:
return jsonify([template_repo.get_survey_template_link_info(x)
for x in [1, 3, 4, 5, 6, 7,
SurveyTemplateRepo.VIOSCREEN_ID,
SurveyTemplateRepo.MYFOODREPO_ID,
SurveyTemplateRepo.POLYPHENOL_FFQ_ID,
SurveyTemplateRepo.SPAIN_FFQ_ID,
SurveyTemplateRepo.BASIC_INFO_ID,
SurveyTemplateRepo.AT_HOME_ID,
SurveyTemplateRepo.LIFESTYLE_ID,
SurveyTemplateRepo.GUT_ID,
SurveyTemplateRepo.GENERAL_HEALTH_ID,
SurveyTemplateRepo.HEALTH_DIAG_ID,
SurveyTemplateRepo.ALLERGIES_ID,
SurveyTemplateRepo.DIET_ID,
SurveyTemplateRepo.DETAILED_DIET_ID,
SurveyTemplateRepo.MIGRAINE_ID,
SurveyTemplateRepo.SURFERS_ID,
SurveyTemplateRepo.COVID19_ID]]), 200
for x in [
SurveyTemplateRepo.VIOSCREEN_ID,
SurveyTemplateRepo.POLYPHENOL_FFQ_ID,
SurveyTemplateRepo.SPAIN_FFQ_ID,
SurveyTemplateRepo.BASIC_INFO_ID,
SurveyTemplateRepo.AT_HOME_ID,
SurveyTemplateRepo.LIFESTYLE_ID,
SurveyTemplateRepo.GUT_ID,
SurveyTemplateRepo.GENERAL_HEALTH_ID,
SurveyTemplateRepo.HEALTH_DIAG_ID,
SurveyTemplateRepo.ALLERGIES_ID,
SurveyTemplateRepo.DIET_ID,
SurveyTemplateRepo.DETAILED_DIET_ID,
SurveyTemplateRepo.COVID19_ID,
SurveyTemplateRepo.OTHER_ID
]]), 200
elif source.source_type == Source.SOURCE_TYPE_ANIMAL:
return jsonify([template_repo.get_survey_template_link_info(x)
for x in [2]]), 200
Expand Down Expand Up @@ -259,22 +259,65 @@ def read_survey_template(account_id, source_id, survey_template_id,
if field.id in client_side_validation:
field.set(**client_side_validation[field.id])

results, percent_comp = st_repo.migrate_responses(source_id,
survey_template_id)
date_last_taken = st_repo.get_date_survey_last_taken(
source_id,
survey_template_id
)
if date_last_taken is not None:
with force_locale(language_tag):
date_last_taken = format_datetime(
date_last_taken, "MMM d, yyyy"
)
info.date_last_taken = date_last_taken

results = st_repo.migrate_responses(source_id, survey_template_id)
if results:
# modify info with previous results before returning to client.
for group in info.survey_template_text.groups:
for field in group.fields:
previous_response = results[field.inputName]
if previous_response:
field.default = previous_response
info.percentage_completed = "{0:.2f}%".format(percent_comp * 100)
info.percentage_completed = _calculate_completion_percentage(
info.survey_template_text
)
else:
info.percentage_completed = '0.00%'
info.percentage_completed = 0
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

return jsonify(info), 200


def _calculate_completion_percentage(survey_model):
visible_questions = 0
answered_questions = 0
answers = {}

for group in survey_model.groups:
for field in group.fields:
answers[field.id] = field.default
if hasattr(field, 'triggered_by'):
q_is_visible = False
for trigger_q in field.triggered_by:
if answers[trigger_q['q_id']] == trigger_q['response']:
q_is_visible = True
break

if q_is_visible is True:
visible_questions += 1
if _is_question_answered(field.default):
answered_questions += 1
else:
visible_questions += 1
if _is_question_answered(field.default):
answered_questions += 1

return answered_questions / visible_questions
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved


def _is_question_answered(response):
return (response is not None and response != '' and response != [])


def read_answered_surveys(account_id, source_id, language_tag, token_info):
_validate_account_access(token_info, account_id)

Expand Down
27 changes: 26 additions & 1 deletion microsetta_private_api/api/_vioscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ def get_vioscreen_food_components_descriptions(token_info):
return jsonify(descriptions)


def get_vioscreen_sessions(account_id, source_id):
def get_vioscreen_sessions(account_id, source_id, token_info):
_validate_has_account(token_info)
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

"""Obtain vioscreen sessions if it exists"""
with Transaction() as t:
vio_session = VioscreenRepo(t)
Expand All @@ -299,3 +301,26 @@ def get_vioscreen_sessions(account_id, source_id):
return jsonify(code=404, message="No vioscreens found"), 404
else:
return jsonify({"vioscreens": vioscreen_session})


def check_ffq_code(ffq_code, token_info):
_validate_has_account(token_info)

with Transaction() as t:
vio_repo = VioscreenRepo(t)
unused_code = vio_repo.get_unused_code(ffq_code)
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
if unused_code is None:
return jsonify(code=404, message="No unused code found"), 404
return jsonify({"unused_code": True}), 200


def get_vioscreen_registry_entries(account_id, source_id, token_info):
_validate_has_account(token_info)
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

with Transaction() as t:
vio_repo = VioscreenRepo(t)
vio_registry_entries = vio_repo.get_registry_entries_by_source(
account_id,
source_id
)
return jsonify(vio_registry_entries), 200
Loading