@@ -53,49 +53,53 @@ def patient_list_update_patient(patient_id, research_study_id=None):
53
53
if not user .has_role (ROLE .PATIENT .value ):
54
54
return
55
55
56
- patient = PatientList .query .get (patient_id )
57
- new_record = False
58
- if not patient :
59
- new_record = True
60
- patient = PatientList (userid = patient_id )
61
- db .session .add (patient )
56
+ from ..timeout_lock import TimeoutLock
57
+ # async possibility, only allow one thread at a time
58
+ key = f"patient_list_update_patient:{ patient_id } "
59
+ with TimeoutLock (key , expires = 60 , timeout = 60 ):
60
+ patient = PatientList .query .get (patient_id )
61
+ new_record = False
62
+ if not patient :
63
+ new_record = True
64
+ patient = PatientList (userid = patient_id )
65
+ db .session .add (patient )
62
66
63
- if research_study_id is None or new_record :
64
- patient .study_id = user .external_study_id
65
- patient .firstname = user .first_name
66
- patient .lastname = user .last_name
67
- patient .email = user .email
68
- patient .birthdate = user .birthdate
69
- patient .deleted = user .deleted_id is not None
70
- patient .test_role = True if user .has_role (ROLE .TEST .value ) else False
71
- patient .org_id = user .organizations [0 ].id if user .organizations else None
72
- patient .org_name = user .organizations [0 ].name if user .organizations else None
67
+ if research_study_id is None or new_record :
68
+ patient .study_id = user .external_study_id
69
+ patient .firstname = user .first_name
70
+ patient .lastname = user .last_name
71
+ patient .email = user .email
72
+ patient .birthdate = user .birthdate
73
+ patient .deleted = user .deleted_id is not None
74
+ patient .test_role = True if user .has_role (ROLE .TEST .value ) else False
75
+ patient .org_id = user .organizations [0 ].id if user .organizations else None
76
+ patient .org_name = user .organizations [0 ].name if user .organizations else None
73
77
74
- # necessary to avoid recursive loop via some update paths
75
- now = datetime .utcnow ()
76
- if patient .last_updated and patient .last_updated + timedelta (seconds = 10 ) > now :
77
- db .session .commit ()
78
- return
78
+ # necessary to avoid recursive loop via some update paths
79
+ now = datetime .utcnow ()
80
+ if patient .last_updated and patient .last_updated + timedelta (seconds = 10 ) > now :
81
+ db .session .commit ()
82
+ return
79
83
80
- patient .last_updated = now
81
- if research_study_id == BASE_RS_ID or research_study_id is None :
82
- rs_id = BASE_RS_ID
83
- qb_status = qb_status_visit_name (
84
- patient .userid , research_study_id = rs_id , as_of_date = now )
85
- patient .questionnaire_status = str (qb_status ['status' ])
86
- patient .visit = qb_status ['visit_name' ]
87
- patient .consentdate , _ = consent_withdrawal_dates (user = user , research_study_id = rs_id )
84
+ patient .last_updated = now
85
+ if research_study_id == BASE_RS_ID or research_study_id is None :
86
+ rs_id = BASE_RS_ID
87
+ qb_status = qb_status_visit_name (
88
+ patient .userid , research_study_id = rs_id , as_of_date = now )
89
+ patient .questionnaire_status = str (qb_status ['status' ])
90
+ patient .visit = qb_status ['visit_name' ]
91
+ patient .consentdate , _ = consent_withdrawal_dates (user = user , research_study_id = rs_id )
88
92
89
- if (research_study_id == EMPRO_RS_ID or research_study_id is None ) and user .clinicians :
90
- rs_id = EMPRO_RS_ID
91
- patient .clinician = '; ' .join (
92
- (clinician_name_map ().get (c .id , "not in map" ) for c in user .clinicians )) or ""
93
- qb_status = qb_status_visit_name (
94
- patient .userid , research_study_id = rs_id , as_of_date = now )
95
- patient .empro_status = str (qb_status ['status' ])
96
- patient .empro_visit = qb_status ['visit_name' ]
97
- patient .action_state = qb_status ['action_state' ].title () \
98
- if qb_status ['action_state' ] else ""
99
- patient .empro_consentdate , _ = consent_withdrawal_dates (
100
- user = user , research_study_id = rs_id )
101
- db .session .commit ()
93
+ if (research_study_id == EMPRO_RS_ID or research_study_id is None ) and user .clinicians :
94
+ rs_id = EMPRO_RS_ID
95
+ patient .clinician = '; ' .join (
96
+ (clinician_name_map ().get (c .id , "not in map" ) for c in user .clinicians )) or ""
97
+ qb_status = qb_status_visit_name (
98
+ patient .userid , research_study_id = rs_id , as_of_date = now )
99
+ patient .empro_status = str (qb_status ['status' ])
100
+ patient .empro_visit = qb_status ['visit_name' ]
101
+ patient .action_state = qb_status ['action_state' ].title () \
102
+ if qb_status ['action_state' ] else ""
103
+ patient .empro_consentdate , _ = consent_withdrawal_dates (
104
+ user = user , research_study_id = rs_id )
105
+ db .session .commit ()
0 commit comments