def _add_patients(self, database, count, clinic_id=None): """Adds <count> random patients to <database>""" # if we don't specify a clinic, use the database name if clinic_id is None: clinic_id = database.dbname orig_db = CPatient.get_db() CPatient.set_db(database) for i in range(count): p = random_person() p.clinic_ids = [clinic_id,] p.save() CPatient.set_db(orig_db)
def add_form_to_patient(line): change = Change(line) # check the global max seq in this process to avoid repeating ourselves. if change.seq < max_seq: return log_and_abort(logging.DEBUG, "ignoring item %s because it's seq is old") update_max_seq(change) form_id = change.id # don't bother with deleted or old documents if change.deleted or not change.is_current(db): return formdoc = CXFormInstance.get(form_id) pat_id = get_patient_id_from_form(formdoc) if pat_id is not None and get_classification(formdoc.namespace) == "clinic": try: # this is so we don't get conflicting updates. # resolve conflicts and bump version numbers before adding forms pat_data = get_db().get(pat_id, conflicts=True) if "_conflicts" in pat_data: return log_and_abort(logging.INFO, "ignoring patient %s because there are still conflicts that need to be resolved" % pat_id) else: pat = CPatient.wrap(pat_data) if pat.requires_upgrade(): return log_and_abort(logging.INFO, "ignoring patient %s, form %s because it is not yet upgraded to the latest version" % (pat_id, form_id)) found_ids = [enc.xform_id for enc in pat.encounters] if form_id in found_ids and not formdoc.requires_upgrade(): return log_and_abort(logging.DEBUG, "Already found appropriate version of form %s in patient %s, no need to do anything" % (form_id, pat_id)) else: print "Form %s not found in patient %s or was old. Rebuilding the patient now" % (form_id, pat_id) reprocess(pat_id) except ResourceNotFound, e: return log_and_abort(logging.WARNING, "tried to check form %s in patient %s but patient has been deleted. Ignoring" % (form_id, pat_id))
def upgrade_patient(line): change = Change(line) # don't bother with deleted or old documents if change.deleted or not change.is_current(db): return patient_id = change.id try: if patient_id in problem_patients: return log_and_abort(logging.DEBUG, "skipping problem patient: %s" % patient_id) pat_data = get_db().get(patient_id, conflicts=True) # this is so we don't get conflicting updates. resolve conflicts before bumping version numbers. if "_conflicts" in pat_data: return log_and_abort(logging.INFO, "ignoring patient %s because there are still conflicts that need to be resolved" % patient_id) else: pat = CPatient.wrap(pat_data) if pat.requires_upgrade(): print "upgrading patient: %s" % patient_id logging.debug("upgrading patient: %s" % patient_id) if not reprocess(pat.get_id): log_and_abort(logging.ERROR, "problem upgrading patient %s!" % patient_id) problem_patients.append(patient_id) except Exception, e: logging.exception("problem upgrading patient (id: %s)" % patient_id) problem_patients.append(patient_id)
def testReprocessUpgradesVersion(self): patient = random_person() patient.app_version = MIN_VERSION patient.save() self.assertEqual(MIN_VERSION, patient.app_version) self.assertEqual(MIN_VERSION, patient.original_app_version) reprocess(patient.get_id) patback = CPatient.get(patient.get_id) self.assertEqual(settings.APP_VERSION, patback.app_version) # shouldn't upgrade the original version self.assertEqual(MIN_VERSION, patient.original_app_version)
def add_form_to_patient(patient_id, form): """ From a handle to a patient and xform instance xml, add that form to the patient. Returns the updated patient and newly created form. This will OVERRIDE any existing form with the same ID. """ formdoc = post_xform_to_couch(form) new_form_workflow(formdoc, SENDER_EXPORT, patient_id) pat = CPatient.get(patient_id) if patient_id is not None else None return pat, formdoc
def try_get_patient_id_from_referral(form): """ The referrals might have a patient id or a referral id that we can use to link the patient record. """ if form.namespace == config.CHW_REFERRAL_NAMESPACE: bhoma_id = form.xpath("patient_id") if bhoma_id: str_id = jr_float_to_string_int(bhoma_id) str_id = get_latest_patient_id_format(str_id) try: pat = CPatient.view(VIEW_PATIENT_BY_BHOMA_ID, key=str_id, reduce=False, include_docs=True).one() if pat: return pat.get_id else: logging.warning("No patient found with bhoma id %s in referral form %s" % (str_id, form.get_id)) except ResourceNotFound: logging.warning("No patient found with bhoma id %s in referral form %s" % (str_id, form.get_id))
def testReturnToClinicSupercedes(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "ltfu_test2") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_general.xml")) reprocess(updated_patient.get_id) updated_patient = CPatient.get(updated_patient.get_id) self.assertEqual(3, len(updated_patient.cases)) [c1, c2, c3] = sorted(updated_patient.cases, key=lambda case: case.opened_on) # after reprocessing the first case should be closed with returned to clinic, # but the second should be ltfu self.assertTrue(c1.closed) self.assertEqual(const.Outcome.RETURNED_TO_CLINIC, c1.outcome) self.assertTrue(c2.closed) self.assertEqual(const.Outcome.LOST_TO_FOLLOW_UP, c2.outcome)
def import_patient_json(pat_data): """ From a handle to a json file, import patient data. Returns the newly created patient. This will OVERRIDE any existing patient with the same ID. and DELETE ALL FORMS ASSOCIATED WITH THE PATIENT. """ id = get_id(pat_data) if get_db().doc_exist(id): get_db().save_doc(pat_data, force_update=True) else: try: pat_data.pop("_rev") except KeyError: pass get_db().save_doc(pat_data, force_update=True) pat = CPatient.get(id) for form in pat.xforms(): form.delete() return pat