Exemple #1
0
    def POST(self):
        params = web.input(district="", subcounty="")
        if params.district and params.subcounty:
            if USE_OLD_WEBHOOKS:
                patient_id = get_webhook_msg_old(params, 'patient_id')
                patient_age = get_webhook_msg_old(params, 'patient_age')
                patient_sex = get_webhook_msg_old(params, 'patient_sex')
            else:
                payload = json.loads(web.data())
                patient_id = get_webhook_msg(payload, 'patient_id')
                patient_age = get_webhook_msg(payload, 'patient_age')
                patient_sex = get_webhook_msg(payload, 'patient_sex')

            district_contacts = CARAMAL_RESEARCH_TEAM.get(params.district, '')
            if district_contacts:
                subcounty_team_contacts = district_contacts.get(params.subcounty, '')
                if subcounty_team_contacts:
                    # time to schedule reminder accordingly
                    msg = (
                        "Patient with ID %s, age of %s and sex %s is "
                        "due for follow up 3 days from now." % (
                            patient_id, int(float(patient_age)), patient_sex))
                    sms_params = {'text': msg, 'to': subcounty_team_contacts}

                    current_time = datetime.datetime.now()
                    run_time = current_time + datetime.timedelta(days=25)
                    queue_schedule(db, sms_params, run_time, None, 'sms')
                    return json.dumps({"message": "scheduled successfully"})

        return json.dumps({"message": "success"})
    def POST(self):
        params = web.input(optoutall="false")
        web.header("Content-Type", "application/json; charset=utf-8")
        username, password = get_basic_auth_credentials()
        r = auth_user(db, username, password)
        if not r[0]:
            web.header('WWW-Authenticate', 'Basic realm="Auth API"')
            web.ctx.status = '401 Unauthorized'
            return json.dumps({'detail': 'Authentication failed!'})

        secreceivers = get_webhook_msg_old(params, 'secreceivers')
        pprint.pprint(secreceivers)
        payload = json.loads(secreceivers)
        if params.optoutall == "true":
            for k, v in payload.iteritems():
                contact_id = v['contact_id']
                contact_field = v['contact_field']
                db.query(
                    "UPDATE values_value SET (string_value, decimal_value) = ('', NULL) "
                    "WHERE contact_id = $contact_id AND contact_field_id = $contact_field_id",
                    {
                        'contact_id': contact_id,
                        'contact_field_id': contact_field
                    })
            return json.dumps({'success': 'true'})

        optout_option = get_webhook_msg_old(params, 'OptOutOption')
        print("OptOutOption => ", optout_option)

        try:
            contact_details = payload['%s' % int(float(optout_option))]
        except:
            contact_details = None
        if not contact_details:
            return json.dumps({'success': 'False'})

        contact_id = contact_details['contact_id']
        contact_field = contact_details['contact_field']
        print("contact_id=>", contact_id, " fields => ", contact_field)

        db.query(
            "UPDATE values_value SET (string_value, decimal_value) = ('', NULL) "
            "WHERE contact_id = $contact_id AND contact_field_id = $contact_field_id",
            {
                'contact_id': contact_id,
                'contact_field_id': contact_field
            })

        return json.dumps({'success': 'True'})
Exemple #3
0
    def POST(self, form):
        params = web.input()
        if USE_OLD_WEBHOOKS:
            msg = get_webhook_msg_old(params, 'msg')
        else:
            payload = json.loads(web.data())
            msg = get_webhook_msg(payload, 'msg')

        message = parse_message(msg, form)

        return json.dumps({"message": message})
    def POST(self):
        params = web.input()
        web.header("Content-Type", "application/json; charset=utf-8")
        username, password = get_basic_auth_credentials()
        r = auth_user(db, username, password)
        if not r[0]:
            web.header('WWW-Authenticate', 'Basic realm="Auth API"')
            web.ctx.status = '401 Unauthorized'
            return json.dumps({'detail': 'Authentication failed!'})

        client = TembaClient(
            config.get('familyconnect_uri', 'http://localhost:8000/'),
            config['api_token'])

        secreceivers = get_webhook_msg_old(params, 'secreceivers')
        pprint.pprint(secreceivers)
        payload = json.loads(secreceivers)

        optout_option = get_webhook_msg_old(params, 'OptOutOption')
        print("OptOutOption => ", optout_option)
        try:
            contact_details = payload['%s' % int(float(optout_option))]
        except:
            contact_details = None
        if not contact_details:
            return json.dumps({'success': 'False'})

        contact_id = contact_details['contact_id']
        contact_uuid = contact_details['uuid']
        print("contact_id=>", contact_id, " uuid => ", contact_uuid)

        date_of_birth = get_webhook_msg_old(params, 'child_dob')

        try:
            client.create_flow_start(config['babytrigger_flow_uuid'],
                                     contacts=[contact_uuid],
                                     extra={'child_dob': date_of_birth})
        except:
            pass

        return json.dumps({'success': 'True'})