def delete_collection(collection, keep_metadata=False, sync=False): cancel_queue(collection) aggregator = get_aggregator(collection) try: aggregator.drop() finally: aggregator.close() flush_notifications(collection, sync=sync) index.delete_entities(collection.id, sync=sync) xref_index.delete_xref(collection, sync=sync) deleted_at = collection.deleted_at or datetime.utcnow() Entity.delete_by_collection(collection.id, deleted_at=deleted_at) Mapping.delete_by_collection(collection.id, deleted_at=deleted_at) Diagram.delete_by_collection(collection.id, deleted_at=deleted_at) Document.delete_by_collection(collection.id) if not keep_metadata: # Considering linkages metadata for now, might be wrong: Linkage.delete_by_collection(collection.id) Permission.delete_by_collection(collection.id, deleted_at=deleted_at) collection.delete(deleted_at=deleted_at) db.session.commit() if not keep_metadata: index.delete_collection(collection.id, sync=True) Authz.flush() refresh_collection(collection.id, sync=True)
def cleanup_deleted(): from aleph.model import Alert, Entity, Collection from aleph.model import Permission, Role, Document from aleph.model import Diagram, Mapping Mapping.cleanup_deleted() Diagram.cleanup_deleted() Document.cleanup_deleted() Alert.cleanup_deleted() Permission.cleanup_deleted() Entity.cleanup_deleted() Collection.cleanup_deleted() Role.cleanup_deleted() db.session.commit()
def delete_collection(collection, keep_metadata=False, sync=False): reset_collection(collection, sync=False) deleted_at = collection.deleted_at or datetime.utcnow() Entity.delete_by_collection(collection.id, deleted_at=deleted_at) Mapping.delete_by_collection(collection.id, deleted_at=deleted_at) Diagram.delete_by_collection(collection.id, deleted_at=deleted_at) Document.delete_by_collection(collection.id) if not keep_metadata: Permission.delete_by_collection(collection.id, deleted_at=deleted_at) collection.delete(deleted_at=deleted_at) db.session.commit() if not keep_metadata: index.delete_collection(collection.id, sync=sync) Authz.flush() refresh_collection(collection.id, sync=True)
def view(diagram_id): """Return the diagram with id `diagram_id`. --- get: summary: Fetch a diagram parameters: - description: The diagram id. in: path name: diagram_id required: true schema: minimum: 1 type: integer example: 2 responses: '200': content: application/json: schema: $ref: '#/components/schemas/Diagram' description: OK tags: - Diagram """ diagram = obj_or_404(Diagram.by_id(diagram_id)) get_db_collection(diagram.collection_id, request.authz.READ) return DiagramSerializer.jsonify(diagram)
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 delete(diagram_id): """Delete a diagram. --- delete: summary: Delete a diagram parameters: - description: The diagram id. in: path name: diagram_id required: true schema: minimum: 1 type: integer example: 2 responses: '204': description: No Content tags: - Diagram """ diagram = obj_or_404(Diagram.by_id(diagram_id)) collection = get_db_collection(diagram.collection_id, request.authz.WRITE) diagram.delete() collection.touch() db.session.commit() return ('', 204)
def delete_collection(collection, keep_metadata=False, sync=False, reset_sync=False): reset_collection(collection, sync=reset_sync) deleted_at = collection.deleted_at or datetime.utcnow() Entity.delete_by_collection(collection.id, deleted_at=deleted_at) Mapping.delete_by_collection(collection.id, deleted_at=deleted_at) Diagram.delete_by_collection(collection.id, deleted_at=deleted_at) Document.delete_by_collection(collection.id) if not keep_metadata: # Considering this metadata for now, might be wrong: Linkage.delete_by_collection(collection.id) Permission.delete_by_collection(collection.id, deleted_at=deleted_at) collection.delete(deleted_at=deleted_at) db.session.commit() if not keep_metadata: index.delete_collection(collection.id, sync=sync) Authz.flush() refresh_collection(collection.id, sync=True)
def index(): """Returns a list of diagrams for the role --- get: summary: List diagrams parameters: - description: The collection id. in: query name: 'filter:collection_id' required: true schema: minimum: 1 type: integer responses: '200': content: application/json: schema: type: object allOf: - $ref: '#/components/schemas/QueryResponse' properties: results: type: array items: $ref: '#/components/schemas/Diagram' description: OK tags: - Diagram """ parser = QueryParser(request.args, request.authz) q = Diagram.by_authz(request.authz) collection_ids = ensure_list(parser.filters.get('collection_id')) if len(collection_ids): q = q.filter(Diagram.collection_id.in_(collection_ids)) result = DatabaseQueryResult(request, q) return DiagramSerializer.jsonify_result(result)
def update(diagram_id): """Update the diagram with id `diagram_id`. --- post: summary: Update a diagram parameters: - description: The diagram id. in: path name: diagram_id required: true schema: minimum: 1 type: integer example: 2 requestBody: content: application/json: schema: $ref: '#/components/schemas/DiagramUpdate' responses: '200': content: application/json: schema: $ref: '#/components/schemas/Diagram' description: OK tags: - Diagram """ diagram = obj_or_404(Diagram.by_id(diagram_id)) collection = get_db_collection(diagram.collection_id, request.authz.WRITE) data = parse_request('DiagramUpdate') diagram.update(data, collection) collection.touch() 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
def get_diagram(diagram_id): return Diagram.by_id(diagram_id)