Example #1
0
File: api.py Project: spsu/sylph
def push_profile(request):
	"""Handle a profile that has been pushed to us."""
	try:
		udata = request.extract('User')[0]
		uri = udata['uri']
		node_uri = udata['node']
	except:
		raise Exception, "Improper payload."

	# These are handled specially
	del udata['uri']
	del udata['node']

	try:
		user = User.objects.get(uri=uri)
	except User.DoesNotExist:
		user = User(uri=uri)

	try:
		node = Node.objects.get(uri=node_uri)
	except Node.DoesNotExist:
		node = Node(uri=node_uri)

	# XXX/TODO: There's no security in this...
	for k, v in udata.iteritems():
		setattr(user, k, v)

	user.node = node
	user.save()

	return HttpResponse('ACK')
Example #2
0
File: tasks.py Project: spsu/sylph
def do_add_node_lookup(uri):
	"""
	Nodes will get added all the time and in various contexts.
	This is the task that must run for each of them.

	Query every single added node -- this MUST occur.
	Remember, adding a node doesn't require permission. You can
	navigate to a URI without permission (though you may be blocked)

	The response may provide some additional details:

		* Person (with varying amount of data)
		* etc.

			> Add them
			> Maybe with an additional footnote? (TODO)

	after done, set is_remaining_to_query or whatever = False

	Pinging a node does not require us to give identity unless the
	remote node requests it.
	"""
	node = None
	try:
		node = Node.objects.get(uri=uri)
	except Node.DoesNotExist:
		# TODO: ERROR LOG FILE
		print "cannot do_add_node_lookup: Node does not exist!!!"
		return

	if node.node_class in ['webpage', 'service', 'feed']:
		print "cannot do_add_node_lookup: non-sylph or unknown nodes"
		return

	if node.node_class_guess in ['webpage', 'service', 'feed']:
		print "(TEMP) cannot do_add_node_lookup: non-sylph"
		return

	user = None
	try:
		user = User.objects.get(node=node)
	except User.DoesNotExist:
		user = User()

	# Perform communications
	message = SylphMessage(uri)
	message.set_post('dispatch', 'ping')
	response = send(message)

	if response.has_errors():
		print "No communication return data!!" # TODO: Error log
		node.just_failed(save=True)
		return

	parser = None
	node_data = None

	try:
		node_data = response.extract('Node')[0]
	except Exception:
		print "Error parsing payload" # TODO: Error log
		node.just_failed(save=True)
		return

	user_data = None
	try:
		user_data = response.extract('User')[0]
	except Exception:
		print "1. No user data, or error. Ignoring."

	# Update the node's status
	node.is_yet_to_resolve = False
	node.datetime_last_resolved = datetime.today()
	node.status = 'AVAIL'
	node.protocol_version = node_data['protocol_version']
	node.software_name = node_data['software_name']
	node.software_version = node_data['software_version']
	node.node_class = 'sylph'
	node.node_class_guess = ''
	node.name = node_data['name']
	node.description = node_data['description']
	#node.datetime_edited = node_data['datetime_edited'] # TODO
	node.save()

	# Update the user's status, if we have user data.
	if user_data:
		user.username = user_data['username']
		user.first_name = user_data['first_name']
		user.middle_name = user_data['middle_name']
		user.last_name = user_data['last_name']
		user.bio = user_data['bio']
		user.title = user_data['title']
		user.suffix = user_data['suffix']
		#user.datetime_created = user_data['datetime_created']
		#user.datetime_edited = user_data['datetime_edited']
		user.node = node
		user.save()

	# Now have the node add us
	message = SylphMessage(uri)
	post = {
		'dispatch': 'node_ask_to_add',
		'uri': settings.FULL_ENDPOINT_URI, # XXX XXX XXX: Very bad protocol!
	}
	message.set_post(post)
	response = send(message)

	if response.has_errors():
		print "Failed to ask node to add us!!"
		node.just_failed(save=True)

	print "CREATING SUBSCRIPTIONS:"
	create_subscriptions_to(node)
	create_subscriptions_from(node)