# 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
		"""
		newobj = cls(
			version=version,
			wavelet=Wavelet.objects.get(id=opman.waveletId),
			contributor=Participant.objects.get(id=opman.contributorId),
			operations=simplejson.dumps(opman.serialize())
		)
		newobj._OpManager = opman
		
		return newobj
	
	def getOpManager(self):
		"""
		Retrieve an OpManager based on this delta.
		
		"""
		opman = getattr(self, "_OpManager", None)
		if opman == None:
			opman = OpManager(self.wavelet.wave.id, self.wavelet.id, self.contributor.id)
			opman.unserialize(simplejson.loads(self.operations))
			self._OpManager = opman
		
		return opman
	
	def timestampMs(self):
		"""
		Returns the timestamp of this Delta in milliseconds since
		the epoc.
		
		"""

# 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

		"""
		newobj = cls(
			version=version,
			wavelet=Wavelet.objects.get(id=opman.waveletId),
			contributor=Participant.objects.get(id=opman.contributorId),
			operations=simplejson.dumps(opman.serialize())
		)
		newobj._OpManager = opman
		
		return newobj
	
	def getOpManager(self):
		"""
		Retrieve an OpManager based on this delta.
		
		"""
		opman = getattr(self, "_OpManager", None)
		if opman == None:
			opman = OpManager(self.wavelet.wave.id, self.wavelet.id, self.contributor.id)
			opman.unserialize(simplejson.loads(self.operations))
			self._OpManager = opman
		
		return opman
	
	def timestampMs(self):
		"""
		Returns the timestamp of this Delta in milliseconds since
		the epoc.
		
		"""

		return datetime2milliseconds(self.timestamp)