Пример #1
0
def single_author(request):
	"""
	Deals with single author graph requests
	"""

	# Get author info and cutoff from request
	if request.method == 'GET':
		author_info = request.GET.get("author")
		cutoff = int(request.GET.get("cutoff")) 

	unigraph = get_unigraph()

	# if author info is a number, use it as id and return the graph for that node
	if numerical(author_info):
		author_id = author_info
		return single_from_id(author_id, unigraph, cutoff)
	# Otherwise author info is a name, get nodes matching that name
	else:
		candidates = graph_utils.get_matching_nodes(author_info, unigraph)

	# If no matching nodes, return error message
	if len(candidates) == 0:
		errorMessage = json.dumps({"error": "Sorry, the source author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')

	# If more than one matching node, return matching node information
	if len(candidates) > 1:
		return HttpResponse(json.dumps({"candidates": candidates}), content_type='application/json')

	# Otherwise get id of single match and return graph for that node
	author_id = candidates[0]["id"]
	return single_from_id(author_id, unigraph, cutoff)
Пример #2
0
def longest_path(request):
	"""
	Deals with longest path requests
	"""
	# Get the source info from the request
	if request.method == 'GET':
		source_info = request.GET.get('source')

	unigraph = get_unigraph()

	source_id = ""
	source_candidates = []
	# If source info is a number, it is the node id so return the longest path graph for that node
	if numerical(source_info):
		source_id = source_info
		return longest_from_id(source_id, unigraph)
	# Otherwise info is a name, get nodes which match the name
	else:
		source_candidates = graph_utils.get_matching_nodes(source_info, unigraph)
		
	# If no matching nodes found, return error message
	if len(source_candidates) == 0:
		errorMessage = json.dumps({"error": "Sorry, the source author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')

	candidates = {}
	# If more than one matching node, return the matching nodes to display to user
	if len(source_candidates) > 1:
		candidates["longest_candidates"] = source_candidates
		return HttpResponse(json.dumps({"candidates": candidates}), content_type='application/json')

	# Otherwise only one matching node, return the longest path graph for this node
	source_id = source_candidates[0]["id"]
	
	return longest_from_id(source_id, unigraph)
Пример #3
0
def shortest_path(request):
	"""
	Deals with shortest path request
	"""

	# Get source and target info passed in request
	if request.method == 'GET':
		source_info = request.GET.get('source')
		target_info = request.GET.get('target')

	unigraph = get_unigraph()
	source_id = ""
	target_id = ""
	source_candidates = []
	target_candidates = []
	
	# If source info is a number, it is the node id, set source id to source info
	if numerical(source_info):
		source_id = source_info
	# Otherwise, it is a name - get nodes whose name matches the given name
	else:
		source_candidates = graph_utils.get_matching_nodes(source_info, unigraph)

	# See above
	if numerical(target_info):
		target_id = target_info
	else:
		target_candidates = graph_utils.get_matching_nodes(target_info, unigraph)

	# If both source and target id's are known, return the graph for the path between source and target
	if source_id and target_id:
		return path_graph_from_ids(source_id, target_id, unigraph)

	# Cases where info provided does not match nodes in graph (either source, target or both)
	# In these cases an error message is returned to be displayed in front end
	if len(source_candidates) == 0 and not source_id and len(target_candidates) == 0 and not target_id:
		errorMessage = json.dumps({"error": "Sorry, neither author was found"})
		return HttpResponse(errorMessage, content_type='application/json')
	elif len(source_candidates) == 0 and not source_id:
		errorMessage = json.dumps({"error": "Sorry, the source author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')
	elif len(target_candidates) == 0 and not target_id:
		errorMessage = json.dumps({"error": "Sorry, the target author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')

	# If both source and target have matching nodes
	candidates = {}
	# If there is more than one matching node for source or target, add the candidates to the candidate nodes dict
	# Candidates can then be displayed in front end
	if len(source_candidates) > 1:
		candidates["source_candidates"] = source_candidates
	if len(target_candidates) > 1:
		candidates["target_candidates"] = target_candidates

	# If candidates not empty it means there is more than one candidate for either source or target,
	# return this information to the front end
	if candidates:
		return HttpResponse(json.dumps({"candidates": candidates}), content_type='application/json')

	# Otherwise, there is only one candidate for both source and target, so get their ids
	if not source_id:
		source_id = source_candidates[0]["id"]
	if not target_id:
		target_id = target_candidates[0]["id"]

	# Return the graph for the path between the source and target
	return path_graph_from_ids(source_id, target_id, unigraph)