def reprocess(patient_id): """ Reprocess a patient's data from xforms, by playing them back in the order they are found. Returns true if successfully regenerated, otherwise false. """ # you can't call the loader because the loader calls this patient = CPatient.get(patient_id) # first create a backup in case anything goes wrong backup_id = CPatient.copy(patient) try: # have to change types, otherwise we get conflicts with our cases backup = CPatient.get(backup_id) backup.doc_type = "PatientBackup" backup.save() # reload the original and blank out encounters/cases/version patient = CPatient.get(patient_id) patient.encounters = [] patient.cases = [] # don't blank out pregnancies. we need them in order to preserve ids # patient.pregnancies = [] patient.backup_id = backup_id patient.app_version = None # blanking out the version marks it "upgraded" to the current version patient.save() for form in patient.unique_xforms(): add_form_to_patient(patient_id, form) # save to stick the new version on so we know we've upgraded this form if form.requires_upgrade(): form.save() # only send the updated signal when all the dust has settled patient_updated.send(sender="reprocessing", patient_id=patient_id) get_db().delete_doc(backup_id) return True except Exception, e: logging.exception("problem regenerating patient case data (patient: %s)" % patient_id) current_rev = get_db().get_rev(patient_id) patient = get_db().get(backup_id) patient["_rev"] = current_rev patient["_id"] = patient_id patient["doc_type"] = "CPatient" get_db().save_doc(patient) get_db().delete_doc(backup_id) return False
def new_form_workflow(doc, sender, patient_id=None): """ The shared workflow that is called any time a new form is received (either from the phone, touchscreen, or some import/export utilities) """ if doc.contributes(): if not patient_id: patient_id = get_patient_id_from_form(doc) if not patient_id: patient_id = try_get_patient_id_from_referral(doc) if patient_id: # this hack says that we should add this this magic # property here. This allows us to easily find these # forms later on doc["#patient_guid"] = patient_id doc.save() if patient_id and get_db().doc_exist(patient_id): new_form_received(patient_id=patient_id, form=doc) patient_updated.send(sender=sender, patient_id=patient_id)