Skip to content

Commit c7d57c3

Browse files
author
“Carmel
committed
Start set up of buttons 2, 3, change json to yaml config
1 parent 6145b96 commit c7d57c3

11 files changed

Lines changed: 263 additions & 48 deletions

__pycache__/app.cpython-311.pyc

7.95 KB
Binary file not shown.

app.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
from pathlib import Path
2+
13
from flask import Flask, json, render_template, request, jsonify
24
import requests
5+
import yaml
36

47
from new_ldot_workflows.b_1_get_new_subjects import get_new_subjects
58
from new_ldot_workflows.b_2_get_qualtrics_links import add_individuals_to_survey
69
from new_ldot_workflows.b_2_send_links_to_ldot import send_links_to_ldot
10+
from new_ldot_workflows.b_3_get_incomplete_subjects import get_incomplete_subjects
711
from new_ldot_workflows.b_4_get_individual_progress import get_individual_progress
812

913
app = Flask(__name__)
1014

11-
STUDIES = json.load(open("ldot_study_configs.json"))
15+
CONFIG_PATH = Path(__file__).resolve().with_name("study_configs.yaml")
16+
STUDIES = yaml.safe_load(CONFIG_PATH.read_text(encoding="utf-8"))
17+
18+
def get_study_settings(study_key: str):
19+
study = STUDIES.get(study_key)
20+
if not study:
21+
return None, None, None
22+
23+
return study, study.get("ldot_variables", {}), study.get("qualtrics_variables", {})
1224

1325
@app.route("/")
1426
def index():
@@ -17,12 +29,19 @@ def index():
1729

1830
@app.route("/api/button1", methods=["POST"])
1931
def button1():
20-
"""First API call"""
32+
"""Get the participants who have not yet been added to Qualtrics"""
2133
data = request.json
2234
study_id = data.get("study_id")
35+
print(f"Received study_id: {study_id}")
2336

24-
# TODO: Replace this with your actual API call that returns subject IDs
25-
subject_ids = get_new_subjects(study_id)
37+
study, ldot_vars, qualtrics_vars = get_study_settings(study_id)
38+
if not study:
39+
return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400
40+
41+
ldot_study_id = ldot_vars.get("ldot_study_id")
42+
link_creation_eaid = ldot_vars.get("link_creation_eaid") or ldot_vars.get("survey_progress_wait_for_entry_eaid")
43+
44+
subject_ids = get_new_subjects(ldot_study_id, link_creation_eaid)
2645

2746
message = f"Found {len(subject_ids)} subject IDs for study {study_id}" if study_id else f"Found {len(subject_ids)} subject IDs"
2847
return jsonify({"success": True, "message": message, "subject_ids": subject_ids})
@@ -43,18 +62,24 @@ def button2():
4362
return jsonify({"success": False, "message": "Missing subject_ids in request"}), 400
4463

4564
# Lookup study variables from STUDIES config
46-
study = STUDIES.get(study_id)
65+
study, ldot_vars, qualtrics_vars = get_study_settings(study_id)
4766
if not study:
4867
return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400
4968

50-
vars = study.get("variables", {})
51-
survey_id = vars.get("survey_id")
52-
mailing_list_id = vars.get("mailing_list_id")
53-
embedded_data_field = vars.get("embedded_data_field")
54-
directory_id = vars.get("directory_id")
55-
distribution_id = vars.get("distribution_id")
69+
ldot_study_id = ldot_vars.get("ldot_study_id")
70+
survey_id = qualtrics_vars.get("survey_id")
71+
mailing_list_id = qualtrics_vars.get("mailing_list_id")
72+
embedded_data_field = qualtrics_vars.get("embedded_data_field")
73+
directory_id = qualtrics_vars.get("directory_id")
74+
distribution_id = qualtrics_vars.get("distribution_id")
75+
link_creation_eaid = ldot_vars.get("link_creation_eaid") or ldot_vars.get("survey_progress_wait_for_entry_eaid")
76+
77+
if not ldot_study_id:
78+
return jsonify({"success": False, "message": f"Missing ldot_study_id for study_id: {study_id}"}), 400
79+
5680
debug_inputs = {
5781
"study_id": study_id,
82+
"ldot_study_id": ldot_study_id,
5883
"subject_ids": subject_ids,
5984
"survey_id": survey_id,
6085
"mailing_list_id": mailing_list_id,
@@ -64,29 +89,38 @@ def button2():
6489
}
6590

6691
participant_to_link_dict = add_individuals_to_survey(subject_ids, embedded_data_field, distribution_id, survey_id, mailing_list_id, directory_id)
67-
send_links_to_ldot(participant_to_link_dict)
68-
92+
send_links_to_ldot(ldot_study_id, link_creation_eaid)
6993

70-
# return jsonify({
71-
# "success": True,
72-
# "message": "Button 2 inputs resolved successfully",
73-
# "debug_inputs": debug_inputs,
74-
# "participant_to_link_dict": participant_to_link_dict
75-
# })
94+
return jsonify({
95+
"success": True,
96+
"message": f"Processed {len(subject_ids)} subject IDs for study {study_id}",
97+
"debug_inputs": debug_inputs,
98+
"participant_to_link_dict": participant_to_link_dict,
99+
})
76100

