Ejemplo n.º 1
0
 def create_entity(self, ctx, type_, **kwargs):
     entity = Entity(assume_contexts=[ctx.id])
     entity.set(types.Object.attributes.type, type_, ctx)
     for attr in type_.attributes:
         if attr.name in kwargs:
             entity.set(attr, kwargs.get(attr.name), ctx)
     return entity
Ejemplo n.º 2
0
def load_entity(context, mapping, record):
    type_ = types.get(mapping.get('type'))
    if type_ is None:
        log.warning("No type defined for entity in mapping: %r", mapping)
        return

    query = {'assume': [context.id], 'type': unicode(type_)}
    has_key = False

    data = [(types.Object.attributes.type, type_)]
    for attr in type_.attributes:
        if attr.name not in mapping or attr == types.Object.attributes.type:
            continue
        attr_map = mapping[attr.name]
        if attr.data_type == 'entity':
            value = load_entity(context, attr_map, record)
        else:
            value = record.get(attr_map.get('field'))

        if attr_map.get('key'):
            has_key = True
            query[attr.name] = value

        data.append((attr, value))

    query = EntityQuery(query)
    entity = query.first() if has_key else None
    if entity is None:
        entity = Entity()

    for (attr, value) in data:
        entity.set(attr, value, context)

    db.session.commit()
    # log.info("Loaded entity: %r", entity)
    return entity