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)
Exemple #2
0
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)