コード例 #1
0
def root(obj):
	url = request.url
	data = Structure(url)

	results = mongoDBQuery(obj)

	for item in results:
		
		entry = {}
		entry['href'] = ""
		entry['data'] = []
		for key, value in item.items():
			
			if(key == '_id'):
				mod_path = '/test/' + db + '/' + str(value)
				entry['href'] = getCurrentPath(url, mod_path)
				entry['data'].append(generateNameValuePair(key, str(value)))
			else:
				entry['data'].append(generateNameValuePair(key, str(value)))

		#entry['href'] = "/test/" + obj + "/" + item
		key = item 
		value = item
		data.appendItem(entry)	

	return packageResponse(data)
コード例 #2
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def showall(table):
	url = request.url
	collection = Structure(url)
	collection.setPostTemplate(generateTemplate(table))

	query = "SELECT * FROM {0}".format(table)
	column_query = "SHOW COLUMNS FROM {0}".format(table)
	rows = runSQLQuery(query, 0)
	column_flags = runSQLQuery(column_query, 0)

	for i in rows:
		item = {}
		unique_ref = str(i[0])
		mod_path = '/table/showone/' + table + "/" + unique_ref
		item['href'] = getCurrentPath(url, mod_path)
		data = []

		counter=0
		for x in column_flags:
			data.append(generateNameValuePair(x[0], i[counter]))
			counter=counter+1

		item['data'] = data
		collection.appendItem(item)

	return packageResponse(collection)
コード例 #3
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def showDatabases():
	url = request.url
	collection = Structure(url)
	query = "SHOW DATABASES"
	data = runSQLQuery(query, 0)
	for x in data:
		collection.appendItem(generateNameValuePair('database', x[0]))

	return packageResponse(collection)
コード例 #4
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def showallByColumn(table, column):
	url = request.url
	collection = Structure(url)
	collection.setPostTemplate(generateTemplate(table))

	query = "SELECT {0} FROM {1}".format(column, table)
	query_result = runSQLQuery(query, 0)

	for item in query_result:
		collection.appendItem({'name': column, 'value': item[0]})

	return packageResponse(collection)
コード例 #5
0
def getTableList():
	url = request.url
	collection = Structure(url)
	results = mongoGetCollections()

	for i in results:
		item = {}
		mod_path = '/table/post/' + i
		item['href'] = getCurrentPath(url, mod_path)
		item['data'] = generateNameValuePair('collection', i)
		collection.appendItem(item)

	return packageResponse(collection)
コード例 #6
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def getTableList():

	url = request.url
	collection = Structure(url)
	query_results = getTables()

	for i in query_results:
		item = {}
		mod_path = '/table/post/' + i[0]
		item['href'] = getCurrentPath(url, mod_path)
		item['data'] = []
		item['data'].append(generateNameValuePair('table', i[0]))
		collection.appendItem(item)

	return packageResponse(collection)
コード例 #7
0
def showall(input_collection):
	url = request.url
	collection = Structure(url)
	documents = mongoFindAll(input_collection)

	collection_exists = False
	data_exists = False

	for element in documents:
		collection_exists = True
		item = {}
		data = []
		for key, value in element.items():
			if('id' in key):
				mod_path = '/table/showone/' + input_collection + '/' + str(value)
				item['href'] = getCurrentPath(url, mod_path)
				data.append(generateNameValuePair('id', str(value)))
			
			elif(type(value) is list):
				list_values = ', '.join(map(str, value))
				data.append(generateNameValuePair(key, list_values))
			else:
				data.append(generateNameValuePair(key, value))

		item['data'] = data
		collection.appendItem(item)

	if(collection_exists == False):
		collection.setError(getError(-1, "Collection does not exist!"))
	else:
		collection.setPostTemplate(returnTemplateFromData(input_collection))
	        
	return packageResponse(collection)
コード例 #8
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def error(e):
	url = request.url
	collection = Structure(url)
	collection.appendLinks(describeAPI(url))

	if (e.code == 404):
		collection.setError(getHTTPError(404, request))
	elif(e.code == 405):
		collection.setError(getHTTPError(405, request))
	else:
		collection.setError(getHTTPError(5, e))
		return packageResponse(collection)

	collection.setError(getError(6, e))
	return packageResponse(collection)
コード例 #9
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def showone(table, id):
	url = request.url
	collection = Structure(url)
	column_query = "SHOW COLUMNS FROM {0}".format(table)
	columns = runSQLQuery(column_query, 0)

	query = "SELECT * FROM {0} WHERE {1} = {2}".format(table, columns[0][0], id)
	
	rows = runSQLQuery(query, 0)
	item = {}
	data = []
	mod_path = '/table/showone/' + id
	item['href'] = getCurrentPath(url, mod_path)
	#row_item={}
	row_item_data = []
	counter=0

	if len(rows) == 0:
		collection.setError(getError(3, ""))
	else:
		for x in columns:
			data_item = generateNameValuePair(x[0], rows[0][counter])
			row_item_data.append(data_item)
			counter=counter+1

		link = getCurrentPath(url, mod_path)
		collection.appendLink(generateLink(link, 'showone'))
		link = getCurrentPath(url, '/table/showall/' + table)
		collection.appendLink(generateLink(link, 'showall'))
		link = getCurrentPath(url, '/table/post/' + table)
		collection.appendLink(generateLink(link, 'post'))

		item['data'] = row_item_data
		collection.appendItem(item)

	collection.setPostTemplate(generateTemplate(table))
	
	return packageResponse(collection)
