示例#1
0
	def fromPython( self, pythonValue, dereferences=[], modifier=None ):
		self._getClassInfo( )
		
		if pythonValue is None:
			return None
		
		if isinstance(pythonValue, dbref.DBRef):
			return {
				'_ref': pythonValue
			}
		elif not isinstance(pythonValue, self.documentClass):
			# try mapping to an objectid
			try:
				objectId = objectid.ObjectId( str( pythonValue ) )
			except bson.errors.InvalidId:
				pass # if it's not a valid ObjectId, then pass through and allow the assert to fail
			else:
				return {
					'_ref': dbref.DBRef( self.documentClass._collection, objectId ),
				}
		
		assert isinstance(pythonValue, self.documentClass), \
				"Referenced value must be a document of type %s" % (self.documentName,)
		assert pythonValue.id is not None, "Referenced Document must be saved before being assigned"
		
		data = {
			'_types': serialiseTypesForDocumentType(pythonValue.__class__),
			'_ref': dbref.DBRef( pythonValue.__class__._collection, pythonValue.id ),
		}
		
		return data
示例#2
0
	def fromPython( self, pythonValue, dereferences=[], modifier=None ):
		self._getClassInfo( )
		
		if pythonValue is None:
			return None
		
		if len(dereferences) > 0:
			return pythonValue # can't do any value checking here.. we actually need to recurse to our referenced class's fromPython
		
		if not isinstance(pythonValue, self.documentClass):
			# try mapping to an objectid
			try:
				objectId = objectid.ObjectId( str( pythonValue ) )
			except InvalidId:
				pass # if it's not a valid ObjectId, then pass through and allow the assert to fail
			else:
				return {
					'_ref': dbref.DBRef( self.documentClass._collection, objectId ),
				}
		
		assert isinstance(pythonValue, self.documentClass), \
				"Referenced value must be a document of type %s" % (self.documentName,)
		assert pythonValue.id is not None, "Referenced Document must be saved before being assigned"
		
		data = {
			'_types': serialiseTypesForDocumentType(pythonValue.__class__),
			'_ref': dbref.DBRef( pythonValue.__class__._collection, pythonValue.id ),
		}
		
		if self.cached_fields is not None:
			for field in self.cached_fields:
				if field.startswith('_'): continue
				data[field] = getattr(pythonValue, field)
		
		return data
示例#3
0
	def __init__( self, **kwargs ):
		self._is_lazy = False
		self._data = {}
		self._values = {}
		
		self._data['_types'] = serialiseTypesForDocumentType( self.__class__ )
		
		for name,value in kwargs.iteritems( ):
			setattr(self, name, value)
示例#4
0
	def __init__( self, document, collection, query=None, orderBy=None, onlyFields=None ):
		self.document = document
		self.documentTypes = serialiseTypesForDocumentType( document )
		self.collection = collection
		self.orderBy = []
		self.onlyFields = onlyFields
		if orderBy is not None:
			self.orderBy = orderBy[:]
		self._savedCount = None
		self._savedItems = None
		self._savedBuiltItems = None
		if query is None:
			self.query = Q( )
		else:
			self.query = query
示例#5
0
	def __init__( self, document, collection, query=None, orderBy=None, fields=None, timeout=True, readPref=None, types=None ):
		self.document = document
		self.documentTypes = serialiseTypesForDocumentType( document )
		self.collection = collection
		self.orderBy = []
		self._fields = fields
		self.timeout = timeout
		self.readPref = readPref
		self.types = []
		if orderBy is not None:
			self.orderBy = orderBy[:]
		self._savedCount = None
		self._savedItems = None
		if query is None:
			self.query = Q( )
		else:
			self.query = query
		if types:
			for subclass in types:
				if not issubclass(subclass, self.document):
					raise TypeError, "'%s' is not a subclass of '%s'" % (subclass, self.document)
				self.types.append( subclass )