コード例 #1
0
def user(request):
	decrypted = booleanize(request.REQUEST.get('decrypted', False))
	accepted_scopes = set(['connector_raw.all_data', 'connector_raw.all_data_researcher'])
	auth = authorization_manager.authenticate_token(request)

	if 'error' in auth:
		log.error(audit.message(request, auth))
		return HttpResponse(json.dumps(auth), status=401)
	
	auth_scopes = set([x for x in auth['scope']])
	if len(accepted_scopes & auth_scopes) == 0:
		log.error(audit.message(request, {'error':'token not authorized for any accepted scope %s'%str(list(accepted_scopes))}))
		return HttpResponse(json.dumps({'error':'token not authorized for any accepted scope %s'%str(list(accepted_scopes))}), status=401)

	is_researcher = False
	for s in auth_scopes:
		if s == 'connector_raw.all_data_researcher': is_researcher = True

	users_to_return = buildUsersToReturn(auth['user'], request, is_researcher = is_researcher)
	roles = []
	try: roles = [x.role for x in UserRole.objects.get(user=auth['user']).roles.all()]
	except: pass

	own_data = False
	if len(users_to_return) == 1 and users_to_return[0] == auth['user'].username: own_data = True
	
	return userBuild(request, users_to_return, decrypted = decrypted, own_data = own_data, roles = roles)
コード例 #2
0
def userBuild(request, users_to_return, decrypted = False, own_data = False, roles = []):
	
	_start_time = time.time()

	pretty = booleanize(request.REQUEST.get('pretty', False))
	response = {}
	response['meta'] = {}

	db = db_wrapper.DatabaseHelper()

	collection= 'device_inventory'

	response['results'] = [x['user'] for x in db.execute_named_query(NAMED_QUERIES["get_unique_users_in_device_inventory"], None) if x['user'] in users_to_return or 'all' in users_to_return]

	response['meta']['execution_time_seconds'] = time.time()-_start_time
	response['meta']['status'] = {'status':'OK','code':200, 'desc':''}
	


	if decrypted:
		pass

	if pretty:
		log.info(audit.message(request, response['meta']))
		return render_to_response('pretty_json.html', {'response': json.dumps(response, indent=2)})
	else:
		log.info(audit.message(request, response['meta']))
		return HttpResponse(json.dumps(response), content_type="application/json", status=response['meta']['status']['code'])
	return HttpResponse('hello decrypted')
コード例 #3
0
def get_data(request, probe_settings):
	decrypted = booleanize(request.REQUEST.get('decrypted', False))

	if decrypted:
		accepted_scopes = set([probe_settings['scope'], 'connector_raw.all_data'])
	else:
		accepted_scopes = set([probe_settings['scope'], 'connector_raw.all_data', 'connector_raw.all_data_researcher'])

	auth = authorization_manager.authenticate_token(request)

	if 'error' in auth:
		response = {'meta':{'status':{'status':'error','code':401,'desc':auth['error']}}}
		log.error(audit.message(request, response))
		return HttpResponse(json.dumps(response), status=401, content_type="application/json")

	if probe_settings['collection'] == 'grades' and auth['user'] not in SECURE_settings:
		response = {'meta':{'status':{'status':'error','code':401,'desc':'Not allowed to view grades data.'}}}
		log.error(audit.message(request, response))
		return HttpResponse(json.dumps(response), status=401, content_type="application/json")

	auth_scopes = set([x for x in auth['scope']])

	if len(accepted_scopes & auth_scopes) == 0:
		response = {'meta':{'status':{'status':'error','code':401,'desc':'token not authorized for any accepted scope %s'%str(list(accepted_scopes))}}}
		log.error(audit.message(request, response))
		return HttpResponse(json.dumps(response), status=401)

	if ('dummy' in request.REQUEST.keys()):
		return HttpResponse('[]', content_type="application/json")

	is_researcher = False
	for s in auth_scopes:
		if s == 'connector_raw.all_data_researcher': is_researcher = True

	users_to_return = buildUsersToReturn(auth['user'], request, is_researcher = is_researcher)
	roles = []
	try: roles = [x.role for x in UserRole.objects.get(user=auth['user']).roles.all()]
	except: pass

	own_data = False
	if len(users_to_return) == 1 and users_to_return[0] == auth['user'].username: own_data = True
	return dataBuild(request, probe_settings, users_to_return, decrypted = decrypted, own_data = own_data, roles = roles)
