def _set(self, _, others): args = [] for other in others: if other.id is None: msg = "You must save all other instances before defining a relationship" raise ReferenceNotSavedError(msg) args.append({ self.thisname: self.inst.id, self.othername: other.id }) return self.dbconfig.insertMany(self.tablename(), args)
def _set_polymorphic(self, others): ds = [] for other in others: if other.id is None: msg = "You must save all other instances before defining a relationship" raise ReferenceNotSavedError(msg) setattr(other, "%s_id" % self.args['as'], self.inst.id) setattr(other, "%s_type" % self.args['as'], self.thisclass.__name__) ds.append(other.save()) return defer.DeferredList(ds)
def _update(self, _, others): tablename = self.otherklass.tablename() args = {self.thisname: self.inst.id} ids = [] for other in others: if other.id is None: msg = "You must save all other instances before defining a relationship" raise ReferenceNotSavedError(msg) ids.append(str(other.id)) where = ["id IN (%s)" % ",".join(ids)] return self.dbconfig.update(tablename, args, where)
def __getattribute__(self, name): """ Get the given attribute. @param name: The name of the property to get. @return: If the name is a relationship based property, then a L{Relationship} instance will be returned. Otherwise the set property of the class will be returned. """ klass = object.__getattribute__(self, "__class__") if klass.RELATIONSHIP_CACHE is not None and name in klass.RELATIONSHIP_CACHE: if object.__getattribute__(self, 'id') is None: raise ReferenceNotSavedError("Cannot get/set relationship on unsaved object") relationshipKlass, args = klass.RELATIONSHIP_CACHE[name] return relationshipKlass(self, name, args) return object.__getattribute__(self, name)