# Path: pygowave_server/utils.py
# def find_random_id(manager, length, suffix="", prefix=""):
# 	"""
# 	Generates a random id for obj, checks if it exists and retries in
# 	case of collision. Returns the found number.
# 	
# 	"""
# 	rnd_id = prefix + gen_random_id(length) + suffix
# 	while manager.filter(id=rnd_id).count() > 0:
# 		rnd_id = prefix + gen_random_id(length) + suffix
# 	return rnd_id
# 
# def gen_random_id(length):
# 	"""
# 	Generate a random string with the given length.
# 	Characters are taken from RANDOM_ID_BASE.
# 	
# 	"""
# 	return "".join([random.choice(RANDOM_ID_BASE) for x in xrange(length)])
# 
# def datetime2milliseconds(dt):
# 	"""
# 	Convert a python datetime instance to milliseconds since the epoc.
# 	
# 	"""
# 	return int(time.mktime(dt.timetuple())) * 1000 + dt.microsecond / 1000


from datetime import datetime, timedelta
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist
from django.utils.hashcompat import sha_constructor as sha1
from django.utils import simplejson
from pygowave_server.utils import find_random_id, gen_random_id, datetime2milliseconds
from pygowave_server.common.operations import *
import uuid
	
	id = models.CharField(max_length=42, primary_key=True)
	wave = models.ForeignKey(Wave, related_name="wavelets")
	creator = models.ForeignKey(Participant, related_name="created_wavelets")
	is_root = models.BooleanField()
	root_blip = models.OneToOneField("Blip", related_name="rootblip_wavelet", null=True)
	created = models.DateTimeField(auto_now_add=True)
	last_modified = models.DateTimeField(auto_now=True)
	title = models.CharField(max_length=255, blank=True)
	version = models.IntegerField(default=0)
	participants = models.ManyToManyField(Participant, related_name="wavelets")
	participant_conns = models.ManyToManyField(ParticipantConn, blank=True, related_name="wavelets", verbose_name=_(u'connections'))
	
	def blipById(self, id):
		"""
		Returns the Blip object with the given id, if the Blip resides on this
		Wavelet. Returns None otherwise.
		
		"""
		
		try:
			return self.blips.get(id=id)
		except ObjectDoesNotExist:
			return None
	
	def save(self, force_insert=False, force_update=False):
		if not self.id:
			if self.is_root:
				self.id = self.wave.id + ROOT_WAVELET_ID_SUFFIX
			else:

# Path: pygowave_server/utils.py
# def find_random_id(manager, length, suffix="", prefix=""):
# 	"""
# 	Generates a random id for obj, checks if it exists and retries in
# 	case of collision. Returns the found number.
# 	
# 	"""
# 	rnd_id = prefix + gen_random_id(length) + suffix
# 	while manager.filter(id=rnd_id).count() > 0:
# 		rnd_id = prefix + gen_random_id(length) + suffix
# 	return rnd_id
# 
# def gen_random_id(length):
# 	"""
# 	Generate a random string with the given length.
# 	Characters are taken from RANDOM_ID_BASE.
# 	
# 	"""
# 	return "".join([random.choice(RANDOM_ID_BASE) for x in xrange(length)])
# 
# def datetime2milliseconds(dt):
# 	"""
# 	Convert a python datetime instance to milliseconds since the epoc.
# 	
# 	"""
# 	return int(time.mktime(dt.timetuple())) * 1000 + dt.microsecond / 1000


# Path: pygowave_server/models.py
from datetime import datetime, timedelta
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist
from django.utils.hashcompat import sha_constructor as sha1
from django.utils import simplejson
from pygowave_server.utils import find_random_id, gen_random_id, datetime2milliseconds
from pygowave_server.common.operations import *
import uuid

	
	id = models.CharField(max_length=42, primary_key=True)
	wave = models.ForeignKey(Wave, related_name="wavelets")
	creator = models.ForeignKey(Participant, related_name="created_wavelets")
	is_root = models.BooleanField()
	root_blip = models.OneToOneField("Blip", related_name="rootblip_wavelet", null=True)
	created = models.DateTimeField(auto_now_add=True)
	last_modified = models.DateTimeField(auto_now=True)
	title = models.CharField(max_length=255, blank=True)
	version = models.IntegerField(default=0)
	participants = models.ManyToManyField(Participant, related_name="wavelets")
	participant_conns = models.ManyToManyField(ParticipantConn, blank=True, related_name="wavelets", verbose_name=_(u'connections'))
	
	def blipById(self, id):
		"""
		Returns the Blip object with the given id, if the Blip resides on this
		Wavelet. Returns None otherwise.
		
		"""
		
		try:
			return self.blips.get(id=id)
		except ObjectDoesNotExist:
			return None
	
	def save(self, force_insert=False, force_update=False):
		if not self.id:
			if self.is_root:
				self.id = self.wave.id + ROOT_WAVELET_ID_SUFFIX
			else:

				self.id = find_random_id(Wavelet.objects, 10, prefix=self.wave.id+"!")