|
| 1 | +from flask import Flask, render_template, request, jsonify |
| 2 | +import requests |
| 3 | + |
| 4 | +from new_ldot_workflows.b_2_get_qualtrics_links import add_individuals_to_survey |
| 5 | +from new_ldot_workflows.b_2_send_links_to_ldot import send_links_to_ldot |
| 6 | +from new_ldot_workflows.b_4_get_individual_progress import get_individual_progress |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | + |
| 10 | +# Study configurations - modify these as needed |
| 11 | +STUDIES = { |
| 12 | + "LDOT-001": {"name": "PIAMA study", "variables": {"survey_id": "SV_efCMOg6wHU0T8ii", |
| 13 | + "directory_id": "POOL_10pyxk9leSUisrT", |
| 14 | + "distribution_id": "EMD_7AEa416lRwFhrkF", |
| 15 | + "mailing_list_id": "CG_2dMbO6WUBMnCeIK", |
| 16 | + "embedded_data_field": "study_id_child"}}, |
| 17 | + "LDOT-002": {"name": "LDOT Study 002", "variables": {"survey_id": "value3", |
| 18 | + "directory_id": "POOL_10pyxk9leSUisrT", |
| 19 | + "distribution_id": "EMD_7AEa416lRwFhrkF", |
| 20 | + "mailing_list_id": "value4", |
| 21 | + "embedded_data_field": "study_id_child"}}, |
| 22 | + "LDOT-003": {"name": "LDOT Study 003", "variables": {"survey_id": "value5", |
| 23 | + "directory_id": "POOL_10pyxk9leSUisrT", |
| 24 | + "distribution_id": "EMD_7AEa416lRwFhrkF", |
| 25 | + "mailing_list_id": "value6", |
| 26 | + "embedded_data_field": "study_id_child"}}, |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +@app.route("/") |
| 31 | +def index(): |
| 32 | + return render_template("index.html", studies=STUDIES) |
| 33 | + |
| 34 | + |
| 35 | +@app.route("/api/button1", methods=["POST"]) |
| 36 | +def button1(): |
| 37 | + """First API call""" |
| 38 | + data = request.json |
| 39 | + study_id = data.get("study_id") |
| 40 | + |
| 41 | + # TODO: Replace this with your actual API call that returns subject IDs |
| 42 | + subject_ids = ["subject1", "subject2", "subject3"] |
| 43 | + |
| 44 | + message = f"Found {len(subject_ids)} subject IDs for study {study_id}" if study_id else f"Found {len(subject_ids)} subject IDs" |
| 45 | + return jsonify({"success": True, "message": message, "subject_ids": subject_ids}) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +@app.route("/api/button2", methods=["POST"]) |
| 50 | +def button2(): |
| 51 | + """Second API call - uses subjectIDs from button1""" |
| 52 | + data = request.json |
| 53 | + study_id = data.get("study_id") |
| 54 | + subject_ids = data.get("subject_ids") |
| 55 | + |
| 56 | + if not study_id: |
| 57 | + return jsonify({"success": False, "message": "Missing study_id in request"}), 400 |
| 58 | + |
| 59 | + if not subject_ids: |
| 60 | + return jsonify({"success": False, "message": "Missing subject_ids in request"}), 400 |
| 61 | + |
| 62 | + # Lookup study variables from STUDIES config |
| 63 | + study = STUDIES.get(study_id) |
| 64 | + if not study: |
| 65 | + return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400 |
| 66 | + |
| 67 | + vars = study.get("variables", {}) |
| 68 | + survey_id = vars.get("survey_id") |
| 69 | + mailing_list_id = vars.get("mailing_list_id") |
| 70 | + embedded_data_field = vars.get("embedded_data_field") |
| 71 | + directory_id = vars.get("directory_id") |
| 72 | + distribution_id = vars.get("distribution_id") |
| 73 | + debug_inputs = { |
| 74 | + "study_id": study_id, |
| 75 | + "subject_ids": subject_ids, |
| 76 | + "survey_id": survey_id, |
| 77 | + "mailing_list_id": mailing_list_id, |
| 78 | + "embedded_data_field": embedded_data_field, |
| 79 | + "directory_id": directory_id, |
| 80 | + "distribution_id": distribution_id |
| 81 | + } |
| 82 | + |
| 83 | + participant_to_link_dict = add_individuals_to_survey(subject_ids, embedded_data_field, distribution_id, survey_id, mailing_list_id, directory_id) |
| 84 | + send_links_to_ldot(participant_to_link_dict) |
| 85 | + |
| 86 | + |
| 87 | + # return jsonify({ |
| 88 | + # "success": True, |
| 89 | + # "message": "Button 2 inputs resolved successfully", |
| 90 | + # "debug_inputs": debug_inputs, |
| 91 | + # "participant_to_link_dict": participant_to_link_dict |
| 92 | + # }) |
| 93 | + |
| 94 | + |
| 95 | +@app.route("/api/button3", methods=["POST"]) |
| 96 | +def button3(): |
| 97 | + """Third API call - returns list of subjectIDs""" |
| 98 | + data = request.json |
| 99 | + study_id = data.get("study_id") |
| 100 | + |
| 101 | + # TODO: Add your API call logic here - should return a list of subjectIDs |
| 102 | + try: |
| 103 | + result = f"Button 3 executed for study {study_id}" |
| 104 | + # Return both message and subject_ids list |
| 105 | + subject_ids = [] # TODO: Fill this with actual subjectIDs from your API call |
| 106 | + return jsonify({"success": True, "message": result, "subject_ids": subject_ids}) |
| 107 | + except Exception as e: |
| 108 | + return jsonify({"success": False, "message": str(e)}) |
| 109 | + |
| 110 | + |
| 111 | +@app.route("/api/button4", methods=["POST"]) |
| 112 | +def button4(): |
| 113 | + """Fourth API call - uses subjectIDs from button3""" |
| 114 | + data = request.json |
| 115 | + study_id = data.get("study_id") |
| 116 | + subject_ids = data.get("subject_ids") # List of subjectIDs from button3 |
| 117 | + |
| 118 | + if not study_id: |
| 119 | + return jsonify({"success": False, "message": "Missing study_id in request"}), 400 |
| 120 | + |
| 121 | + if not subject_ids: |
| 122 | + return jsonify({"success": False, "message": "Missing subject_ids in request"}), 400 |
| 123 | + |
| 124 | + study = STUDIES.get(study_id) |
| 125 | + if not study: |
| 126 | + return jsonify({"success": False, "message": f"Unknown study_id: {study_id}"}), 400 |
| 127 | + |
| 128 | + vars = study.get("variables", {}) |
| 129 | + survey_id = vars.get("survey_id") |
| 130 | + embedded_data_field = vars.get("embedded_data_field") |
| 131 | + |
| 132 | + participant_to_progress_dict = get_individual_progress(subject_ids, embedded_data_field, survey_id) |
| 133 | + |
| 134 | + |
| 135 | + return jsonify({ |
| 136 | + "success": True, |
| 137 | + "message": f"Retrieved progress for {len(participant_to_progress_dict)} subjects", |
| 138 | + "study_id": study_id, |
| 139 | + "survey_id": survey_id, |
| 140 | + "embedded_data_field": embedded_data_field, |
| 141 | + "progress_results": participant_to_progress_dict, |
| 142 | + }) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + app.run(debug=True) |
0 commit comments