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 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