def delete_events(): if request.method == 'GET': return render_template('delete_form.html') elif request.method == 'POST': if 'submit' in request.form: # Access form properties, get participant information, get events, and delete participant_id = request.form['participant'] rc = Redcap(api_token=current_app.config['AUTOMATIONCONFIG'] ['redcap_api_token']) try: phone_number = rc.get_participant_phone(participant_id) except RedcapError as err: flash(str(err), 'danger') return render_template('delete_form.html') apptoto = Apptoto( api_token=current_app.config['AUTOMATIONCONFIG'] ['apptoto_api_token'], user=current_app.config['AUTOMATIONCONFIG']['apptoto_user']) begin = datetime.now() event_ids = apptoto.get_events(begin=begin, phone_number=phone_number) for event_id in event_ids: apptoto.delete_event(event_id) flash('Deleted messages', 'success') return render_template('delete_form.html')
def diary_form(): if request.method == 'GET': return render_template('daily_diary_form.html') elif request.method == 'POST': if 'submit' in request.form: # Access form properties and do stuff error = _validate_participant_id(request.form) if error: for e in error: flash(e, 'danger') return render_template('daily_diary_form.html') rc = Redcap(api_token=current_app.config['AUTOMATIONCONFIG'] ['redcap_api_token']) try: part = rc.get_session_0(request.form['participant']) except RedcapError as err: flash(str(err), 'danger') return render_template('daily_diary_form.html') eg = EventGenerator(config=current_app.config['AUTOMATIONCONFIG'], participant=part, instance_path=current_app.instance_path) if not eg.daily_diary(): flash('Failed to create daily diary round 1', 'danger') return render_template('daily_diary_form.html')
def generation_form(): if request.method == 'GET': return render_template('generation_form.html') elif request.method == 'POST': if 'submit' in request.form: # Access form properties and do stuff error = _validate_participant_id(request.form) if error: for e in error: flash(e, 'danger') return render_template('generation_form.html') rc = Redcap(api_token=current_app.config['AUTOMATIONCONFIG'] ['redcap_api_token']) try: part = rc.get_participant_specific_data( request.form['participant']) except RedcapError as err: flash(str(err), 'danger') return render_template('generation_form.html') eg = EventGenerator(config=current_app.config['AUTOMATIONCONFIG'], participant=part, instance_path=current_app.instance_path) if eg.generate(): f = eg.write_file() return send_file(f, mimetype='text/csv', as_attachment=True) else: flash('Failed to create some messages', 'danger') return render_template('generation_form.html')
def task(): if request.method == 'GET': return render_template('task_form.html') elif request.method == 'POST': if 'value-task' in request.form: error = _validate_participant_id(request.form) if error: for e in error: flash(e, 'danger') return render_template('task_form.html') rc = Redcap(api_token=current_app.config['AUTOMATIONCONFIG'] ['redcap_api_token']) try: part = rc.get_participant_specific_data( request.form['participant']) except RedcapError as err: flash(str(err), 'danger') return render_template('task_form.html') eg = EventGenerator(config=current_app.config['AUTOMATIONCONFIG'], participant=part, instance_path=current_app.instance_path) f = eg.task_input_file() return send_file(f, mimetype='text/csv', as_attachment=True)
def test_get_session_0_valid(self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data]) participant = rc.get_session_0('ASH999') assert participant.participant_id == 'ASH999'
def test_get_participant_specific_data_invalid_id(self, requests_mock): session0_data.update(session1_data) rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data]) with pytest.raises(RedcapError): rc.get_participant_specific_data('Invalid ID')
def test_get_participant_phone_valid(self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data]) post = rc.get_participant_phone('ASH999') assert post assert post == '555-555-1234'
def test_get_participant_phone_invalid_id(self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data]) with pytest.raises(RedcapError) as e: rc.get_participant_phone('Invalid ID') assert 'phone number' in str(e.value)
def test_get_session_0_blank_sleeptime(self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data_missing_sleep]) with pytest.raises(RedcapError) as e: rc.get_session_0('ASH999') assert 'sleep time' in str(e.value)
def test_get_session_0_invalid_id(self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data_invalid_date]) with pytest.raises(RedcapError) as e: rc.get_session_0('Invalid ID') assert 'Unable to find session 0' in str(e.value)
def test_get_participant_specific_data_does_not_follow_schema( self, requests_mock): rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[{ 'ash_id': 123 }]) with pytest.raises(RedcapError): rc.get_participant_specific_data('Response from REDCap')
def test_get_participant_specific_data_valid_id(self, requests_mock): session0_data.update(session1_data) rc = Redcap(api_token='test token') requests_mock.post(url=rc._endpoint, status_code=requests.codes.ok, json=[session0_data]) part = rc.get_participant_specific_data('ASH999') assert part.participant_id == 'ASH999' assert part.message_values == [ CodedValues.humor, CodedValues.relationships ] assert part.task_values == [CodedValues.humor, CodedValues.athletic]
def test_get_participant_specific_data_missing_session( self, requests_mock): # Test when session 1 is missing rc = Redcap(api_token='test token') requests_mock.post(rc._endpoint, [{ 'json': [session0_data], 'status_code': requests.codes.ok }, { 'json': [], 'status_code': requests.codes.ok }]) with pytest.raises(RedcapError) as e: rc.get_participant_specific_data('ASH999') assert 'session 1' in str(e.value)
def participant_responses(participant_id): part = ImmutableMultiDict({'participant': participant_id}) error = _validate_participant_id(part) if error: return make_response((jsonify(error), 400)) # Use participant ID to get phone number, then get all events and filter conversations for participant responses. rc = Redcap( api_token=current_app.config['AUTOMATIONCONFIG']['redcap_api_token']) try: phone_number = rc.get_participant_phone(participant_id) except RedcapError as err: return make_response((jsonify(str(err)), 404)) apptoto = Apptoto( api_token=current_app.config['AUTOMATIONCONFIG']['apptoto_api_token'], user=current_app.config['AUTOMATIONCONFIG']['apptoto_user']) conversations = apptoto.get_conversations(phone_number=phone_number) return make_response(jsonify(conversations), 200)