Beispiel #1
0
	def model_post_init(self, instance, **kwargs):
		inf = get_inst_info(instance)
		signals.fire("start-tracking-changes", instance=instance)
		self._alive[(inf.model_info.model, inf.get_pk_as_key())] = inf

		# If instance was not initialized with a value for primary key,
		# then it has not been saved yet and goes into self._dirty.
		if instance.pk is None:
			self.set_dirty(inf)
Beispiel #2
0
	def add(self, values):
		'''For adding ‘‘values‘‘ to the already related items.'''

		# assert self.relation.has_model(type(values[0])), (str(values[0]),
				# str(type(values[0])), str(self.relation.model),
				# str(self.relation.related_model))

		signals.fire("relation-pre-add", manager=self, values=values)
		self._add(values)
		signals.fire("relation-post-add", manager=self, values=values)
Beispiel #3
0
	def set(self, values):
		'''Set ‘‘values‘‘ to be the new related items, erasing any others.'''
		
		# assert self.relation.has_model(type(values[0])), (str(values[0]),
				# str(type(values[0])), str(self.relation.model),
				# str(self.relation.related_model))

		signals.fire("relation-pre-set", manager=self, values=values)
		self._set(values)
		signals.fire("relation-post-set", manager=self, values=values)
Beispiel #4
0
	def save(self):
		"""When overriding this method, do not forget to call the Model.save()
		method and to return a tuple of (instance, created), where created
		means, it the instance was newly INSERTED into the database.
		"""

		signals.fire("model-pre-save", instance=self)
		inst, created = self._save()
		signals.fire("model-post-save", instance=self, created=created)
		return inst, created
Beispiel #5
0
	def model_post_save(self, instance, created):
		inf = get_inst_info(instance)
		if not inf._meta["do-cache"]:
			return

		self.cache(inf)
		self._dirty.pop(inf, None)

		# On calling Model.delete, tracking of changes is stopped, so start
		# tracking now.
		signals.fire("start-tracking-changes", instance=instance)
Beispiel #6
0
	def remove(self, values):
		'''For removing ‘‘values‘‘ from the relation.'''

		
		# assert self.relation.has_model(type(values[0])), (str(values[0]),
				# str(type(values[0])), str(self.relation.model),
				# str(self.relation.related_model))

		signals.fire("relation-pre-remove", manager=self, values=values)
		self._remove(values)
		signals.fire("relation-post-remove", manager=self, values=values)
	def __set__(self, inst, val):
		inst_info = get_inst_info(inst)

		if inst_info._meta["track-changes"]:
			signals.fire("model-pre-update", instance=inst,
						value=inst_info.get(self.name), fieldname=self.name)

		field = type(inst).fields()[self.name]
		val = field.to_python(val)
		inst_info[field.column_name] = val

		if inst_info._meta["track-changes"]:
			signals.fire("model-post-update", instance=inst, value=val,
													fieldname=self.name)
Beispiel #8
0
	def _gen(self, start=None, stop=None, step=None):
		start, stop, step = start or 0, stop or sys.maxint, step or 1

		aliases = self.query.get_selection_aliases()
		pkcol = self.query.model.pk.column_name

		## Save all dirty instances to get consistent results.
		for obj in self.store._dirty.values():
			obj.save()

		for row in self.query.execute().fetchall()[start:stop:step]:
			vars = dict(zip(aliases, row))

			if not (self.query.model, vars[pkcol]) in self.store._alive:
				inst = self.query.model(**vars)
				inf = get_inst_info(inst)
			else:
				inf = self.store._alive[(self.query.model, vars[pkcol])]
				if inf.get_inst() is None:
					## Initializing a new instance with __init__ automatically
					## sets a new InstanceInfo instance which is not useful 
					## here, since the instance info already exists.
					inst = object.__new__(inf.model_info.model)
					inf.set_inst(inst)
					inf._meta["was-reloaded"] = True
				if inf._meta["was-reloaded"] or inf._meta["force-sync"]:
					inf.update(vars)

			self.store._cache.add(inf)
			yield inf.get_inst()

		# Remove any objects in this process that were deleted in other
		# processes.
		else:
			# Delete all self.query.model instances from the cache.
			# There is probably a more efficient way of cleaning the
			# cache (more selectively), but it seems not worth the 
			# effort atm.
			for m, pk in self.store._alive.keys():
				if isinstance(m, self.query.model):
					inf = self.store._alive[(m, pk)]
					if inf.get_inst() is not None:
						signals.fire("model-pre-delete", inf.get_inst(), True)
						signals.fire("model-post-delete", inf.get_inst(), True)
Beispiel #9
0
	def __init__(self, **kwargs):

		signals.fire("model-pre-init", instance=self, kwargs=kwargs)

		for fname, f in self.fields().items():
			if f.attrs.get("initial") is not None:
				if callable(f.attrs["initial"]):
					value = f.attrs["initial"]()
				else:
					value = f.attrs["initial"]
				setattr(self, fname, value)

		# Set any Field and non-Field parameters on this instance
		for name, value in kwargs.items():
			if name in self._relations.get_identifiers():
				raise TypeError("'%s' refers to a RelationManager. "\
						"RelationManagers can't be set on instantiating, "\
						"because at that point, the instance has not been "\
						"created and so has no id with which to link it to %s."\
						% (name, value))

			setattr(self, name, value)

		signals.fire("model-post-init", instance=self, kwargs=kwargs)
Beispiel #10
0
	def delete(self):
		'''For removing all items from the relation.'''

		signals.fire("relation-pre-delete", manager=self)
		self._delete()
		signals.fire("relation-post-delete", manager=self)
Beispiel #11
0
	def uncache(self):
		signals.fire("model-do-not-cache", instance=self)
Beispiel #12
0
	def delete(self, force_delete=False):
		signals.fire("model-pre-delete", instance=self)
		instance, deleted = self._delete()
		signals.fire("model-post-delete", instance=self, deleted=deleted)
		return instance, deleted
Beispiel #13
0
	def model_do_not_cache(self, instance):
		inf = get_inst_info(instance)
		self.uncache(inf)
		inf._meta["do-cache"] = False
		signals.fire("stop-tracking-changes", instance=instance)
Beispiel #14
0
	def model_do_cache(self, instance):
		inf = get_inst_info(instance)
		self.cache(inf)
		self.set_dirty(inf)
		inf._meta["do-cache"] = True
		signals.fire("start-tracking-changes", instance=instance)
Beispiel #15
0
	def model_pre_delete(self, instance):
		signals.fire("stop-tracking-changes", instance=instance)
Beispiel #16
0
	def rollback(self):
		signals.fire("cache-rollback")
Beispiel #17
0
	def model_pre_init(self, instance, **kwargs):
		signals.fire("stop-tracking-changes", instance=instance)