def GET(self): username, password = get_basic_auth_credentials() r = auth_user(db, username, password) if not r[0]: web.header("Content-Type", "application/json; charset=utf-8") web.header('WWW-Authenticate', 'Basic realm="Auth API"') web.ctx.status = '401 Unauthorized' return json.dumps({'detail': 'Authentication failed!'}) web.header("Content-Type", "application/zip; charset=utf-8")
def GET(self): username, password = get_basic_auth_credentials() r = auth_user(db, username, password) if not r[0]: web.header("Content-Type", "application/json; charset=utf-8") web.header('WWW-Authenticate', 'Basic realm="Auth API"') web.ctx.status = '401 Unauthorized' return json.dumps({'detail': 'Authentication failed!'}) web.header("Content-Type", "application/zip; charset=utf-8") # web.header('Content-disposition', 'attachment; filename=%s.csv'%file_name) web.seeother("/static/downloads/reporters_all.xls.zip")
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'})
def GET(self, district_code): params = web.input(from_date="", type="") 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!'}) y = db.query("SELECT id, lft, rght FROM locations WHERE code = $code", {'code': district_code}) location_id = 0 if y: loc = y[0] location_id = loc['id'] lft = loc['lft'] rght = loc['rght'] SQL = ( "SELECT a.id, a.name, a.code, a.uuid, a.lft, a.rght, a.tree_id, a.tree_parent_id, " "b.code as parent_code, c.level, c.name as type, " "to_char(a.cdate, 'YYYY-mm-dd') as created " " FROM locations a, locations b, locationtype c" " WHERE " " a.tree_parent_id = b.id " " AND a.lft > %s AND a.lft < %s " " AND a.type_id = c.id ") SQL = SQL % (lft, rght) if params.from_date: SQL += " AND a.cdate >= $date " if params.type: SQL += " AND c.name = $type " r = db.query(SQL, { 'id': location_id, 'date': params.from_date, 'type': params.type }) ret = [] for i in r: ret.append(dict(i)) return json.dumps(ret)
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'})
def GET(self): # params = web.input(form="") 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!'}) indicators = db.query( "SELECT id, form_order, form, slug, cmd, description, shortname, dataset, dataelement, " "category_combo, threshold FROM dhis2_mtrack_indicators_mapping " "ORDER BY form, form_order") ret = {} for i in indicators: ret[i["slug"]] = { 'categoryOptionCombo': i['category_combo'], 'dataElement': i['dataelement'], 'descr': i['description'], } return json.dumps(ret)
def GET(self, location_code): params = web.input(role="") 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!'}) ret = [] reporter_role = params.role SQL = ( "SELECT firstname, lastname, telephone, alternate_tel, email, national_id, " "reporting_location, district, role, loc_name, location_code FROM reporters_view4 " "WHERE reporting_location IN (SELECT id FROM get_descendants_including_self(( SELECT id FROM " "locations WHERE code=$location_code))) ") if reporter_role: SQL += " AND lower(role) = $role" res = db.query(SQL, { 'location_code': location_code, 'role': reporter_role.lower() }) if res: for r in res: ret.append({ "firstname": r.firstname, "lastname": r.lastname, "telephone": r.telephone, "alternate_tel": r.alternate_tel, "email": r.email, "national_id": r.national_id, "location_name": r.loc_name, "location_code": r.location_code, "distrcit": r.district, "role": r.role }) return json.dumps(ret)
def GET(self): params = web.input(contact="", babytrigger="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!'}) print(params.contact) SQL = ("SELECT * FROM fcapp_get_secondary_receivers($contact) ") if params.babytrigger == "true": SQL += " WHERE has_msisdn = 'f'" res = db.query(SQL, {'contact': params.contact}) payload = {'secreceivers': {}} receivers_count = 0 screen_1 = "" screen_2 = "" screen_3 = "" # screen_1_len = 0 # screen_2_len = 0 # screen_3_len = 0 if res: for idx, r in enumerate(res, 1): receivers_count += 1 if idx < 6: screen_1 += "%s. %s\n" % (idx, r['name']) payload['secreceivers']['%s' % idx] = { 'name': r['name'], 'uuid': r['uuid'], 'contact_id': r['contact_id'], 'contact_field': r['contact_field'] } elif idx > 5 and idx < 11: screen_2 += "%s. %s\n" % ((idx + 1), r['name']) payload['secreceivers']['%s' % (idx + 1)] = { 'name': r['name'], 'uuid': r['uuid'], 'contact_id': r['contact_id'], 'contact_field': r['contact_field'] } elif idx > 10 and idx < 16: screen_3 += "%s. %s\n" % ((idx + 2), r['name']) payload['secreceivers']['%s' % (idx + 2)] = { 'name': r['name'], 'uuid': r['uuid'], 'contact_id': r['contact_id'], 'contact_field': r['contact_field'] } # screen_1_len = len(screen_1.strip().split('\n')) # screen_2_len = len(screen_2.strip().split('\n')) if screen_2 else 0 # screen_3_len = len(screen_3.strip().split('\n')) if screen_3 else 0 if screen_2: screen_1 += "6. More\n" screen_2 += "0. Back" if screen_3: screen_2 += "12. More\n" screen_3 += "0. Back" payload['receivers_count'] = receivers_count payload['screen_1'] = screen_1 payload['screen_2'] = screen_2 payload['screen_3'] = screen_3 # payload['screen_1_len'] = screen_1_len # payload['screen_2_len'] = screen_2_len # payload['screen_3_len'] = screen_3_len pprint.pprint(payload) return json.dumps(payload)