def update_pregnancies(patient, encounter): """ From a patient object, update the list of pregnancies. """ if is_pregnancy_encounter(encounter): # in the reprocessing case, we might already have counted this encounter # in a pregnancy. If that's the case we can totally ignore this. for pregnancy in patient.pregnancies: if pregnancy.contains(encounter): return # by default we create a new pregnancy unless we find a match matched_pregnancy = None if len(patient.pregnancies) > 0: potential_match = patient.pregnancies[-1] if _is_match(potential_match, encounter): matched_pregnancy = potential_match if matched_pregnancy: matched_pregnancy.add_visit(encounter) else: # recursive import :( from bhoma.apps.case.models.couch import Pregnancy preg = Pregnancy.from_encounter(encounter) patient.pregnancies.append(preg) # recursive import :( from bhoma.apps.case.bhomacaselogic.pregnancy.case import update_pregnancy_cases update_pregnancy_cases(patient, encounter) return patient
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()