Exemplo n.º 1
0
def nut_register(message, args, sub_cmd, cmd):
    """ Incomming:
            nut register hc_code, create_date, patient_id,
                         first_name, last_name, mother, sex, dob, contact
                         #weight height oed pb nbr, is_ureni
            exple: 'nut register sab3 20121004 23 Moussa Kone Camara M 7
                    35354#6 65 YES 111 25 0'
        Outgoing:
            [SUCCES] Le rapport de name_health_center a ete enregistre.
            Son id est 8.
            or  [ERREUR] Votre rapport n'a pas été enregistrer"""

    try:
        register_data, follow_up_data = args.split('#')

        hc_code, create_date, patient_id, first_name, last_name, mother, sex, dob, contact = register_data.split()

        weight, height, oedema, muac, nb_plumpy_nut, is_ureni = follow_up_data.split()
    except:
        return resp_error(message, u"enregistrement")
    try:
        # On essai prendre le seat
        hc = HealthCenter.objects.get(code=hc_code.lower())
    except:
        # On envoi un sms pour signaler que le code n'est pas valide
        msg = u"[ERREUR] %(hc)s n'est pas un code de Centre valide."\
              % {'hc': hc_code}
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    # check date
    try:
        registration_date = formatdate(create_date, True)
    except ValueError as e:
        return resp_error(message, e)
    # Car on ne traite que les URENI pour l'instant
    type_uren = NutritionalData.SAM
    # True or False
    is_ureni = bool(int(is_ureni))

    if is_ureni:
        if hc_code in ["qmali", "cr"]:
            type_uren = NutritionalData.SAMP
        else:
            msg = u"[ERREUR] Seul les CSREF ont le droit " \
                  u"d'enregistrer les enfants URENI"
            message.respond(msg)
            send_sms(FOL_NUMBER, msg)
            return True
    try:
        patient_id = clean_up_pid(patient_id)
        nut_id = Patient.get_nut_id(hc_code, type_uren.lower(), patient_id)
    except ValueError as e:
        return resp_error(message, e)

    patient = Patient()
    patient.nut_id = nut_id
    patient.first_name = first_name.replace('_', ' ').title()
    patient.last_name = last_name.replace('_', ' ').title()
    patient.surname_mother = mother.replace('_', ' ').title()
    patient.create_date = registration_date
    try:
        patient.birth_date = formatdate(dob)
    except ValueError as e:
        message.respond(u"[ERREUR] %(e)s" % {'e': e})
        return True

    patient.sex = sex.upper()
    patient.contact = contact
    patient.health_center = hc
    try:
        patient.save()
    except IntegrityError:
        patient = Patient.objects.get(nut_id=nut_id)
        save_error(message, u"/!\ l'identifiant #%(id)s appartient déjà à"
                            u" %(full_name)s. enregistré au centre"
                            u" %(health_center)s le %(date)s"
                            u" Pour plus d'information, appelez ce numéro %(admin)s"
                   % {'full_name': patient.full_name(), 'id': nut_id,
                      'date': patient.create_date.strftime("%x %H: %Mmm %Ss"),
                      'health_center': patient.health_center.name,
                      'admin': FOL_NUMBER})
        return False
    except:
        return save_error(message, u"Impossible d'enregistrer ce rapport dans "
                                   u"la base des donnees")

    # adding patient to the program
    programio = ProgramIO()
    programio.patient = patient
    programio.event = programio.SUPPORT
    programio.date = registration_date
    try:
        programio.save()
    except:
        return resp_error(message, u"enregistrement")

    # creating a followup event
    weight = float(weight)
    height = float(height)
    oedema = {'yes': NutritionalData.OEDEMA_YES,
              'no': NutritionalData.OEDEMA_NO,
              'unknown': NutritionalData.OEDEMA_UNKNOWN}[oedema.lower()]
    muac = int(muac)
    nb_plumpy_nut = int(nb_plumpy_nut) if not str(nb_plumpy_nut).lower() == '-' else 0
    datanut = add_followup_data(patient=patient, weight=weight,
                                height=height, oedema=oedema, muac=muac,
                                nb_plumpy_nut=nb_plumpy_nut,
                                is_ureni=is_ureni,
                                date=registration_date)
    if not datanut:
        msg = u"/!\ %(full_name)s enregistre avec ID#%(id)s." \
              u" Donnees nutrition non enregistres." % {'full_name': patient.full_name(), 'id': nut_id}
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    message.respond(u"[SUCCES] %(full_name)s enregistre avec ID#%(id)s."
                    u" Donnees nutrition enregistres." % {'full_name': patient.full_name_mother(), 'id': nut_id})
    return True