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

Allow Minors to Change age_range on Source #306

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
152 changes: 112 additions & 40 deletions microsetta_interface/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Source:
ACCT_WRITEABLE_KEYS = [ACCT_FNAME_KEY, ACCT_LNAME_KEY, ACCT_EMAIL_KEY,
ACCT_ADDR_KEY, ACCT_LANG_KEY, ACCT_TERMS_KEY]

# Age groups for consent purposes, in order. Order is key as this will be
# used to govern progression. E.g., a child may move to teen, but a teen may
# not move to child.
HUMAN_CONSENT_AGE_GROUPS = ["0-6", "7-12", "13-17", "18-plus"]
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

# States
NEEDS_REROUTE = "NeedsReroute"
NEEDS_LOGIN = "NeedsLogin"
Expand Down Expand Up @@ -1213,7 +1218,9 @@ def get_consent_page(*, account_id=None):
participant_name=None,
age_range=None,
source_id=None,
reconsent=reconsent
reconsent=reconsent,
cur_age_index=-1,
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
update_age=False
)


Expand Down Expand Up @@ -1621,9 +1628,12 @@ def top_food_report_pdf(*,


def render_consent_page(account_id, source_id, form_type, sample_ids=None,
reconsent=None):
reconsent=None, update_age=False):
endpoint = SERVER_CONFIG["endpoint"]

if SOURCE_ID not in session:
session[SOURCE_ID] = source_id

relative_post_url = _make_acct_path(account_id,
suffix="create_human_source")

Expand Down Expand Up @@ -1654,38 +1664,32 @@ def render_consent_page(account_id, source_id, form_type, sample_ids=None,
# biospecimen consent
skip_dupe_check = True

# NB: For the time being, we need to block any existing under-18 profiles
# from reconsenting. Checking whether they have a current data consent is
# an absolute way to establish old profiles vs. new, so we'll see if they
# have the current data consent, then divert anyone under 18 away from the
# reconsent form.
need_reconsent_data = check_current_consent(
account_id, source_id, "data"
)
if need_reconsent_data and source_output['consent']['age_range'] !=\
"legacy" and source_output['consent']['age_range'] != "18-plus":
return _render_with_defaults(
'minor_reconsent.jinja2',
account_id=account_id,
source_id=source_id
)
else:
return _render_with_defaults(
'new_participant.jinja2',
tl=consent_output,
post_url=post_url,
duplicate_source_check=duplicate_source_check,
home_url=home_url,
form_type=form_type,
reconsent=reconsent,
language_tag=session_locale(),
sample_ids=sample_ids,
participant_name=source_output['source_name'],
age_range=source_output['consent']['age_range'],
account_id=account_id,
source_id=source_id,
skip_dupe_check=skip_dupe_check
try:
cur_age_index = HUMAN_CONSENT_AGE_GROUPS.index(
source_output['consent']['age_range']
)
except ValueError:
# In case a source has a blank, legacy or faulty age range
cur_age_index = -1

return _render_with_defaults(
'new_participant.jinja2',
tl=consent_output,
post_url=post_url,
duplicate_source_check=duplicate_source_check,
home_url=home_url,
form_type=form_type,
reconsent=reconsent,
language_tag=session_locale(),
sample_ids=sample_ids,
participant_name=source_output['source_name'],
age_range=source_output['consent']['age_range'],
account_id=account_id,
source_id=source_id,
skip_dupe_check=skip_dupe_check,
cur_age_index=cur_age_index,
update_age=update_age
)


def decline_reconsent(*, account_id, source_id):
Expand All @@ -1699,6 +1703,30 @@ def decline_reconsent(*, account_id, source_id):
)


def get_update_age(*, account_id, source_id):
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
# Retrieve the source
has_error, source_output, _ = ApiRequest.get(
'/accounts/%s/sources/%s' %
(account_id, source_id))
if has_error:
return source_output

# Adults can't change their age range, redirect them to My Profile
if source_output['consent']['age_range'] == "18-plus":
return redirect(
"/accounts/%s/sources/%s" %
(account_id, source_id)
)

return render_consent_page(
account_id,
source_id,
"data",
reconsent=True,
update_age=True
)


@prerequisite([SOURCE_PREREQS_MET, BIOSPECIMEN_PREREQS_MET])
def get_source(*, account_id=None, source_id=None):
# If the user is switching profiles, we need to remove the
Expand Down Expand Up @@ -1732,6 +1760,10 @@ def get_source(*, account_id=None, source_id=None):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

# Hack to determine if user's country is Spain OR locale is es_ES
# If either condition is true, show the Spain FFQ
spain_user = False
Expand Down Expand Up @@ -1839,7 +1871,8 @@ def get_source(*, account_id=None, source_id=None):
remote_surveys=remote_surveys,
source_name=source_output['source_name'],
profile_has_samples=profile_has_samples,
need_reconsent=need_reconsent
need_reconsent=need_reconsent,
show_update_age=show_update_age
)


Expand All @@ -1859,6 +1892,10 @@ def get_kits(*, account_id=None, source_id=None, check_survey_date=False):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

