# Path: pygowave_server/models.py
# class Participant(models.Model):
# 	"""
# 	Represents participant information. It contains id, display name, avatar's
# 	URL, and an external URL to view the participant's profile page. The
# 	instances may be coming from external Wave servers (not yet).
# 	
# 	Note: This model also serves as user profile on this server.
# 	
# 	"""
# 	
# 	objects = ParticipantManager()
# 	
# 	id = models.CharField(max_length=255, primary_key=True)
# 	user = models.ForeignKey(User, blank=True, null=True, unique=True, related_name="participants")
# 	is_bot = models.BooleanField()
# 	last_contact = models.DateTimeField(auto_now_add=True)
# 	
# 	name = models.CharField(max_length=255, blank=True)
# 	avatar = models.URLField(verify_exists=False, blank=True)
# 	profile = models.URLField(verify_exists=False, blank=True)
# 	
# 	def create_new_connection(self):
# 		"""
# 		Creates a new connection object for this participant and returns it.
# 		This generates the participant's new access keys.
# 		
# 		"""
# 		new_conn = ParticipantConn(participant=self)
# 		new_conn.save()
# 		return new_conn
# 	
# 	def serialize(self):
# 		"""
# 		Serialize participant into Google's format (taken from Gadgets API).
# 		
# 		"""
# 		return {
# 			"id": self.id,
# 			"displayName": self.name,
# 			"thumbnailUrl": self.avatar,
# 			"profileUrl": self.profile,
# 			"isBot": self.is_bot
# 		}
# 	
# 	def __unicode__(self):
# 		return u"Participant '%s'" % (self.id)


from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
from datetime import datetime
from pygowave_server.models import Participant



class UserOnlineMiddleware:
	"""
	Update the last_contact field of the user's participant object. Used to
	determine how many users are online. This also creates a participant object
	if it is missing.
	
	"""
	def process_request(self, request):
		if request.user.is_authenticated():
			try:
				profile_obj = Participant.objects.get(user__id=request.user.id)
			except ObjectDoesNotExist:

# Path: pygowave_server/models.py
# class Participant(models.Model):
# 	"""
# 	Represents participant information. It contains id, display name, avatar's
# 	URL, and an external URL to view the participant's profile page. The
# 	instances may be coming from external Wave servers (not yet).
# 	
# 	Note: This model also serves as user profile on this server.
# 	
# 	"""
# 	
# 	objects = ParticipantManager()
# 	
# 	id = models.CharField(max_length=255, primary_key=True)
# 	user = models.ForeignKey(User, blank=True, null=True, unique=True, related_name="participants")
# 	is_bot = models.BooleanField()
# 	last_contact = models.DateTimeField(auto_now_add=True)
# 	
# 	name = models.CharField(max_length=255, blank=True)
# 	avatar = models.URLField(verify_exists=False, blank=True)
# 	profile = models.URLField(verify_exists=False, blank=True)
# 	
# 	def create_new_connection(self):
# 		"""
# 		Creates a new connection object for this participant and returns it.
# 		This generates the participant's new access keys.
# 		
# 		"""
# 		new_conn = ParticipantConn(participant=self)
# 		new_conn.save()
# 		return new_conn
# 	
# 	def serialize(self):
# 		"""
# 		Serialize participant into Google's format (taken from Gadgets API).
# 		
# 		"""
# 		return {
# 			"id": self.id,
# 			"displayName": self.name,
# 			"thumbnailUrl": self.avatar,
# 			"profileUrl": self.profile,
# 			"isBot": self.is_bot
# 		}
# 	
# 	def __unicode__(self):
# 		return u"Participant '%s'" % (self.id)


# Path: pygowave_server/middleware.py
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
from datetime import datetime
from pygowave_server.models import Participant




class UserOnlineMiddleware:
	"""
	Update the last_contact field of the user's participant object. Used to
	determine how many users are online. This also creates a participant object
	if it is missing.
	
	"""
	def process_request(self, request):
		if request.user.is_authenticated():
			try:
				profile_obj = Participant.objects.get(user__id=request.user.id)
			except ObjectDoesNotExist:

				profile_obj = Participant.objects.create_from_user(request.user)