77101

78102
@app.route("/api/button3", methods=["POST"])
79103
def button3():
80104
"""Third API call - returns list of subjectIDs"""
81105
data = request.json
82106
study_id = data.get("study_id")
107+
study, ldot_vars, qualtrics_vars = get_study_settings(study_id)
108+
109+
if not study:
110+
return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400
83111

84-
# TODO: Add your API call logic here - should return a list of subjectIDs
112+
ldot_study_id = ldot_vars.get("ldot_study_id")
113+
survey_progress_wait_for_entry_eaid = ldot_vars.get("survey_progress_wait_for_entry_eaid")
114+
survey_progress_completed_eaid = ldot_vars.get("survey_progress_completed_eaid")
115+
116+
if not ldot_study_id:
117+
return jsonify({"success": False, "message": f"Missing ldot_study_id for study_id: {study_id}"}), 400
118+
85119
try:
86120
result = f"Button 3 executed for study {study_id}"
87121
# Return both message and subject_ids list
88-
subject_ids = [] # TODO: Fill this with actual subjectIDs from your API call
89-
return jsonify({"success": True, "message": result, "subject_ids": subject_ids})
122+
incomplete_subjects = get_incomplete_subjects(ldot_study_id, survey_progress_wait_for_entry_eaid, survey_progress_completed_eaid)
123+
return jsonify({"success": True, "message": result, "subject_ids": incomplete_subjects})
90124
except Exception as e:
91125
return jsonify({"success": False, "message": str(e)})
92126

@@ -104,11 +138,10 @@ def button4():
104138
if not subject_ids:
105139
return jsonify({"success": False, "message": "Missing subject_ids in request"}), 400
106140

107-
study = STUDIES.get(study_id)
141+
study, vars = get_study_settings(study_id)
108142
if not study:
109143
return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400
110144

111-
vars = study.get("variables", {})
112145
survey_id = vars.get("survey_id")
113146
embedded_data_field = vars.get("embedded_data_field")
114147

ldot_study_configs.json

Lines changed: 0 additions & 20 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
def send_links_to_ldot(participant_to_link_dict):
2-
# {'11111TEST11111': 'https://survey.uu.nl/jfe/form/SV_efCMOg6wHU0T8ii?Q_CHL=gl&Q_DL=EMD_7AEa416lRwFhrkF_efCMOg6wHU0T8ii_CGC_CJK3jAlcohFgRMS&_g_=g', '22222TEST22222': 'https://survey.uu.nl/jfe/form/SV_efCMOg6wHU0T8ii?Q_CHL=gl&Q_DL=EMD_7AEa416lRwFhrkF_efCMOg6wHU0T8ii_CGC_M6crsA37s6sSOeE&_g_=g'}
3-
send_links_to_ldot
41

