def patient_start_ivr(request, patient_id): """Start interactive voice interaction with patient.""" patient = get_object_or_404(patients.Patient, pk=patient_id) survey = PatientSurvey(patient=patient, query_type=QUERY_TYPE_IVR, is_test=True) survey.start() return redirect('patient-list')
def patient_start_adherence_tree(request, patient_id): """Start adherence tree interaction with patient.""" patient = get_object_or_404(patients.Patient, pk=patient_id) survey = PatientSurvey(patient=patient, query_type=QUERY_TYPE_SMS, is_test=True) survey.start() return redirect('patient-list')
def patient_ivr_complete(request, patient_id): """ We tell tropo to call us back at this view's URL with the results (good or bad) of running an IVR. """ logger.debug("##%s" % request.raw_post_data) patient = get_object_or_404(patients.Patient, pk=patient_id) survey = PatientSurvey.find_active(patient, QUERY_TYPE_IVR) try: if not survey: logger.error( "Could not find an IVR survey in progress for patient %s" % patient) return http.HttpResponseServerError() postdata = json.loads(request.raw_post_data) if 'result' in postdata: # Survey result result = postdata['result'] logger.debug("## Results=%r" % result) if 'error' in result and result['error'] is not None: logger.error("## Error from phone survey: %s" % result['error']) survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponse() if result['complete'] == False: survey.completed(PatientSurvey.STATUS_NO_ANSWER) return http.HttpResponse() if 'actions' not in result: # Some kind of error if result['state'] == 'DISCONNECTED': # seem to get this if we hang up in the middle survey.completed(PatientSurvey.STATUS_NOT_COMPLETED) return http.HttpResponse() actions = result['actions'] # actions can be either an array of dictionaries or a single # dictionary, sigh. Figure out if it's a single dictionary # and wrap it in an array to avoid insanity. if isinstance(actions, dict): actions = [ actions, ] num_questions_answered = 0 complete = False error = False for item in actions: logger.debug("## action=%s", item) if item['name'] == 'question1' and \ item['disposition'] == 'SUCCESS': # Got an answer, yay answer = item['value'] num_pills = int(answer) if not survey.is_test: # remember result PillsMissed(patient=patient, num_missed=num_pills, source=QUERY_TYPE_IVR).save() complete = True elif item['disposition'] == 'TIMEOUT': pass #incomplete = True else: logger.debug( "## Error on question %s for patient: disposition %s" % (item['name'], item['disposition'])) error = True if error: status = PatientSurvey.STATUS_ERROR elif not complete: status = PatientSurvey.STATUS_NOT_COMPLETED else: status = PatientSurvey.STATUS_COMPLETE survey.completed(status) return http.HttpResponse() # whoops logger.error("patient_ivr_complete called with no result data!!") survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponseServerError() except Exception, e: logger.exception(e) survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponseServerError()
def patient_ivr_complete(request, patient_id): """ We tell tropo to call us back at this view's URL with the results (good or bad) of running an IVR. """ logger.debug("##%s" % request.raw_post_data) patient = get_object_or_404(patients.Patient, pk=patient_id) survey = PatientSurvey.find_active(patient, QUERY_TYPE_IVR) try: if not survey: logger.error("Could not find an IVR survey in progress for patient %s" % patient) return http.HttpResponseServerError() postdata = json.loads(request.raw_post_data) if 'result' in postdata: # Survey result result = postdata['result'] logger.debug("## Results=%r" % result) if 'error' in result and result['error'] is not None: logger.error("## Error from phone survey: %s" % result['error']) survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponse() if result['complete'] == False: survey.completed(PatientSurvey.STATUS_NO_ANSWER) return http.HttpResponse() if 'actions' not in result: # Some kind of error if result['state'] == 'DISCONNECTED': # seem to get this if we hang up in the middle survey.completed(PatientSurvey.STATUS_NOT_COMPLETED) return http.HttpResponse() actions = result['actions'] # actions can be either an array of dictionaries or a single # dictionary, sigh. Figure out if it's a single dictionary # and wrap it in an array to avoid insanity. if isinstance(actions, dict): actions = [actions, ] num_questions_answered = 0 complete = False error = False for item in actions: logger.debug("## action=%s", item) if item['name'] == 'question1' and \ item['disposition'] == 'SUCCESS': # Got an answer, yay answer = item['value'] num_pills = int(answer) if not survey.is_test: # remember result PillsMissed(patient=patient, num_missed=num_pills, source=QUERY_TYPE_IVR).save() complete = True elif item['disposition'] == 'TIMEOUT': pass #incomplete = True else: logger.debug("## Error on question %s for patient: disposition %s" % (item['name'], item['disposition'])) error = True if error: status = PatientSurvey.STATUS_ERROR elif not complete: status = PatientSurvey.STATUS_NOT_COMPLETED else: status = PatientSurvey.STATUS_COMPLETE survey.completed(status) return http.HttpResponse() # whoops logger.error("patient_ivr_complete called with no result data!!") survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponseServerError() except Exception,e: logger.exception(e) survey.completed(PatientSurvey.STATUS_ERROR) return http.HttpResponseServerError()