def transform_incoming(ent_cls, ent): """ Converts an incoming entity into a SON object. In particular, this involves translating entity references to DB Refs. """ son = SON() son['_id'] = getattr(ent, '_id') # We store the slug for querying purposes. son['slug'] = ent.slug for attr in get_domain_class_attribute_iterator(ent_cls): try: value = getattr(ent, attr.entity_attr) except AttributeError: continue if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL: son[attr.entity_attr] = value else: if value is None: son[attr.entity_attr] = None else: root_coll = get_root_collection(attr.attr_type) if attr.kind == RESOURCE_ATTRIBUTE_KINDS.MEMBER: son[attr.entity_attr] = DBRef(root_coll.__name__, getattr(value, '_id')) else: son[attr.entity_attr] = [DBRef(root_coll.__name__, getattr(el, '_id')) for el in value] return son
def get_state_data(cls, entity): """ Returns the state data for the given entity. This also works for unmanaged entities. """ attrs = get_domain_class_attribute_iterator(type(entity)) return dict([(attr, get_nested_attribute(entity, attr.entity_attr)) for attr in attrs if not attr.entity_attr is None])
def transform_outgoing(entity_class, son): """ Converts an outgoing SON object into an entity. DBRefs are converted lazily. """ ref_map = {} ent = object.__new__(entity_class) for attr in get_domain_class_attribute_iterator(entity_class): try: value = son[attr.entity_attr] except KeyError: continue if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL or value is None: setattr(ent, attr.entity_attr, value) else: ref_map[attr.entity_attr] = value ent.__mongo_refs__ = ref_map # Set the _id attribute. setattr(ent, '_id', son['_id']) return ent
def _attribute_iterator(self): it = get_domain_class_attribute_iterator(self._data) for attr in it: if not attr.entity_attr is None: yield attr