def resolve_row_references(self, current_dataset, row): """resolve this DataRow object's referenced values. """ def resolved_rowlike(rowlike): key = rowlike.__name__ if rowlike._dataset is type(current_dataset): return DeferredStoredObject(rowlike._dataset, key) loaded_ds = self.loaded[rowlike._dataset] return loaded_ds.meta._stored_objects.get_object(key) def resolve_stored_object(candidate): if is_rowlike(candidate): return resolved_rowlike(candidate) else: # then it is the stored object itself. this would happen if # there is a reciprocal foreign key (i.e. organization has a # parent organization) return candidate for name in row.columns(): val = getattr(row, name) if type(val) in (types.ListType, types.TupleType): # i.e. categories = [python, ruby] setattr(row, name, map(resolve_stored_object, val)) elif is_rowlike(val): # i.e. category = python setattr(row, name, resolved_rowlike(val)) elif isinstance(val, Ref.Value): # i.e. category_id = python.id. ref = val.ref # now the ref will return the attribute from a stored object # when __get__ is invoked ref.dataset_obj = self.loaded[ref.dataset_class]
def resolve_stored_object(candidate): if is_rowlike(candidate): return resolved_rowlike(candidate) else: # then it is the stored object itself. this would happen if # there is a reciprocal foreign key (i.e. organization has a # parent organization) return candidate
def test_is_rowlike(): class StubDataSet(DataSet): class some_row: pass class StubDataSetNewStyle(DataSet): class some_row(object): pass eq_(is_rowlike(StubDataSet.some_row), True) eq_(is_rowlike(StubDataSetNewStyle.some_row), True) eq_(is_rowlike(DataRow(StubDataSet)), True) class StubRow: pass class StubRowNewStyle(object): pass eq_(is_rowlike(StubRow), False) eq_(is_rowlike(StubRowNewStyle), False) eq_(is_rowlike(1), False) eq_(is_rowlike({}), False) eq_(is_rowlike([]), False)
def test_row_is_rowlike(self): assert is_rowlike(self.dataset_class.lolita), ( "expected %s to be rowlike" % self.dataset_class.lolita)
def test_datarow_is_rowlike(self): class StubDataSet(DataSet): pass row = DataRow(StubDataSet) assert is_rowlike(row), "expected %s to be rowlike" % row