Example #1
0
def assessments(request):
    data_client = DataClient()
    completed = get_sidebar_completed()
    tests_completed = get_tests_completed()
    if request.method == 'POST':
        assessments_form = AssessmentForm(request.POST)
        if assessments_form.is_valid():
            chosen_list = []
            for field in assessments_form.fields:
                if (assessments_form.cleaned_data[field]):
                    chosen_list.append(field)
            if not chosen_list:
                return HttpResponseRedirect('/app/assessments/')
            else:
                data_client.assessments_chosen = chosen_list
                return HttpResponseRedirect('/app/assessments/details')
    else:
        assessments_form = AssessmentForm()

    # Comment this out later, just want to see it works
    # print(tests_completed)
    return render(
        request, 'app/assessments.html', {
            'assessments_form': assessments_form,
            'patient': data_client.patient,
            'completed': completed,
            'tests_completed': tests_completed
        })
Example #2
0
def assessments_details(request):
    """
    If there's anything in this project that needs refactoring, this is it.
    """
    data_client = DataClient()
    assessments_chosen = data_client.assessments_chosen
    completed = get_sidebar_completed()
    more_info = {}
    for assessment in assessments_chosen:
        for test in data_client.func_test:
            if (test['code'] == assessment):
                more_info[assessment] = test['more_info']
    for key, value in more_info.items():
        for test in data_client.func_test:
            if test["code"] == key:
                more_info[key]["name"] = test["name"]
    if request.method == 'POST':
        assessments_form = AssessmentDetailsForm(
            request.POST, assessments_chosen=assessments_chosen)
        if assessments_form.is_valid():
            # Local obs just in case
            observations = {}

            # Boolean to ultimately determine if patient fails GSB
            has_problem = False
            # Score of how many key questions have been answered 'yes' (use 0 because adds 1 each iteration)
            tug_key = 0
            # Minimum amount of key questions needed to be answered 'yes' to fail (use -1 as a check if test conducted)
            tug_min_key = -1
            # Score of how many evaluations have exceeded their respective min time (use 0 because adds 1 each iteration)
            bal_failure = 0
            # Minimum amount of failures for evaluations to fail the test overall (use -1 as a check if test conducted)
            bal_min_failure = -1
            # Minimum amount of time needed for patient to stand in chair (use -1 as a check if test conducted)
            chair_min_failure = -1
            bal_score = 0
            tug_conducted = False
            bal_conducted = False
            chair_conducted = False

            for test in data_client.func_test:
                if test['code'] in data_client.assessments_chosen:
                    # Pulls required fields to evaluate logic
                    if test['code'] == "tug000":
                        tug_min_key = test['min_logic']['min_key']
                        tug_conducted = True
                    elif test['code'] == "bal000":
                        bal_min_failure = test['min_logic']['min_failure']
                        bal_conducted = True
                    elif test['code'] == "chair000":
                        chair_conducted = True

                    for i, form in enumerate(test['forms']):
                        code = test['forms'][i]['code']
                        field_name = code
                        answer = assessments_form.cleaned_data[field_name]
                        data_client.observations[code] = answer
                        observations[code] = answer

                        # Check logic for TUG
                        if test['code'] == "tug000":
                            # Check for key questions
                            if test['forms'][i]['type'] == 'boolean':
                                if test['forms'][i]['is_key'] and answer:
                                    tug_key = tug_key + 1
                                if test['forms'][i][
                                        'code'] == 'tug001' and answer:
                                    has_problem = True
                                    data_client.observations["tug000"] = "Fail"
                                    observations["tug000"] = "Fail"
                            # Check for timing scores
                            if test['forms'][i]['type'] == 'integer':
                                # Check to make sure it isnt NoneType
                                if answer is not None:
                                    form_logic = test['forms'][i]['logic']
                                    if form_logic in test['min_logic']:
                                        if answer < test['min_logic'][
                                                form_logic]:
                                            has_problem = True
                                            data_client.observations[
                                                "tug000"] = "Fail"
                                            observations["tug000"] = "Fail"
                        # Check logic for 30 Chair
                        if test['code'] == 'chair000':
                            if test['forms'][i]['type'] == 'integer':
                                form_logic = test['forms'][i]['logic']
                                if form_logic in test['min_logic']:
                                    patient_gender = data_client.patient[
                                        'resource']['gender']
                                    date_string = data_client.patient[
                                        'resource']['birthDate']
                                    patient_age = calculate_age(date_string)
                                    age_index = test['min_logic'][form_logic][
                                        'ages']
                                    male_score = test['min_logic'][form_logic][
                                        'male']
                                    female_score = test['min_logic'][
                                        form_logic]['female']
                                    if patient_age < age_index[0]:
                                        print(
                                            "Age is less than minimum age for test, will pass since score irrelevant"
                                        )
                                    if patient_age > age_index[-1]:
                                        print(
                                            "Age is greater than maximum age for test, will fail since score irrelevant"
                                        )
                                    else:
                                        print(age_index)
                                        for i, age in enumerate(age_index):
                                            if patient_age >= age:
                                                continue
                                            else:
                                                index = i - 1
                                                break
                                    if patient_gender == 'male':
                                        chair_min_failure = male_score[index]
                                    elif patient_gender == 'female':
                                        chair_min_failure = female_score[index]
                                    if answer is not None and chair_min_failure >= 0:
                                        if answer < chair_min_failure:
                                            has_problem = True
                                            data_client.observations[
                                                "chair000"] = "Fail"
                                            observations["chair000"] = "Fail"

                        # Check logic for Balance Test
                        if test['code'] == 'bal000':
                            if test['forms'][i]['type'] == 'integer':
                                # Bug here: what if logic doesn't exist?
                                form_logic = test['forms'][i]['logic']
                                if form_logic in test['min_logic']:
                                    # However, still force them to enter 0 so that the field is acknowledged
                                    if answer is None:
                                        bal_score = bal_score + 1
                                    if answer is not None and answer < test[
                                            'min_logic'][form_logic]:
                                        bal_score = bal_score + 1

            # Final logic checks for tests
            if tug_min_key >= 0 and tug_key > tug_min_key:
                # print ("has problem from key tugs")
                has_problem = True
                data_client.observations["tug000"] = "Fail"
                observations["tug000"] = "Fail"
            if bal_min_failure >= 0 and bal_score > bal_min_failure:
                # print ('has problem from key bal')
                has_problem = True
                data_client.observations["bal000"] = "Fail"
                observations["bal000"] = "Fail"
            # TODO: If "fail is in before, and someone changes to pass, this wont work."
            if tug_conducted:
                if "tug000" not in observations:
                    data_client.observations["tug000"] = "Pass"
            if bal_conducted:
                if "bal000" not in observations:
                    data_client.observations["bal000"] = "Pass"
            if chair_conducted:
                if "chair000" not in observations:
                    data_client.observations["chair000"] = "Pass"

            #save observations to FHIR server
            data_client.fhir_client.write_list_of_observations_to_fhir(
                data=data_client.observations)
            # Wipe clean for next iteration through, if desired to do more assessments
            data_client.assessments_chosen = []

            # Call calculate_risk at this point...
            calculate_risk()

            if has_problem:
                if data_client.risk_level == "incomplete":
                    return HttpResponseRedirect('/app/risks/')
                else:
                    return HttpResponseRedirect('/app/medications/')
            else:
                return HttpResponseRedirect('/app/risks/')

    else:
        assessments_answers = {}
        for test in data_client.func_test:
            if test['code'] in data_client.assessments_chosen:
                for i, form in enumerate(test['forms']):
                    code = test['forms'][i]['code']
                    if code in data_client.observations:
                        field_name = code
                        assessments_answers[
                            field_name] = data_client.observations[code]
                        if isinstance(assessments_answers[field_name], str):
                            if assessments_answers[field_name] == "True":
                                assessments_answers[field_name] = True
                            elif assessments_answers[field_name] == "False":
                                assessments_answers[field_name] = False
        assessments_form = AssessmentDetailsForm(
            initial=assessments_answers, assessments_chosen=assessments_chosen)
    return render(
        request, 'app/assessments.html', {
            'assessments_form': assessments_form,
            'patient': data_client.patient,
            'completed': completed,
            'assessments_chosen': assessments_chosen,
            'more_info': more_info
        })