コード例 #4
0
def dataBuild(request,
              probe_settings,
              users_to_return,
              decrypted=False,
              own_data=False,
              roles=[]):
    _start_time = time.time()

    results = None
    query = None
    proc_req = None
    response = {}
    response['meta'] = {}

    try:
        if len(users_to_return) == 0:
            raise BadRequestException(
                'error', 403,
                'The current token does not allow to view data from any users')
        proc_req = processApiCall(request, probe_settings, users_to_return)
        query = buildQuery(users_to_return, proc_req)
        collection = probe_settings['collection']
        if own_data and 'researcher' in roles: collection += '_researcher'

        db = database.Database()

        docs = db.getDocumentsCustom(query=query, collection=collection,\
          fields = proc_req['fields'])

        ### hinting
        # if the users are specified, we use the hint with users
        if proc_req['users'] is not None:
            docs = docs.hint([('timestamp', proc_req['order']),
                              ('facebook_id', 1), ('user', 1)])
        # else, we use only id and timestamp
        else:
            docs = docs.hint([('timestamp', proc_req['order']),
                              ('facebook_id', 1)])
        #pagination (skipping)
        if proc_req['after'] is not None:
            docs = docs.skip(1)

        #apply limit
        docs.limit(proc_req['limit'])

        try:
            results = cursorToArray(docs,
                                    decrypted=decrypted,
                                    probe=probe_settings['collection'])
        except Exception as e:
            raise BadRequestException(
                'error', 500, 'The request caused a DB malfunction: ' + str(e))
        results_count = len(results)

        response['meta']['status'] = proc_req['status']
        response['meta']['results_count'] = len(results)
        response['meta']['api_call'] = proc_req
        response['meta']['query'] = query
        response['results'] = results

        if len(results) > 0:
            response['meta']['paging'] = {}
            response['meta']['paging']['cursors'] = {}
            response['meta']['paging']['cursors']['after'] =OrderedDict([\
              (proc_req['sortby'],getValueOfFullKey(results[-1], proc_req['sortby'])),\
              ('facebook_id',results[-1]['facebook_id']),
              ('user',results[-1]['user'])])
            if results_count == proc_req['limit']:
                if proc_req['after'] is not None:
                    response['meta']['paging']['links'] =\
                     {'next':re.sub('&after=[^ &]+','&after=' + urlize_dict(response['meta']['paging']['cursors']['after']),request.build_absolute_uri())}
                else:
                    response['meta']['paging']['links'] = \
                     {'next':request.build_absolute_uri() + '&after=' + urlize_dict(response['meta']['paging']['cursors']['after'])}
    except BadRequestException as e:
        response['meta']['status'] = e.value
        proc_req = {'format': 'json'}

    response['meta']['execution_time_seconds'] = time.time() - _start_time
    callback = request.REQUEST.get('callback', '')

    if len(callback) > 0:
        data = '%s(%s);' % (callback, json.dumps(response))
        log.info(audit.message(request, response['meta']))
        return HttpResponse(data,
                            content_type="text/plain",
                            status=response['meta']['status']['code'])

    if decrypted:
        pass

    if proc_req['format'] == 'pretty':
        log.info(audit.message(request, response['meta']))
        return render_to_response('pretty_json.html',
                                  {'response': json.dumps(response, indent=2)})
    elif proc_req['format'] == 'csv':
        output = '#' + json.dumps(response['meta'], indent=2).replace(
            '\n', '\n#') + '\n'
        if probe_settings['scope'] == 'connector_raw.locationfacebook':
            output2 = ''
            output += locationfacebook_to_csv(results, output2)
        else:
            output += array_to_csv(results, probe_settings['collection'])
            log.info(audit.message(request, response['meta']))
        return HttpResponse(output,
                            content_type="text/plain",
                            status=response['meta']['status']['code'])
    else:
        log.info(audit.message(request, response['meta']))
        return HttpResponse(json.dumps(response),
                            content_type="application/json",
                            status=response['meta']['status']['code'])
    return HttpResponse('hello decrypted')