2+
# {'11111TEST11111': 'https://survey.uu.nl/jfe/form/SV_efCMOg6wHU0T8ii?Q_CHL=gl&Q_DL=EMD_7AEa416lRwFhrkF_efCMOg6wHU0T8ii_CGC_CJK3jAlcohFgRMS&_g_=g', '22222TEST22222': 'https://survey.uu.nl/jfe/form/SV_efCMOg6wHU0T8ii?Q_CHL=gl&Q_DL=EMD_7AEa416lRwFhrkF_efCMOg6wHU0T8ii_CGC_M6crsA37s6sSOeE&_g_=g'}
3+
4+
import json
5+
import requests
6+
7+
with open("new_ldot_workflows/ldot_config.json") as f:
8+
config = json.load(f)
9+
CLIENT_ID = config["client_id"]
10+
CLIENT_SECRET = config["client_secret"]
11+
LDOT_API_URL = config["LDOT_API_URL"]
12+
13+
14+
#### I then need to flip it to the next status
15+
16+
def send_links_to_ldot(study_id: str, link_creation_eaid: str) -> list:
17+
"""Get subjects that have not yet been added to Qualtrics by checking their event actions"""
18+
19+
response = requests.post(
20+
"https://accware.memic.maastrichtuniversity.nl/ldot_identity_server/connect/token",
21+
data={
22+
"grant_type": "client_credentials",
23+
"client_id": CLIENT_ID,
24+
"client_secret": CLIENT_SECRET
25+
}
26+
)
27+
28+
token = response.json()["access_token"]
29+
30+
headers={"accept": "application/json",
31+
"Authorization": f"Bearer {token}"
32+
}
33+
34+
subject_alias = "a5335cfd-b96a-4c08-b860-700cc4867ec3"
35+
response = requests.post(
36+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Subject/VerifySubject",
37+
headers=headers
38+
)
39+
40+
response.raise_for_status()
41+
payload = response.json()
42+
print(payload)
43+
# study_event_actions = payload.get("Data", {}).get("StudyEventActions", [])
44+
# return [
45+
# action["SubjectGuid"]
46+
# for action in study_event_actions
47+
# if action.get("SubjectGuid")
48+
# ]
49+
50+
if __name__ == "__main__":
51+
print(send_links_to_ldot("5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b", "5d31129c-d814-5d4b-a96f-048cadc150ce"))
Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,78 @@
1-
# Get the subject IDs that have not yet received a survey link, so we can check on their progress.
1+
import json
2+
import requests
3+
4+
with open("new_ldot_workflows/ldot_config.json") as f:
5+
config = json.load(f)
6+
CLIENT_ID = config["client_id"]
7+
CLIENT_SECRET = config["client_secret"]
8+
LDOT_API_URL = config["LDOT_API_URL"]
9+
10+
11+
def get_incomplete_subjects(study_id: str, survey_progress_wait_for_entry_eaid: str = None, survey_progress_completed_eaid: str = None) -> list:
12+
"""Get subjects that have not yet been added to Qualtrics by checking their event actions"""
13+
14+
response = requests.post(
15+
"https://accware.memic.maastrichtuniversity.nl/ldot_identity_server/connect/token",
16+
data={
17+
"grant_type": "client_credentials",
18+
"client_id": CLIENT_ID,
19+
"client_secret": CLIENT_SECRET
20+
}
21+
)
22+
token = response.json()["access_token"]
23+
24+
headers={"accept": "application/json",
25+
"Authorization": f"Bearer {token}"
26+
}
27+
28+
29+
# FIrst lets get all EventAction GUIDs
30+
31+
actions = requests.get(
32+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action",
33+
headers=headers
34+
)
35+
36+
actions.raise_for_status()
37+
payload = actions.json()
38+
# print(payload)
39+
action_guids = []
40+
for action in payload.get("Data", {}).get("StudyEventActions", []):
41+
action_guids.append((action["EventActionGuid"], action["Description"]))
42+
43+
# for guid, description in action_guids:
44+
# people_with_this_action = requests.get(
45+
# f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{guid}",
46+
# headers=headers
47+
# )
48+
# people_with_this_action.raise_for_status()
49+
# if "Data" in people_with_this_action.json():
50+
# print (people_with_this_action.json()["Data"]["StudyEventActions"])
51+
52+
53+
# # Get SubjectIDs where survey invitation has been completed but survey progress has not been completed
54+
result = requests.get(
55+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{survey_progress_wait_for_entry_eaid}",
56+
headers=headers
57+
)
58+
result.raise_for_status()
59+
60+
subjects_with_survey_progress_wait_for_entry = set()
61+
for action in result.json().get("Data", {}).get("StudyEventActions", []):
62+
subjects_with_survey_progress_wait_for_entry.add(action["SubjectGuid"])
63+
64+
result = requests.get(
65+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Action/{survey_progress_completed_eaid}",
66+
headers=headers
67+
)
68+
result.raise_for_status()
69+
70+
subjecst_with_survey_progress_completed = set()
71+
for action in result.json().get("Data", {}).get("StudyEventActions", []):
72+
subjecst_with_survey_progress_completed.add(action["SubjectGuid"])
73+
74+
incomplete_subjects = subjects_with_survey_progress_wait_for_entry - subjecst_with_survey_progress_completed
75+
return list(incomplete_subjects)
76+
77+
if __name__ == "__main__":
78+
print(get_incomplete_subjects("5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b", "9f09f2cf-32cc-844d-9a41-7a44accd3dfd", '120d298d-9aa9-084b-abc6-432148db0e10'))

new_ldot_workflows/scratch.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
from urllib import response
3+
import requests
4+
5+
with open("new_ldot_workflows/ldot_config.json") as f:
6+
config = json.load(f)
7+
CLIENT_ID = config["client_id"]
8+
CLIENT_SECRET = config["client_secret"]
9+
LDOT_API_URL = config["LDOT_API_URL"]
10+
11+
def get_new_subjects(study_id: str, link_creation_eaid: str) -> list:
12+
"""Get subjects that have not yet been added to Qualtrics by checking their event actions"""
13+
14+
response = requests.post(
15+
"https://accware.memic.maastrichtuniversity.nl/ldot_identity_server/connect/token",
16+
data={
17+
"grant_type": "client_credentials",
18+
"client_id": CLIENT_ID,
19+
"client_secret": CLIENT_SECRET
20+
}
21+
)
22+
23+
token = response.json()["access_token"]
24+
25+
headers={"accept": "application/json",
26+
"Authorization": f"Bearer {token}"
27+
}
28+
29+
response = requests.post(
30+
f"https://accware.memic.maastrichtuniversity.nl/memic_ldot_api/api/v1.1/{study_id}/Subject/VerifySubject",
31+
json={"alias": "", "regID": "DEE5"},
32+
headers=headers
33+
)
34+
35+
response.raise_for_status()
36+
payload = response.json()
37+
38+
print(response.status_code)
39+
print(response.text)
40+
print(payload)
41+
42+
43+
if __name__ == "__main__":
44+
print(get_new_subjects("5c9c6a47-c8d7-8142-a8c8-ccdcb8a8044b", "5d31129c-d814-5d4b-a96f-048cadc150ce"))

requirements.txt

26 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)