def data(request): print "\n\n DATA REACHED \n\n" '''parse json data and upload them to your PDS''' if request.method == 'GET': return HttpResponseBadRequest("GET requests disallowed", content_type = 'text/json') # get necessary info from outside request device_id = request.POST['device_id'] # flie_hash = request.POST['file_hash'] token = request.POST['bearer_token'] data = json.loads(request.POST['data']) result = {} # use token to authorize writing to the PDS authorization = PDSAuthorization("ios_write", audit_enabled=True) if (not authorization.is_authorized(request)): print "authorization not received" return HttpResponse("Unauthorized", status=401) # Get the relevant internal data store datastore_owner, ds_owner_created = Profile.objects.get_or_create(uuid = device_id) internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", token) # write to the datastore result = extractAndInsertData(data, internalDataStore, token) # let the client know what happened print result if result.get('status'): return HttpResponse(json.dumps(result), content_type='application/json') else: return HttpResponseBadRequest(json.dumps(result), \ content_type='application/json')
def data(request): '''decrypt funf database files, and upload them to your PDS''' result = {} if request.method == 'GET': template = {'token': request.GET['bearer_token']} return HttpResponse("File not found", status=404) pds = None #scope = AccessRange.objects.get(key="funf_write") authorization = PDSAuthorization("funf_write", audit_enabled=False) if (not authorization.is_authorized(request)): return HttpResponse("Unauthorized", status=401) scope = 'funf_write' token = request.GET['bearer_token'] datastore_owner_uuid = request.GET["datastore_owner__uuid"] datastore_owner, ds_owner_created = Profile.objects.get_or_create( uuid=datastore_owner_uuid) print "Creating IDS for %s" % datastore_owner_uuid #internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", "Activity", token) internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", token) #collection = connection[datastore_owner.getDBName()]["funf"] funf_password = "******" key = decrypt.key_from_password(str(funf_password)) print "PDS: set_funf_data on uuid: %s" % datastore_owner_uuid for filename, file in request.FILES.items(): try: try: file_path = upload_dir + file.name write_file(str(file_path), file) except Exception as ex: print "failed to write file to " + file_path + ". Please make sure you have write permission to the directory set in settings.SERVER_UPLOAD_DIR" dbdecrypt.decrypt_if_not_db_file(file_path, key) con = sqlite3.connect(file_path) cur = con.cursor() cur.execute("select name, value from data") inserted = [] for row in cur: name = convert_string(row[0]) json_insert = clean_keys(json.JSONDecoder().decode( convert_string(row[1]))) #print json_insert$ # Insert into PDS$ pds_data = {} pds_data['time'] = json_insert.get('timestamp') pds_data['value'] = json_insert pds_data['key'] = name insert_pds(internalDataStore, token, pds_data) inserted.append(convert_string(json_insert) + '\n') result = {'success': True, 'rows_inserted': len(inserted)} print "Inserted %s rows" % len(inserted) except Exception as e: print "Exception from funf_connector on pds:" print "%s" % e result = {'success': False, 'error_message': e.message} finally: response_dict = {"status": "success"} return HttpResponse(json.dumps(result), content_type='application/json')
def data(request): '''decrypt funf database files, and upload them to your PDS''' result = {} if request.method == 'GET': template = {'token':request.GET['bearer_token']} return HttpResponse("File not found", status=404) pds = None #scope = AccessRange.objects.get(key="funf_write") authorization = PDSAuthorization("funf_write", audit_enabled=False) if (not authorization.is_authorized(request)): return HttpResponse("Unauthorized", status=401) scope = 'funf_write' token = request.GET['bearer_token'] datastore_owner_uuid = request.GET["datastore_owner__uuid"] datastore_owner, ds_owner_created = Profile.objects.get_or_create(uuid = datastore_owner_uuid) print "Creating IDS for %s" % datastore_owner_uuid #internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", "Activity", token) internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", token) #collection = connection[datastore_owner.getDBName()]["funf"] funf_password = "******" key = decrypt.key_from_password(str(funf_password)) print "PDS: set_funf_data on uuid: %s" % datastore_owner_uuid for filename, file in request.FILES.items(): try: try: file_path = upload_dir + file.name write_file(str(file_path), file) except Exception as ex: print "failed to write file to "+file_path+". Please make sure you have write permission to the directory set in settings.SERVER_UPLOAD_DIR" dbdecrypt.decrypt_if_not_db_file(file_path, key) con = sqlite3.connect(file_path) cur = con.cursor() cur.execute("select name, value from data") inserted = [] for row in cur: name = convert_string(row[0]) json_insert = clean_keys(json.JSONDecoder().decode(convert_string(row[1]))) #print json_insert$ # Insert into PDS$ pds_data= {} pds_data['time']=json_insert.get('timestamp') pds_data['value']=json_insert pds_data['key']=name insert_pds(internalDataStore, token, pds_data) inserted.append(convert_string(json_insert)+'\n') result = {'success': True, 'rows_inserted': len(inserted)} print "Inserted %s rows" % len(inserted) except Exception as e: print "Exception from funf_connector on pds:" print "%s"%e result = {'success':False, 'error_message':e.message} finally: response_dict = {"status":"success"} return HttpResponse(json.dumps(result), content_type='application/json')
def data(request): print "\n\n DATA REACHED \n\n" '''parse json data and upload them to your PDS''' # does not allow get method when uploading json data. currently not parsing jsons correctly if request.method == 'GET': return HttpResponseBadRequest("GET requests disallowed", content_type='text/json') # get necessary info from outside request device_id = request.POST['device_id'] # flie_hash = request.POST['file_hash'] # temp token to insert data # 'bearer_token' = 3f4851fd8a token = request.POST['bearer_token'] data = json.loads(request.POST['data']) result = {} funfresult = {} # use token to authorize writing to the PDS authorization = PDSAuthorization("ios_write", audit_enabled=True) if (not authorization.is_authorized(request)): print "authorization not received" return HttpResponse("Unauthorized", status=401) # Get the relevant internal data store datastore_owner, ds_owner_created = Profile.objects.get_or_create( uuid=device_id) internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", token) # write to the datastore result = extractAndInsertData(data, internalDataStore, token) # write to funf data funfresult = insertfunf(data, internalDataStore, token) blankdata = getfunfdata.insertblankdata() try: internalDataStore.saveData(blankdata, 'funf') blankdata = {'status': 'ok', 'entries_inserted': 1} except Exception as e: print "\n\nException from os_connector on pds: %s\n" % e blankdata = {'success': False, 'error_message': e.message} # let the client know what happened print result if result.get('status'): return HttpResponse(json.dumps(result), content_type='application/json') else: return HttpResponseBadRequest(json.dumps(result), \ content_type='application/json')
def data(request): print "\n\n DATA REACHED \n\n" '''parse json data and upload them to your PDS''' # does not allow get method when uploading json data. currently not parsing jsons correctly if request.method == 'GET': return HttpResponseBadRequest("GET requests disallowed", content_type = 'text/json') # get necessary info from outside request device_id = request.POST['device_id'] # flie_hash = request.POST['file_hash'] # temp token to insert data # 'bearer_token' = 3f4851fd8a token = request.POST['bearer_token'] data = json.loads(request.POST['data']) result = {} funfresult = {} # use token to authorize writing to the PDS authorization = PDSAuthorization("ios_write", audit_enabled=True) if (not authorization.is_authorized(request)): print "authorization not received" return HttpResponse("Unauthorized", status=401) # Get the relevant internal data store datastore_owner, ds_owner_created = Profile.objects.get_or_create(uuid = device_id) internalDataStore = getInternalDataStore(datastore_owner, "Living Lab", "Social Health Tracker", token) # write to the datastore result = extractAndInsertData(data, internalDataStore, token) # write to funf data funfresult = insertfunf(data, internalDataStore, token) blankdata = getfunfdata.insertblankdata() try: internalDataStore.saveData(blankdata, 'funf') blankdata = {'status':'ok', 'entries_inserted':1} except Exception as e: print "\n\nException from os_connector on pds: %s\n" %e blankdata = {'success':False, 'error_message':e.message} # let the client know what happened print result if result.get('status'): return HttpResponse(json.dumps(result), content_type='application/json') else: return HttpResponseBadRequest(json.dumps(result), \ content_type='application/json')