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=True) 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")
class Meta: queryset = ResourceKey.objects.all() authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) filtering = {"datastore_owner": ALL_WITH_RELATIONS}
class Meta: queryset = Profile.objects.all() authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) filtering = {"uuid": ["contains", "exact"]}
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=True) 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) collection = connection["User_" + str(datastore_owner.id)]["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(): #authenticator = JSONAuthenticator(scope=scope) #try: # # Validate the request. # authenticator.validate(request) #except AuthenticationException as e: # # Return an error response. # print e # return authenticator.error_response(content="You didn't authenticate.") #profile = authenticator.user.get_profile() 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(collection, 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 e.message 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=True) 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) collection = connection["User_" + str(datastore_owner.id)]["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(): #authenticator = JSONAuthenticator(scope=scope) #try: # # Validate the request. # authenticator.validate(request) #except AuthenticationException as e: # # Return an error response. # print e # return authenticator.error_response(content="You didn't authenticate.") #profile = authenticator.user.get_profile() 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(collection, 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 e.message result = {'success': False, 'error_message': e.message} finally: response_dict = {"status": "success"} return HttpResponse(json.dumps(result), content_type='application/json')