コード例 #5
0
def bluetoothBuild(request,
                   users_to_return,
                   decrypted=False,
                   own_data=False,
                   roles=[]):
    _start_time = time.time()

    pretty = booleanize(request.REQUEST.get('pretty', False))
    results = None
    query = None
    proc_req = None
    response = {}
    response['meta'] = {}

    try:
        if len(users_to_return) == 0:
            raise BadRequestException(
                'error', 403,
                'The current token does not allow to view data from any users')
        proc_req = processApiCall(request, users_to_return)
        query = buildQuery(users_to_return, proc_req)
        collection = 'edu_mit_media_funf_probe_builtin_BluetoothProbe'
        if own_data and 'researcher' in roles: collection += '_researcher'

        db = database.Database()

        docs = db.getDocumentsCustom(query=query, collection=collection,\
          fields = proc_req['fields'])

        ### hinting
        # if the users are specified, we use the hint with users
        if proc_req['users'] is not None:
            docs = docs.hint([('data.TIMESTAMP', proc_req['order']),
                              ('_id', 1), ('user', 1)])
        # else, we use only id and timestamp
        else:
            docs = docs.hint([('data.TIMESTAMP', proc_req['order']),
                              ('_id', 1)])
        #pagination (skipping)
        if proc_req['after'] is not None:
            docs = docs.skip(1)

        #apply limit
        docs.limit(proc_req['limit'])

        try:
            results = cursorToArray(docs)
        except Exception as e:
            raise BadRequestException(
                'error', 500, 'The request caused a DB malfunction: ' + str(e))
        results_count = len(results)

        response['meta']['status'] = proc_req['status']
        response['meta']['results_count'] = len(results)
        response['meta']['api_call'] = request.REQUEST
        response['meta']['query'] = query
        response['results'] = results

        if len(results) > 0:
            response['meta']['paging'] = {}
            response['meta']['paging']['cursors'] = {}
            response['meta']['paging']['cursors']['after'] =\
              {proc_req['sortby']:getValueOfFullKey(results[-1], proc_req['sortby']),\
              '_id':results[-1]['_id'],
              'user':results[-1]['user']}
            if results_count == proc_req['limit']:
                if proc_req['after'] is not None:
                    response['meta']['paging']['links'] =\
                     {'next':re.sub('&after=[^ &]+','&after=' + urlize_dict(response['meta']['paging']['cursors']['after']),request.build_absolute_uri())}
                else:
                    response['meta']['paging']['links'] = \
                     {'next':request.build_absolute_uri() + '&after=' + urlize_dict(response['meta']['paging']['cursors']['after'])}
    except BadRequestException as e:
        response['meta']['status'] = e.value

    response['meta']['execution_time_seconds'] = time.time() - _start_time
    callback = request.REQUEST.get('callback', '')

    if len(callback) > 0:
        data = '%s(%s);' % (callback, json.dumps(response))
        return HttpResponse(data,
                            content_type="text/javascript",
                            status=response['meta']['status']['code'])

    if decrypted:
        pass

    if pretty:
        log.info(audit.message(request, response['meta']['api_call']))
        return render_to_response('pretty_json.html',
                                  {'response': json.dumps(response, indent=2)})
    else:
        log.info(audit.message(request, response['meta']['api_call']))
        return HttpResponse(json.dumps(response),
                            content_type="application/json",
                            status=response['meta']['status']['code'])
    return HttpResponse('hello decrypted')
def dataBuild(request, probe_settings, users_to_return, decrypted = False, own_data = False, roles = []):
	_start_time = time.time()
	
	results = None
	query = None
	proc_req = None
	response = {}
	response['meta'] = {}

	try:
		if len(users_to_return) == 0:
			raise BadRequestException('error',403,'The current token does not allow to view data from any users')
		proc_req = processApiCall(request, probe_settings, users_to_return)
		query = buildQuery(users_to_return, proc_req)	
		collection = probe_settings['collection']
		if own_data and 'researcher' in roles: collection += '_researcher'

		db = database.Database()
		
		docs = db.getDocumentsCustom(query=query, collection=collection,\
				fields = proc_req['fields'])

		### hinting
		# if the users are specified, we use the hint with users
		if proc_req['users'] is not None:
			docs = docs.hint([('timestamp',proc_req['order']), ('facebook_id',1), ('user',1)])
		# else, we use only id and timestamp
		else:
			docs = docs.hint([('timestamp',proc_req['order']), ('facebook_id',1)])
		#pagination (skipping)
		if proc_req['after'] is not None:
			docs = docs.skip(1)

		#apply limit
		docs.limit(proc_req['limit'])

		try:
			results = cursorToArray(docs, decrypted = decrypted, probe=probe_settings['collection'])
		except Exception as e:
			raise BadRequestException('error',500,'The request caused a DB malfunction: ' + str(e))
		results_count = len(results)

		response['meta']['status'] = proc_req['status']
		response['meta']['results_count'] = len(results)
		response['meta']['api_call'] = proc_req 
		response['meta']['query'] = query
		response['results'] = results

		if len(results) > 0:
			response['meta']['paging'] = {}
			response['meta']['paging']['cursors'] = {}
			response['meta']['paging']['cursors']['after'] =OrderedDict([\
					(proc_req['sortby'],getValueOfFullKey(results[-1], proc_req['sortby'])),\
					('facebook_id',results[-1]['facebook_id']),
					('user',results[-1]['user'])])
			if results_count == proc_req['limit']:
				if proc_req['after'] is not None:	
					response['meta']['paging']['links'] =\
						{'next':re.sub('&after=[^ &]+','&after=' + urlize_dict(response['meta']['paging']['cursors']['after']),request.build_absolute_uri())}
				else:
					response['meta']['paging']['links'] = \
						{'next':request.build_absolute_uri() + '&after=' + urlize_dict(response['meta']['paging']['cursors']['after'])}
	except BadRequestException as e:
		response['meta']['status'] = e.value
		proc_req = {'format':'json'}
	
	response['meta']['execution_time_seconds'] = time.time()-_start_time
	callback = request.REQUEST.get('callback','')

	if len(callback) > 0:
		data = '%s(%s);' % (callback, json.dumps(response))
		log.info(audit.message(request, response['meta']))
		return HttpResponse(data, content_type="text/plain", status=response['meta']['status']['code'])

	if decrypted:
		pass
	
	if proc_req['format'] == 'pretty':
		log.info(audit.message(request, response['meta']))
		return render_to_response('pretty_json.html', {'response': json.dumps(response, indent=2)})
        elif proc_req['format'] == 'csv':
		output = '#' + json.dumps(response['meta'], indent=2).replace('\n','\n#') + '\n'
		if probe_settings['scope']=='connector_raw.locationfacebook':
			output2 = ''
			output += locationfacebook_to_csv(results,output2)
		else:
			output += array_to_csv(results,probe_settings['collection'])
			log.info(audit.message(request, response['meta']))
		return HttpResponse(output, content_type="text/plain", status=response['meta']['status']['code'])
	else:
		log.info(audit.message(request, response['meta']))
		return HttpResponse(json.dumps(response), content_type="application/json", status=response['meta']['status']['code'])
	return HttpResponse('hello decrypted')