コード例 #10
0
ファイル: app.py プロジェクト: kmjbyrne/Flask-REST-API-MySQL
def tableRoute(table):
	"""table/structure & table/post merged into one method"""
	url = request.url
	collection = Structure(url)

	if(request.method == 'GET'):
		dbs = [table]	
		collection.setItems(describeTables(url, dbs))
		link = getCurrentPath(url, '/table/post/' + table)

		collection.appendLink(generateLink(link, 'post'))
		collection.setPostTemplate(generateTemplate(table))

		return packageResponse(collection)

	elif(request.method == 'POST'):
		try:
			dict_data = None
			try:
				data = json.dumps(request.get_json())
				dict_data = json.loads(data)
				if(dict_data == None):
					raise Exception('Exception raised - JSON data package is None')

			except Exception as e:
				collection.setError(getError(1, e))
				collection.setPostTemplate(generateTemplate(table))
				return packageResponse(collection)

			columns = ""
			counter = 0
			for col in dict_data['template']['data']:
				if(counter == 0):
					columns += (col['name'])
				else:
					columns += (", " + col['name'] + "")
				counter = counter + 1

			query = ['INSERT ', 'INTO ', table, '(', columns, ')', ' values ', '(']

			inputs = 0
			for item in dict_data['template']['data']:
				if(item['name'] != 'id'):
					if(inputs == 0):
						query.append(appendByType(item))

					else:
						query.append(", " + appendByType(item))

					inputs = inputs + 1

			query.append(')')
			query = ''.join(query)
			status = ""
			try:
				status = runSQLQuery(query, 1)
			except Exception as e:
				collection.setError(getError(2, status['msg']))
				return packageResponse(collection)

			if(status['code'] != False):
				link = getCurrentPath(url, "/table/showall/" + table)
				collection.appendLink(generateLink(link, 'showall'))
				link = getCurrentPath(url, "/table/showone/" + table + "/" + str(status['msg']))
				collection.appendLink(generateLink(link, 'showone'))
				return packageResponse(collection)

			else:
				collection.setError(getError(2, str(status['msg'])))
				return packageResponse(collection)

		except Exception as e:
			collection.setError(getError(-1, e))
			collection.setPostTemplate(generateTemplate(table))
			return packageResponse(collection)
コード例 #11
0
def showone(input_collection, id):
	url = request.url
	collection = Structure(url)
	
	# API for RDBMS code - Deprecated for ORM MongoDB

	#column_query = "SHOW COLUMNS FROM {0}".format(table)
	#columns = runSQLQuery(column_query, 0)
	#query = "SELECT * FROM {0} WHERE {1} = {2}".format(table, columns[0][0], id)
	#rows = runSQLQuery(query, 0)
	
	document = mongoFindOne(input_collection, id)

	item = {}
	mod_path = '/table/showone/' + input_collection + '/' + id
	item['href'] = getCurrentPath(url, mod_path)
	row_item_data = []
	counter=0

	if document is None:
		collection.setError(getError(3, ""))
	else:
		for element in document:
			for key, value in element.items():
				if('id' in key):
					row_item_data.append(generateNameValuePair(key, str(value)))
				else:
					row_item_data.append(generateNameValuePair(key, value))

		link = getCurrentPath(url, mod_path)
		collection.appendLink(generateLink(link, 'showone'))
		link = getCurrentPath(url, '/table/showall/' + input_collection)
		collection.appendLink(generateLink(link, 'showall'))
		link = getCurrentPath(url, '/table/post/' + input_collection)
		collection.appendLink(generateLink(link, 'post'))

		item['data'] = row_item_data
		collection.appendItem(item)

	collection.setPostTemplate(returnTemplateFromData(input_collection))
	
	return packageResponse(collection)
コード例 #12
0
def tableRoute(input_collection):
	"""table/structure & table/post merged into one method"""
	url = request.url
	collection = Structure(url)

	if(request.method == 'GET'):
		collection.setItems(describeObject(input_collection))
		link = getCurrentPath(url, '/table/post/' + input_collection)
		collection.appendLink(generateLink(link, 'post'))
		collection.setPostTemplate(returnTemplateFromData(input_collection))
		return packageResponse(collection)

	elif(request.method == 'POST'):
		try:
			collections = mongoGetCollections()
			if(input_collection not in collections):
				collection.setError(getError(-1, "Collection does not exist!"))
				return packageResponse(collection)

			data = None
			try:
				data = json.dumps(request.get_json())
				dict_data = json.loads(data)
				if(dict_data == None):
					raise Exception('Exception raised - JSON data package is None')

			except Exception as e:
				collection.setError(getError(1, e))
				collection.setPostTemplate(returnTemplateFromData(input_collection))
				return packageResponse(collection)

			#Insert to MongoDB - template data
			last_id = str(mongoInsertData(input_collection, dict_data))

			link = getCurrentPath(url, "/table/showall/" + input_collection)
			collection.appendLink(generateLink(link, 'showall'))
			link = getCurrentPath(url, "/table/showone/" + input_collection + "/" + last_id)
			collection.appendLink(generateLink(link, 'showone'))
			return packageResponse(collection)

		except Exception as e:
			collection.setError(getError(-1, e))
			#scollection.setPostTemplate(generateTemplate(input_collection))
			return packageResponse(collection)