Ejemplo n.º 1
0
	def update(self, merge_data={}, commit=True):
		""" Update object with current data into mongo. 
		
		Arguments:
		merge_data -- dictionary used to represent the entire object.  This will overwrite the object's current data.
		commit -- Should this update of the data in memory be commited to mongo?  Default: True
		
		"""
		if (not merge_data=={}):
			self.merge_data(merge_data) #Intelligently merge the argument into the self.attributes variable
			cherrylog ("Data merged into object: "+ str(merge_data))
		if (commit):
			self.attributes['modified_at'] = datetime.datetime.now()
			cherrylog ("updating item in database")
			try:
				update = self.my_collection.update({'_id':oid(self.doc_id)}, self.attributes, safe=True)
			except Exception as e:
				Tools.print_errors ("Error updating object in database: " + str(self.__class__.__name__))
				return False
			cherrylog ("Update of item successul: " + (str(update)))
		return True
Ejemplo n.º 2
0
	def load(self, lookup_doc_id=None, alt_matching={}):
		"""Load data from database into object in memory
		
		Arguments:
		lookup_doc_id -- the doc_id of the object that will be used for matching.
		alt_matching -- An alternate dictionary for doing the lookup.
		One of these two parameters is required.
		
		"""
		if (lookup_doc_id != None):
			cherrylog ("Loading " + str(self.__class__.__name__) + " with params: id=" + str(lookup_doc_id))
		else:
			cherrylog ("Loading " + str(self.__class__.__name__) + " with params: " + str(alt_matching))
		try:
			match_param = {}
			if (alt_matching != {}):
				match_param = alt_matching
			else:
				match_param = {'_id':oid(lookup_doc_id)}
			docs = self.my_collection.find(match_param, safe=True)
			cherrylog ("got number of results as: " + str(docs.count()))
			if (docs.count() == 0):
				cherrylog ("Couldn't load object, 0 matching results in database!!")
				return False
			if (docs.count() == 1):
				self.doc_id = str(docs[0]['_id'])
				if docs[0].has_key('_id'): del docs[0]['_id']
				self.attributes = docs[0]
				return True
			else:
				raise Exception("Retrieved multiple videos when only 1 was expected")
		except Exception as e:
			Tools.print_errors ("Unable to load " + str(self.__class__.__name__) + " from database with params: id=" + str(self.doc_id))
			return False
		cherrylog ("Successfully loaded object: " + str(self.attributes))
		return True