def bluetoothBuild(request, users_to_return, decrypted = False, own_data = False, roles = []):
	_start_time = time.time()
	
	pretty = booleanize(request.REQUEST.get('pretty', False))
	results = None
	query = None
	proc_req = None
	response = {}
	response['meta'] = {}

	try:
		if len(users_to_return) == 0:
			raise BadRequestException('error',403,'The current token does not allow to view data from any users')
		proc_req = processApiCall(request, users_to_return)
		query = buildQuery(users_to_return, proc_req)	
		collection = 'edu_mit_media_funf_probe_builtin_BluetoothProbe'
		if own_data and 'researcher' in roles: collection += '_researcher'

		db = database.Database()
		
		docs = db.getDocumentsCustom(query=query, collection=collection,\
				fields = proc_req['fields'])

		### hinting
		# if the users are specified, we use the hint with users
		if proc_req['users'] is not None:
			docs = docs.hint([('data.TIMESTAMP',proc_req['order']), ('_id',1), ('user',1)])
		# else, we use only id and timestamp
		else:
			docs = docs.hint([('data.TIMESTAMP',proc_req['order']), ('_id',1)])
		#pagination (skipping)
		if proc_req['after'] is not None:
			docs = docs.skip(1)

		#apply limit
		docs.limit(proc_req['limit'])

		try:
			results = cursorToArray(docs)
		except Exception as e:
			raise BadRequestException('error',500,'The request caused a DB malfunction: ' + str(e))
		results_count = len(results)

		response['meta']['status'] = proc_req['status']
		response['meta']['results_count'] = len(results)
		response['meta']['api_call'] = request.REQUEST
		response['meta']['query'] = query
		response['results'] = results

		if len(results) > 0:
			response['meta']['paging'] = {}
			response['meta']['paging']['cursors'] = {}
			response['meta']['paging']['cursors']['after'] =\
					{proc_req['sortby']:getValueOfFullKey(results[-1], proc_req['sortby']),\
					'_id':results[-1]['_id'],
					'user':results[-1]['user']}
			if results_count == proc_req['limit']:
				if proc_req['after'] is not None:	
					response['meta']['paging']['links'] =\
						{'next':re.sub('&after=[^ &]+','&after=' + urlize_dict(response['meta']['paging']['cursors']['after']),request.build_absolute_uri())}
				else:
					response['meta']['paging']['links'] = \
						{'next':request.build_absolute_uri() + '&after=' + urlize_dict(response['meta']['paging']['cursors']['after'])}
	except BadRequestException as e:
		response['meta']['status'] = e.value
	
	response['meta']['execution_time_seconds'] = time.time()-_start_time
	callback = request.REQUEST.get('callback','')

	if len(callback) > 0:
		data = '%s(%s);' % (callback, json.dumps(response))
		return HttpResponse(data, content_type="text/javascript", status=response['meta']['status']['code'])

	if decrypted:
		pass

	if pretty:
		log.info(audit.message(request, response['meta']['api_call']))
		return render_to_response('pretty_json.html', {'response': json.dumps(response, indent=2)})
	else:
		log.info(audit.message(request, response['meta']['api_call']))
		return HttpResponse(json.dumps(response), content_type="application/json", status=response['meta']['status']['code'])
	return HttpResponse('hello decrypted')
