def set_mother_arvs(entry):
    """
    Look up and set ARV's for mom.
    """
    schemafile = './apr/registry-schema.json'
    with open(schemafile) as f:
        schema = json.loads(f.read())
    arvs = schema['faces-mapping']['arvs']
    arvset = []
    begin_date = entry.date_of_outcome - datetime.timedelta(360*2)
    encobs = entry.motherobs.filter(voided=0,
        encounter_datetime__gte=begin_date,
        encounter_datetime__lte=entry.date_of_outcome).order_by('encounter_datetime')
    course = 1
    cur_arv = None
    for encob in encobs:
        if encob.concept_id == 1571:
            #arvset.add(encob.value_coded)
            if encob.value_coded != cur_arv:
                cur_arv = encob.value_coded
                arvt = ArvTherapy()
                arvt.registry_entry = entry
                arvt.course = course
                arvt.medcode = arvs[str(cur_arv)]['medcode']
                arvt.date_began = encob.encounter_datetime.date()
                arvt.save(using=APR_DB)
                print 'c: ', course, ' medcode: ', arvs[str(cur_arv)]['medcode'], ' datestarted: ', encob.encounter_datetime
                course += 1
            else:
                pass
            print encob.encounter_datetime, encob.value_coded
Exemplo n.º 2
0
def add_mother_arvs(entry, save=False):
    schemafile = "./apr/registry-schema.json"
    with open(schemafile) as f:
        schema = json.loads(f.read())
    arvs = schema["faces-mapping"]["arvs"]
    begin_date = entry.date_of_outcome - datetime.timedelta(540)
    preg_encobs = (
        Obs.objects.using(FACES_OPENMRS_DB)
        .filter(
            person_id=entry.mother_id, voided=0, obs_datetime__gte=begin_date, obs_datetime__lte=entry.date_of_outcome
        )
        .order_by("obs_datetime")
    )

    # arvs
    course = 1
    cur_arv = None
    arvs_obs = preg_encobs.filter(concept_id=1571)
    if arvs_obs.count() == 0:
        entry.voided_reason = RegistryEntry.VOIDED_NO_ARV_HISTORY
        entry.voided = True
        if save:
            entry.save()
    for arv in arvs_obs:
        if arv.value_coded != cur_arv:
            cur_arv = arv.value_coded_id
            arvt = ArvTherapy()
            arvt.registry_entry = entry
            arvt.course = course
            # print "The cur_arv is: ", cur_arv, " ", str(cur_arv)
            arvt.medcode = arvs[str(cur_arv)]["medcode"]
            arvt.date_began = arv.obs_datetime
            if save:
                arvt.save(using=APR_DB)
            # print 'c: ', course, ' medcode: ', arvs[str(cur_arv)]['medcode'], ' datestarted: ', arv.obs_datetime
            course += 1
        else:
            pass
Exemplo n.º 3
0
def set_mother_arvs(entry, save_entries=False):
    """
    Look up and set ARV's for mom. Returns a list of new ARV's.
    """
    schemafile = './apr/registry-schema.json'
    with open(schemafile) as f:
        schema = json.loads(f.read())
    arvs = schema['ampath-mapping']['arvs']
    arvset = []
    begin_date = entry.date_of_outcome - datetime.timedelta(360*2)
    log_data(entry, "Beginning date for searching ARVs %s" % (begin_date,), 'arvs', 1088)
    encobs = Obs.objects.using(AMRS_DB).filter(voided=0,
        obs_datetime__gte=begin_date,
        obs_datetime__lte=entry.date_of_outcome,
        person_id=entry.mother_id).order_by('obs_datetime')
    course = 1
    cur_arv = None
    for encob in encobs:
        if encob.concept_id == 1088:
            if encob.value_coded_id != cur_arv:
                cur_arv = encob.value_coded_id
                arvt = ArvTherapy()
                arvt.registry_entry = entry
                arvt.course = course
                arvt.medcode = arvs[str(cur_arv)]['medcode']
                arvt.date_began = encob.obs_datetime.date()
                log_data(entry, "Using ARV %s %s" % (encob.concept_id, encob.obs_datetime,), 'arv', 1088)
                if save_entries:
                    arvt.save(using=APR_DB)
                arvset.append(arvt)
                course += 1
            else:
                log_data(entry, "Skipping cur ARV %s %s" % (encob.concept_id, encob.obs_datetime,), 'arv', 1088)
                pass
    # Go through and adjust the end dates as well as the ongoing flag 
    for idx, arvt in enumerate(arvset):
        if len(arvset) == 1:
            arvt.ongoing = 1
        elif idx == 0:
            arvt.ongoing = 0
            arvt.date_ended = arvset[idx+1].date_began
        elif idx == (len(arvset)-1):
            arvt.ongoing = 1
        else:
            arvt.ongoing = 0
            arvt.date_ended = arvset[idx+1].date_began
        print("Saving the updated status...")
        arvt.save(using=APR_DB)
    return arvset