Beispiel #1
0
Datei: api.py Projekt: spsu/sylph
def get_profile(request):
	"""
	Get up-to-date information on the owner of this node.
	In the future this will respect privacy issues. There may also be
	more data than the simple profile serialized into this.
	"""
	try:
		user = User.objects.get(pk=settings.OUR_USER_PK)
	except User.DoesNotExist:
		raise Exception, "Critical error: no user!" # XXX: Core system error!

	# XXX/TODO/FIXME: Privacy issues!!

	# TODO: Email, etc.

	rs = RdfSerializer(user)
	return HttpResponse(rs.to_rdf(), mimetype='text/plain')
Beispiel #2
0
Datei: api.py Projekt: spsu/sylph
def get_by_node(request):
	"""
	Get information on a user given their node resource URI.
	See: get(resource).
	"""
	if 'uri' not in request.POST:
		raise Exception, "No URI in post."

	uri = hashless(request.POST['uri'])

	try:
		user = User.objects.get(node__uri=uri)
	except User.DoesNotExist:
		raise Exception, "User does not exist."

	# XXX/TODO/FIXME: Privacy issues!!
	rs = RdfSerializer(user)
	return HttpResponse(rs.to_rdf(), mimetype='text/plain')
Beispiel #3
0
Datei: api.py Projekt: spsu/sylph
def get(request):
	"""
	Get information on a user given their user resource URI.
	This isn't authoratative, since we're not looking up the
	originating source.
	"""
	if 'uri' not in request.POST:
		raise Exception, "No URI in post."

	uri = hashless(request.POST['uri'])
	try:
		user = User.objects.get(uri=uri)
	except User.DoesNotExist:
		raise Exception, "User does not exist."

	# XXX/TODO/FIXME: Privacy issues!!
	rs = RdfSerializer(user)
	return HttpResponse(rs.to_rdf(), mimetype='text/plain')
Beispiel #4
0
Datei: api.py Projekt: spsu/sylph
def ping_response(request):
	"""
	Respond to a 'ping request' from another node.
	This returns node information and (optionally) some user
	information.
	"""
	print "ping_response" # TODO DEBUG
	node = None
	user = None
	try:
		node = Node.objects.get(pk=settings.OUR_NODE_PK)
		#user = User.objects.get(pk=settings.OUR_USER_PK) # XXX: Temporary.
	except Model.DoesNotExist:
		raise Exception # XXX: This is a critcal system error!

	rs = RdfSerializer(node)
	#rs.add(user) # TODO: Only share minimal info per user's preferences

	return HttpResponse(rs.to_rdf(), mimetype='text/plain')
Beispiel #5
0
	def __add_payload_to_headers(self):
		"""Add the object payload to the message. This performs the
		serialization work."""
		if not self.payload:
			return

		serialize_format = None # TODO: Convert to serializer method
		if not self.serializer:
			self.serializer = RdfSerializer()
			serialize_format = 'RDF/XML'

		# We serialize only when about to be sent...
		for obj in self.payload:
			self.serializer.add(obj)

		# TODO: should be serializer.serialize() !!
		self.add_post_var('data', self.serializer.to_rdf())
		self.add_post_var('format', serialize_format)
Beispiel #6
0
class SylphMessage(Message):
	"""
	Message is a content store relayed between two endpoints.

	Sylph messages are an extension of the lower-level HTTP Message
	store. They add the ability to store Models or QuerySets as well as
	determine what the type of the sender or receiver is based on
	analysis of the headers or content.
	"""

	def __init__(self, uri=''): # TODO: Fix/standardize args
		super(SylphMessage, self).__init__(uri)

		# High-level serialized and parsed info (respectively)
		self.payload = []
		self.extracted = None # TODO

		# Serializer/Parser objects.
		self.serializer = None
		self.parser = None

	# ============= Add/Get Payloads ======================

	def add(self, transportable):
		"""Add a model object or QuerySet to the payload in preparation
		to be sent to the remote node."""
		self.payload.append(transportable)

	def extract(self, type):
		"""Extract all data of a known type. (eg. Node, User, etc.)"""
		# XXX: This is not a nice interface / good practice.
		# TODO: See self.extracted wrt. self.payload. Make them analogous for
		# the two modalities of transfer. (response->extracted)
		if not self.body:
			return None # Nothing to extract
		if not self.parser:
			# XXX/TODO: Parser shouldn't contain content!!! BAD! REUSE IS GOOD!
			self.parser = RdfParser(self.body)
		return self.parser.extract(type) # XXX: BAD

	# XXX/TODO: If something new is added to payload, **mark headers stale** instead
	def _sync(self):
		if self.payload:
			self.__add_payload_to_headers()
		elif self.body:
			pass # TODO: Try to parse out all content to self.extracted!

	def _unsync(self):
		if self.payload:
			self.__remove_payload_from_headers()
		elif self.body:
			pass # TODO: Read above todo (remove from self.extracted?)

	def __add_payload_to_headers(self):
		"""Add the object payload to the message. This performs the
		serialization work."""
		if not self.payload:
			return

		serialize_format = None # TODO: Convert to serializer method
		if not self.serializer:
			self.serializer = RdfSerializer()
			serialize_format = 'RDF/XML'

		# We serialize only when about to be sent...
		for obj in self.payload:
			self.serializer.add(obj)

		# TODO: should be serializer.serialize() !!
		self.add_post_var('data', self.serializer.to_rdf())
		self.add_post_var('format', serialize_format)

		# TODO: Remove data from the serializer to keep state pure
		# self.serializer.flush()

	def __remove_payload_from_headers(self):
		"""Remove the payload from the message."""
		del self.post['data']
		del self.post['format']

	# TODO: Set an alternate serializer (eg. JSON)
	#def set_serializer(self, serializer):
	#	"""Can take string name or object, but will flush the 
	#	serialier of any existing data."""
	#	pass

	# ============= Content/Node Type Analysis ============

	def is_sylph_node(self):
		"""Returns whether the sender/receiver is a sylph node."""
		if 'X-Sylph-Protocol-Version' in self.headers:
			return True
		return False

	def is_feed(self): # TODO
		"""Returns whether the content is an RSS/Atom feed."""
		return None

	def is_webpage(self): # TODO
		"""Returns whether the content is a webpage."""
		# This is going to be imperfect...
		# First check the headers 
		mimetypes = ['text/html', 'application/xhtml+xml']
		if 'Content-Type' in self.headers and \
			self.headers['Content-Type'] in mimetypes:
				return True
		# This is a really poor heuristic...
		if len(self.body) > 100 and "<html" in self.body[0:100]:
			return True
		return False

	def get_node_type(self): # TODO: Depends on above.
		"""Return the type of node the page is."""
		if self.is_sylph_node():
			return 'sylph'
		elif self.is_feed():
			return 'feed'
		elif self.is_webpage():
			return 'webpage'
		return 'unknown'

	def get_protocol_version(self): # TODO
		"""Gets from headers."""
		pass

	def get_software_name(self): # TODO
		"""Gets from headers."""
		pass

	def get_software_version(self): # TODO
		"""Gets from headers."""
		pass