# Retrieve all samples from the source
has_error, samples_output, _ = ApiRequest.get(
'/accounts/%s/sources/%s/samples' % (account_id, source_id))
Expand Down Expand Up @@ -1945,7 +1982,8 @@ def get_kits(*, account_id=None, source_id=None, check_survey_date=False):
prompt_survey_update=prompt_survey_update,
prompt_survey_id=BASIC_INFO_ID,
need_reconsent_data=need_reconsent_data,
need_reconsent_biospecimen=need_reconsent_biospecimen
need_reconsent_biospecimen=need_reconsent_biospecimen,
show_update_age=show_update_age
)


Expand All @@ -1965,6 +2003,10 @@ def get_nutrition(*, account_id=None, source_id=None, new_ffq_code=None):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

# Check the FFQ prereqs for the source (birth year, gender, height, weight
has_error, prereqs_output, _ = ApiRequest.get(
'/accounts/%s/sources/%s/check_ffq_prereqs' %
Expand Down Expand Up @@ -1999,7 +2041,8 @@ def get_nutrition(*, account_id=None, source_id=None, new_ffq_code=None):
new_ffq_code=new_ffq_code,
profile_has_samples=profile_has_samples,
has_basic_info=has_basic_info,
need_reconsent_data=need_reconsent_data
need_reconsent_data=need_reconsent_data,
show_update_age=show_update_age
)


Expand All @@ -2012,6 +2055,10 @@ def get_reports(*, account_id=None, source_id=None):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

# Retrieve all Vioscreen registry entries for the source
has_error, vioscreen_output, _ = ApiRequest.get(
'/accounts/%s/sources/%s/vioscreen_registry_entries' % (
Expand Down Expand Up @@ -2069,7 +2116,8 @@ def get_reports(*, account_id=None, source_id=None):
public_endpoint=SERVER_CONFIG['public_api_endpoint'],
profile_has_samples=profile_has_samples,
external_reports_kit=external_reports_kit,
external_reports_ffq=external_reports_ffq
external_reports_ffq=external_reports_ffq,
show_update_age=show_update_age
)


Expand Down Expand Up @@ -2114,6 +2162,10 @@ def get_consents(*, account_id=None, source_id=None):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

# Retrieve the latest signed consents
has_error, data_consent, _ = ApiRequest.get(
'/accounts/%s/sources/%s/signed_consent/%s' %
Expand Down Expand Up @@ -2144,7 +2196,8 @@ def get_consents(*, account_id=None, source_id=None):
source_name=source_output['source_name'],
data_consent=data_consent,
biospecimen_consent=biospecimen_consent,
profile_has_samples=profile_has_samples
profile_has_samples=profile_has_samples,
show_update_age=show_update_age
)


Expand Down Expand Up @@ -2182,6 +2235,16 @@ def get_consent_view(*, account_id=None, source_id=None, consent_type=None):
False
)

consent_type_parts = consent_output['consent_type'].split("_")
if consent_type_parts[0] == "adult":
age_range = "18-plus"
elif consent_type_parts[0] == "adolescent":
age_range = "13-17"
elif consent_type_parts[0] == "child":
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved
age_range = "7-12"
else:
age_range = "0-6"
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved

if consent_type == "biospecimen":
consent_type_display = "Biospecimen"
else:
Expand All @@ -2191,7 +2254,7 @@ def get_consent_view(*, account_id=None, source_id=None, consent_type=None):
'signed_consent.jinja2',
account_id=account_id,
source_id=source_id,
source_age=source_output['consent']['age_range'],
source_age=age_range,
source_name=source_output['source_name'],
consent=consent_output,
tl=consent_assets,
Expand Down Expand Up @@ -2241,6 +2304,10 @@ def get_update_sample(*, account_id=None, source_id=None, sample_id=None):
if has_error:
return source_output

show_update_age = check_show_update_age(
source_output['consent']['age_range']
)

has_error, sample_output, _ = ApiRequest.get(
'/accounts/%s/sources/%s/samples/%s' %
(account_id, source_id, sample_id))
Expand Down Expand Up @@ -2320,7 +2387,8 @@ def get_update_sample(*, account_id=None, source_id=None, sample_id=None):
is_environmental=is_environmental,
profile_has_samples=profile_has_samples,
need_reconsent_data=need_reconsent_data,
need_reconsent_biospecimen=need_reconsent_biospecimen
need_reconsent_biospecimen=need_reconsent_biospecimen,
show_update_age=show_update_age
)


Expand Down Expand Up @@ -3581,6 +3649,10 @@ def check_current_consent(account_id, source_id, consent_type):
return consent_required["result"]


def check_show_update_age(age_range):
return age_range != "18-plus"
cassidysymons marked this conversation as resolved.
Show resolved Hide resolved


class BearerAuth(AuthBase):
def __init__(self, token):
self.token = token
Expand Down
18 changes: 18 additions & 0 deletions microsetta_interface/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,24 @@ paths:
schema:
type: string

'/accounts/{account_id}/sources/{source_id}/update_age_range':
get:
operationId: microsetta_interface.implementation.get_update_age
tags:
- Source
parameters:
- $ref: '#/components/parameters/account_id'
- $ref: '#/components/parameters/source_id'
responses:
'200':
description: Display the age update and consent interface
content:
text/html:
schema:
type: string
'302':
description: Redirect if the user is 18+

# Note: ideally this would be represented as a DELETE, not as a GET
# However, it is used as a link, which does not support an HTTP action of
# delete
Expand Down
Loading
Loading