Example #1
0
def add_form_to_patient(patient_id, form):
    """
    Adds a clinic form to a patient, including all processing necessary.
    """
    
    patient = CPatient.get(patient_id)
    new_encounter = Encounter.from_xform(form)
    patient.encounters.append(new_encounter)
    
    encounter_info = ENCOUNTERS_BY_XMLNS.get(form.namespace)
    if not encounter_info:
        raise Exception("Attempt to add unknown form type: %s to patient %s!" % \
                        (form.namespace, patient_id))
    
    if encounter_info.classification == CLASSIFICATION_CLINIC:
        case = get_or_update_bhoma_case(form, new_encounter)
        if case:
            patient.cases.append(case)
        
        if is_pregnancy_encounter(new_encounter):
            update_pregnancies(patient, new_encounter)

        if is_delivery_encounter(new_encounter):
            update_deliveries(patient, new_encounter)

    elif encounter_info.classification == CLASSIFICATION_PHONE:
        # process phone form
        process_phone_form(patient, new_encounter)
    else:
        logging.error("Unknown classification %s for encounter: %s" % \
                      (encounter_info.classification, form.get_id))
    
    # finally close any previous cases we had open, according
    # to the complicated rules
    close_previous_cases_from_new_form(patient, form, new_encounter)
    patient.save()