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')
class Meta: resource_name = "funfconfig" list_allowed_methods = ["delete", "get", "post"] authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) object_class = Document collection = "funfconfig" # collection name
class Meta: queryset = Device.objects.all() allowed_methods = ("get", "post", "delete") authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) filtering = {"datastore_owner": ALL_WITH_RELATIONS} limit = 20
class Meta: resource_name = "answer" list_allowed_methods = ["get", "post"] help_text = 'resource help text...' authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) object_class = Document isList = False
class Meta: authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=False) resource_name = "funf" list_allowed_methods = ["delete", "get", "post"] object_class = Document collection = "funf" # collection name filtering = {"key": ["exact"]}
class Meta: authentication = OAuth2Authentication("crowdsos_write") authorization = PDSAuthorization(scope="crowdsos_write", audit_enabled=False) resource_name = "incident" list_allowed_methods = ["delete", "get", "post"] object_class = Document collection = "incident" filtering = {"type": ["exact"]}
class Meta: # NOTE: We're using funf_write as the scope just so we don't have to add a new scope to all tokens that will be used for the app # moving forward, this should have its own scope of meetup_write, or something like that authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=False) collection = "meetup_request" resource_name = "meetup_request" list_allowed_methods = ["delete", "get", "post"] object_class = Document
class Meta: queryset = AuditEntry.objects.extra({ 'date': 'date(timestamp)' }).values('date').annotate(count=models.Count("id")) authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=False) fields = ['date', 'count'] allowed_methods = ('get') filtering = { "date": ["gte", "lte", "gt", "lt"], "datastore_owner": ALL_WITH_RELATIONS } ordering = ("date")
class Meta: queryset = AuditEntry.objects.all() # POST is provided to allow a Resource or Sandbox server to store audit entries on the PDS allowed_methods = ('get', 'post') authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=False) filtering = { "datastore_owner": ALL_WITH_RELATIONS, "timestamp": ["gte", "lte", "gt", "lt"], "script": ["contains"], "requester": ALL_WITH_RELATIONS } ordering = ('timestamp') limit = 20
class Meta: queryset = Profile.objects.all() authentication = OAuth2Authentication("funf_write") authorization = PDSAuthorization(scope="funf_write", audit_enabled=True) filtering = {"uuid": ["contains", "exact"]}