def add_data_item(data_id, data_type, data): '''Add or update a data item given an ID, type, and data. Create a new item, for additions.''' item_id = original_item_id = ItemData.data_to_item(data_id) if item_id is None: item_id = _create_item(data_type) _register_data_item(item_id, data_id, data, update=original_item_id) _map_item(item_id) db.session.commit() return item_id
def _map_item(item_id): # fetch data item = Item.get(item_id) # generate map (mapped, links) = map_item(item.to_dict()) this_mapped = mapped[None] if list(verify_map(this_mapped)): print "Validation errors in mapping item %s: %r, continuing." % (item_id, list(verify_map(this_mapped))) # First, update this item's map item.map = json.dumps(this_mapped, separators=(',', ':')) db.session.flush() # Then, go through the links, creating items as needed with the types designated by their mapping paths ItemLink.delete_by_item_id(item_id) for (node, destination, data_id) in links: link_type = '%'.join([str(d) for d in destination]) if node is None: node_item = item_id else: node_item = ItemData.data_to_item(node) target_item = ItemData.data_to_item(data_id) if target_item is None: target_item = _create_item(_link_type_to_item_type(link_type)) print "%s -> %s" % (data_id, target_item) _register_data_item(target_item, data_id, '{}') if node_item is None: node_item = _create_item('') print "%s -> %s" % (node, node_item) _register_data_item(node_item, node, '{}') ItemLink.find_or_insert(node_item, target_item, link_type) # Now that we can be assured any types set by links are already set on the items, insert the maps for other mapped nodes for data_id in mapped.keys(): if data_id is not None: verify = list(verify_map(mapped[data_id])) if verify: print "Validation errors in mapping data item %s: %r, continuing" % (data_id, verify) add_data_item(data_id, '', json.dumps(mapped[data_id]))
def delete_data_item(data_id): ItemData.delete_data_item(data_id) db.session.commit()
def data_to_item(data_id): return ItemData.data_to_item(data_id)
def get_item_ids(index, item_type): return ItemData.get_item_ids(index, item_type)
def get_item_types_by_index(index): return ItemData.get_item_types_by_index(index)
def get_indexes(): return ItemData.get_indexes()
def _register_data_item(item_id, data_id, data, update=False): if not update: ItemData.create(item_id, data, data_id) else: ItemData.update(item_id, data, data_id)