# 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
	@transaction.commit_on_success
	def create_and_init_new_wave(self, creator, title):
		wave = self.create()
		
		wavelet = Wavelet(wave=wave, creator=creator, title=title, is_root=True)
		wavelet.save()
		wavelet.participants.add(creator)
		
		blip = Blip(wavelet=wavelet, creator=creator)
		blip.save()
		wavelet.root_blip = blip
		wavelet.save()
		return wave

class Wave(models.Model):
	"""
	Models wave instances. These are the core of Google Wave.
	A single wave is composed of its id and any wavelets that belong to it.
	
	"""
	
	objects = WaveManager()
	
	id = models.CharField(max_length=42, primary_key=True) # Don't panic :P
	
	def root_wavelet(self):
		return self.wavelets.get(is_root=True)
	
	def save(self, force_insert=False, force_update=False):
		if not self.id:

# 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

	@transaction.commit_on_success
	def create_and_init_new_wave(self, creator, title):
		wave = self.create()
		
		wavelet = Wavelet(wave=wave, creator=creator, title=title, is_root=True)
		wavelet.save()
		wavelet.participants.add(creator)
		
		blip = Blip(wavelet=wavelet, creator=creator)
		blip.save()
		wavelet.root_blip = blip
		wavelet.save()
		return wave

class Wave(models.Model):
	"""
	Models wave instances. These are the core of Google Wave.
	A single wave is composed of its id and any wavelets that belong to it.
	
	"""
	
	objects = WaveManager()
	
	id = models.CharField(max_length=42, primary_key=True) # Don't panic :P
	
	def root_wavelet(self):
		return self.wavelets.get(is_root=True)
	
	def save(self, force_insert=False, force_update=False):
		if not self.id:

			self.id = find_random_id(Wave.objects, 10)