Example #1
0
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)
Example #2
0
File: case.py Project: dimagi/bhoma
def get_healthy_pregnancy_case(pregnancy, encounter):
    # Any pregnancy case that is created that hasn't been closed by a delivery 
    # gets a followup.  The followups become active at week 42 and expires in
    # week 46 of the pregnancy
    # The CHW should track the delivery and give an outcome to the pregnancy.
    
    lmp = pregnancy.lmp
    # TODO: is this check/logic necessary?
    if lmp:
        send_to_phone = True
        reason = "pregnancy_expecting_outcome"
    else:
        send_to_phone = False
        reason = "unknown_lmp"
    
    ltfu_date = lmp + timedelta(days=46*7) if lmp else None
    bhoma_case = PatientCase(_id=get_bhoma_case_id_from_pregnancy(pregnancy), 
                             opened_on=datetime.combine(encounter.visit_date, time()),
                             modified_on=datetime.utcnow(),
                             type=const.CASE_TYPE_PREGNANCY,
                             encounter_id=encounter.get_id,
                             patient_id=get_patient_id_from_form(encounter.get_xform()),
                             ltfu_date=ltfu_date,
                             outcome=pregnancy.outcome,
                             closed=pregnancy.closed,
                             closed_on=pregnancy.closed_on,
                             send_to_phone=send_to_phone,
                             send_to_phone_reason=reason)
    bhoma_case.status = "pending outcome"
    
    if send_to_phone and not bhoma_case.closed:
        cccase = get_first_commcare_case(encounter, bhoma_case=bhoma_case, 
                                     case_id=get_commcare_case_id_from_block(encounter,bhoma_case))
        cccase.followup_type = const.PHONE_FOLLOWUP_TYPE_PREGNANCY
    
        # starts and becomes active the same day, 42 weeks from LMP
        bhoma_case.lmp = lmp
        cccase.start_date = lmp + timedelta(days= 7 * 42)
        cccase.missed_appointment_date = cccase.start_date
        cccase.activation_date = cccase.start_date
        cccase.due_date = cccase.activation_date + timedelta(days=DAYS_AFTER_PREGNANCY_ACTIVE_DUE)
        bhoma_case.commcare_cases = [cccase]
    return bhoma_case