def create(): """ --- post: summary: Create an entity in a collection description: >- Create an entity in a collection with a given schema and a set of given properties in the database. This is not the API you want to be using to load bulk data, but only for interactive entity manipulation in the UI. Always use the `bulk` API or for loading source datasets, no exceptions. requestBody: content: application/json: schema: $ref: '#/components/schemas/EntityCreate' responses: '200': description: Resturns the created entity content: application/json: schema: $ref: '#/components/schemas/Entity' tags: - Entity """ data = parse_request('EntityCreate') collection = get_nested_collection(data, request.authz.WRITE) data.pop('id', None) validate = get_flag('validate', default=False) entity_id = upsert_entity(data, collection, sync=True, validate=validate) tag_request(entity_id=entity_id, collection_id=str(collection.id)) entity = get_index_entity(entity_id, request.authz.READ) return EntitySerializer.jsonify(entity)
def create(): """Create a diagram. --- post: summary: Create a diagram requestBody: content: application/json: schema: $ref: '#/components/schemas/DiagramCreate' responses: '200': content: application/json: schema: $ref: '#/components/schemas/Diagram' description: OK tags: - Diagram """ data = parse_request('DiagramCreate') collection = get_nested_collection(data, request.authz.WRITE) old_to_new_id_map = {} entity_ids = [] for entity in data.pop('entities', []): old_id = entity.get('id') new_id = upsert_entity(entity, collection, sync=True) old_to_new_id_map[old_id] = new_id entity_ids.append(new_id) data['entities'] = entity_ids layout = data.get('layout', {}) data['layout'] = replace_layout_ids(layout, old_to_new_id_map) diagram = Diagram.create(data, collection, request.authz.id) db.session.commit() return DiagramSerializer.jsonify(diagram)
def _load_data_for_update(self, fixture): fixture = self.get_fixture_path(fixture) with open(fixture, 'r') as fp: data = json.load(fp) data = _normalize_data(data) layout = data.pop('layout') entities = layout.pop('entities') # Replace entitiy ids given by VIS with our own newly created signed # entity ids. signed_entity_ids = {} for ent in entities: ent = json.dumps(ent) for old_id, new_id in signed_entity_ids.items(): ent = ent.replace(old_id, new_id) ent = json.loads(ent) # clear existing id if any ent.pop('foreign_id', None) signed_entity_id = upsert_entity(ent, self.col) signed_entity_ids[ent['id']] = signed_entity_id # Do the same replacement in layout layout = _replace_ids(layout, signed_entity_ids) return { 'collection_id': str(self.col.id), 'layout': layout, 'entities': list(signed_entity_ids.values()), 'label': 'Royal Family', 'summary': '...' }
def create_entityset(collection, data, authz): """Create an entity set. This will create or update any entities that already exist in the entityset and sign their IDs into the collection. """ old_to_new_id_map = {} entity_ids = [] for entity in data.pop("entities", []): old_id = entity.get("id") new_id = upsert_entity(entity, collection, sync=True) old_to_new_id_map[old_id] = new_id entity_ids.append(new_id) layout = data.get("layout", {}) data["layout"] = replace_layout_ids(layout, old_to_new_id_map) entityset = EntitySet.create(data, collection, authz) for entity_id in entity_ids: save_entityset_item(entityset, collection, entity_id) publish( Events.CREATE_ENTITYSET, params={ "collection": collection, "entityset": entityset }, channels=[collection, authz.role], actor_id=authz.id, ) return entityset
def _load_data_for_update(self, fixture): fixture = self.get_fixture_path(fixture) with open(fixture, "r") as fp: data = json.load(fp) data = _normalize_data(data) layout = data.pop("layout") entities = layout.pop("entities") # Replace entitiy ids with our own newly created signed # entity ids. signed_entity_ids = {} for ent in entities: ent = json.dumps(ent) for old_id, new_id in signed_entity_ids.items(): ent = ent.replace(old_id, new_id) ent = json.loads(ent) # clear existing id if any ent.pop("foreign_id", None) signed_entity_id = upsert_entity(ent, self.col) signed_entity_ids[ent["id"]] = signed_entity_id return { "collection_id": str(self.col.id), "layout": layout, "entities": list(signed_entity_ids.values()), "label": "Royal Family", "type": "diagram", "summary": "...", }
def update(entity_id): """ --- post: summary: Update an entity description: > Update the entity with id `entity_id`. This only applies to entities which are backed by a database row, i.e. not any entities resulting from a mapping or bulk load. parameters: - in: path name: entity_id required: true schema: type: string format: entity_id - in: query name: sign description: Sign entity IDs referenced in nested properties. required: false schema: type: boolean requestBody: content: application/json: schema: $ref: '#/components/schemas/EntityUpdate' responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/Entity' tags: - Entity """ data = parse_request("EntityUpdate") try: entity = get_index_entity(entity_id, request.authz.WRITE) require(check_write_entity(entity, request.authz)) collection = get_db_collection(entity.get("collection_id"), request.authz.WRITE) except NotFound: collection = get_nested_collection(data, request.authz.WRITE) tag_request(collection_id=collection.id) data["id"] = entity_id if get_flag("validate", default=False): validate_entity(data) entity_id = upsert_entity( data, collection, authz=request.authz, sync=get_flag("sync", default=True), sign=get_flag("sign", default=False), job_id=get_session_id(), ) db.session.commit() return view(entity_id)
def create(): """ --- post: summary: Create an entity in a collection description: >- Create an entity in a collection with a given schema and a set of given properties in the database. This is not the API you want to be using to load bulk data, but only for interactive entity manipulation in the UI. Always use the `bulk` API or for loading source datasets, no exceptions. parameters: - in: query name: sign description: Sign entity IDs referenced in nested properties. required: false schema: type: boolean requestBody: content: application/json: schema: $ref: '#/components/schemas/EntityCreate' responses: '200': description: Resturns the created entity content: application/json: schema: $ref: '#/components/schemas/Entity' tags: - Entity """ data = parse_request("EntityCreate") collection = get_nested_collection(data, request.authz.WRITE) data.pop("id", None) if get_flag("validate", default=False): validate_entity(data) entity_id = upsert_entity( data, collection, authz=request.authz, sync=True, sign=get_flag("sign", default=False), job_id=get_session_id(), ) db.session.commit() tag_request(entity_id=entity_id, collection_id=collection.id) entity = get_index_entity(entity_id, request.authz.READ) return EntitySerializer.jsonify(entity)
def update(entity_id): """ --- post: summary: Update an entity description: > Update the entity with id `entity_id`. This only applies to entities which are backed by a database row, i.e. not any entities resulting from a mapping or bulk load. parameters: - in: path name: entity_id required: true schema: type: string format: entity_id requestBody: content: application/json: schema: $ref: '#/components/schemas/EntityUpdate' responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/Entity' tags: - Entity """ data = parse_request('EntityUpdate') try: entity = get_index_entity(entity_id, request.authz.WRITE) collection = get_db_collection(entity.get('collection_id'), request.authz.WRITE) except NotFound: collection = get_nested_collection(data, request.authz.WRITE) tag_request(collection_id=collection.id) data['id'] = entity_id sync = get_flag('sync', default=True) validate = get_flag('validate', default=False) entity_id = upsert_entity(data, collection, validate=validate, sync=sync) db.session.commit() entity = get_index_entity(entity_id, request.authz.READ) return EntitySerializer.jsonify(entity)
def create_diagram(collection, data, authz): """Create a network diagram. This will create or update any entities that already exist in the diagram and sign their IDs into the collection. """ old_to_new_id_map = {} entity_ids = [] for entity in data.pop('entities', []): old_id = entity.get('id') new_id = upsert_entity(entity, collection, validate=False, sync=True) old_to_new_id_map[old_id] = new_id entity_ids.append(new_id) data['entities'] = entity_ids layout = data.get('layout', {}) data['layout'] = replace_layout_ids(layout, old_to_new_id_map) diagram = Diagram.create(data, collection, authz.id) db.session.commit() publish(Events.CREATE_DIAGRAM, params={ 'collection': collection, 'diagram': diagram }, channels=[collection, authz.role], actor_id=authz.id) return diagram
def entities_update(entityset_id): """ --- post: summary: Update an entity and add it to the entity set. description: > Update the entity with id `entity_id`. If it does not exist it will be created. If the user cannot edit the given entity, it is merely added to the entity set. New entities are always created in the collection of the entity set. Aside from these idiosyncracies, this is the same as `/api/2/entities/<id>`, but handles entity set membership transparently. parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 requestBody: content: application/json: schema: $ref: '#/components/schemas/EntityUpdate' responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/Entity' tags: - Entity """ entityset = get_entityset(entityset_id, request.authz.WRITE) data = parse_request("EntityUpdate") entity_id = data.get("id", make_textid()) try: entity = get_index_entity(entity_id, request.authz.READ) collection = get_db_collection(entity.get("collection_id"), request.authz.READ) except NotFound: entity = None collection = entityset.collection tag_request(collection_id=entityset.collection_id) if entity is None or check_write_entity(entity, request.authz): if get_flag("validate", default=False): validate_entity(data) sync = get_flag("sync", default=True) entity_id = upsert_entity(data, collection, authz=request.authz, sync=sync) EntitySetItem.save( entityset, entity_id, collection_id=collection.id, added_by_id=request.authz.id, ) db.session.commit() return entity_view(entity_id)