コード例 #8
0
def dataBuild(request, probe_settings, users_to_return, questions_to_return, decrypted = False, own_data = False, roles = []):
	_start_time = time.time()
	
	results = None
	query = None
	proc_req = None
	response = {}
	response['meta'] = {}

	try:
		if len(users_to_return) == 0:
			raise BadRequestException('error',403,'The current token does not allow to view data from any users')
		proc_req = processApiCall(request, probe_settings, users_to_return, questions_to_return)
		query = buildQuery(users_to_return,questions_to_return, proc_req)	
		collection = probe_settings['collection']
		if own_data and 'researcher' in roles: collection += '_researcher'

		db = database.Database()
		
		docs = db.getDocumentsCustom(query=query, collection=collection,\
				fields = proc_req['fields'])

		### hinting
		hint = [('form_version',1),('last_answered',proc_req['order']), ('variable_name',1), ('user',1)]
		docs.hint(hint)
		

		#pagination (skipping)
		if proc_req['after'] is not None:
			docs = docs.skip(1)

		#apply limit
		docs.limit(proc_req['limit'])

		try:
			results = cursorToArray(docs, decrypted = decrypted, probe=probe_settings['collection'])
		except Exception as e:
			raise BadRequestException('error',500,'The request caused a DB malfunction: ' + str(e) + '. Used hint: ' + str(hint))
		results_count = len(results)

		response['meta']['status'] = proc_req['status']
		response['meta']['results_count'] = len(results)
		response['meta']['api_call'] = proc_req 
		response['meta']['query'] = query
		response['results'] = results

		if len(results) > 0:
			response['meta']['paging'] = {}
			response['meta']['paging']['cursors'] = {}
			response['meta']['paging']['cursors']['after'] =OrderedDict([\
					('form_version',results[-1]['form_version']),('last_answered', results[-1]['last_answered']),\
					('variable_name',results[-1]['variable_name']),
					('user',results[-1]['user'])])
			if results_count == proc_req['limit']:
				if proc_req['after'] is not None:	
					response['meta']['paging']['links'] =\
						{'next':re.sub('&after=[^ &]+','&after=' + urlize_dict(response['meta']['paging']['cursors']['after']),request.build_absolute_uri())}
				else:
					response['meta']['paging']['links'] = \
						{'next':request.build_absolute_uri() + '&after=' + urlize_dict(response['meta']['paging']['cursors']['after'])}
	except BadRequestException as e:
		response['meta']['status'] = e.value
		proc_req = {'format':'json'}
	response['meta']['execution_time_seconds'] = time.time()-_start_time
	callback = request.REQUEST.get('callback','')

	if len(callback) > 0:
		data = '%s(%s);' % (callback, json.dumps(response))
		log.info(audit.message(request, response['meta']['api_call']))
		return HttpResponse(data, content_type="text/plain", status=response['meta']['status']['code'])

	if decrypted:
		pass
	
	#auditdb= audit.Audit()
	#doc_audit=response['meta']
	#users_return=[]
	#users_results = cursorToArray(results, decrypted = decrypted, probe=probe_settings['collection'])
	#for data_users in users_results:
	#	if data_users['user'] not in users_return:
	#		users_return.append(data_users['user'])
	#doc_audit['users']=users_return
	#doc_audit=transform.transform(doc_audit)
	#auditdb.d(typ='prueba',tag='prueba2',doc=doc_audit,onlyfile=False)
	if proc_req['format'] == 'pretty':
		log.info(audit.message(request, response['meta']['api_call']))
		return render_to_response('pretty_json.html', {'response': json.dumps(response, indent=2)})
        elif proc_req['format'] == 'csv':
		output = '#' + json.dumps(response['meta'], indent=2).replace('\n','\n#') + '\n'
		output += array_to_csv(results,probe_settings['collection'])
		log.info(audit.message(request, response['meta']['api_call']))
		return HttpResponse(output, content_type="text/plain", status=response['meta']['status']['code'])
	else:
		log.info(audit.message(request, response['meta']['api_call']))
		return HttpResponse(json.dumps(response), content_type="application/json", status=response['meta']['status']['code'])
	return HttpResponse('hello decrypted')