def deconvert(self, value): '''Changes a stored Reference to its Model''' if value is None: return None dict = yaml.load(value) assert "id" in dict and "keyspace" in dict and "kind" in dict, "Homer cannot load an invalid reference" clasz = MetaModel.discover(dict["kind"]) return MetaModel(clasz).read(dict["id"])
def save(self, unique=False): """Stores this Model in Cassandra in one batch update.""" from homer.core.types import TypedCollection model = MetaModel(self) if model.created: model.update() else: model.new(unique) properties = fields(self, CqlProperty) for name, property in properties.items(): #Commit the changes in all collections within this model. if isinstance(property, CqlCollection): value = self[name] if isinstance(value, TypedCollection): value.commit() self.differ.commit() #Commit the differ for the model finally. self.__saved = True #Mark this model as saved.
def validate(self, value): '''Makes sure you can only set a Model or a Key that is complete on a Reference''' if self.empty(value): return None if isinstance(self.clasz, str): found = MetaModel.discover(self.clasz) if found: self.clasz = found if not isinstance(value, (self.clasz, Model)): raise BadValueError("Value: {0} must be an instance of {1}".format(value, self.clasz)) value.validate() #Finally validate the model then return it. return value