def view(entityset_id): """Return the entityset with id `entityset_id`. --- get: summary: Fetch an entityset parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySet' description: OK tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.READ) return EntitySetSerializer.jsonify(entityset)
def item_index(entityset_id): """See a list of all items in that are linked to this entity set. This gives entities that are judged negative and unsure alongside the positive matches returned by the subling `./entities` API. --- post: summary: Get all items in the entity set. parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySetItemResponse' description: OK tags: - EntitySetItem """ entityset = get_entityset(entityset_id, request.authz.READ) result = DatabaseQueryResult(request, entityset.items()) return EntitySetItemSerializer.jsonify_result(result)
def entities_index(entityset_id): """Search entities in the entity set with id `entityset_id`. --- get: summary: Search entities in the entity set with id `entityset_id` description: > Supports all query filters and arguments present in the normal entity search API, but all resulting entities will be members of the set. parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '200': description: Resturns a list of entities in result content: application/json: schema: $ref: '#/components/schemas/EntitiesResponse' tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.READ) parser = SearchQueryParser(request.args, request.authz) tag_request(query=parser.text, prefix=parser.prefix) result = EntitySetItemsQuery.handle(request, parser=parser, entityset=entityset) return EntitySerializer.jsonify_result(result)
def update(entityset_id): """Update the entityset with id `entityset_id`. --- post: summary: Update an entityset 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/EntitySetUpdate' responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySet' description: OK tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.WRITE) data = parse_request("EntitySetUpdate") entityset.update(data) db.session.commit() return EntitySetSerializer.jsonify(entityset)
def view(entityset_id): """Return the entityset with id `entityset_id`. --- get: summary: Fetch an entityset parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySet' description: OK tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.READ) if entityset.type == EntitySet.PROFILE: return redirect(url_for("profile_api.view", profile_id=entityset_id)) data = entityset.to_dict() data["shallow"] = False return EntitySetSerializer.jsonify(data)
def item_update(entityset_id): """Add an item to the entity set with id `entityset_id`, or change the items judgement. To delete an item from the entity set, apply the judgement: `no_judgement`. --- post: summary: Add item to an entityset 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/EntitySetItemUpdate' responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySetItem' description: OK '204': description: Item removed tags: - EntitySetItem """ entityset = get_entityset(entityset_id, request.authz.WRITE) data = parse_request("EntitySetItemUpdate") entity = data.pop("entity", {}) entity_id = data.pop("entity_id", entity.get("id")) entity = get_index_entity(entity_id, request.authz.READ) collection = get_db_collection(entity["collection_id"]) data["added_by_id"] = request.authz.id data.pop("collection", None) item = save_entityset_item(entityset, collection, entity_id, **data) db.session.commit() job_id = get_session_id() queue_task(collection, OP_UPDATE_ENTITY, job_id=job_id, entity_id=entity_id) if item is not None: # The entityset is needed to check if the item is writeable in the serializer: item = item.to_dict(entityset=entityset) else: item = { "id": "$".join((entityset_id, entity_id)), "entityset_id": entityset_id, "entityset_collection_id": entityset.collection_id, "entity_id": entity_id, "collection_id": entity["collection_id"], "judgement": Judgement.NO_JUDGEMENT, } return EntitySetItemSerializer.jsonify(item)
def item_update(entityset_id): """Add an item to the entity set with id `entityset_id`, or change the items judgement. To delete an item from the entity set, apply the judgement: `no_judgement`. --- post: summary: Add item to an entityset 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/EntitySetItemUpdate' responses: '200': content: application/json: schema: $ref: '#/components/schemas/EntitySetItem' description: OK '204': description: Item removed tags: - EntitySetItem """ entityset = get_entityset(entityset_id, request.authz.WRITE) data = parse_request("EntitySetItemUpdate") entity = data.pop("entity", {}) entity_id = data.pop("entity_id", entity.get("id")) entity = get_index_entity(entity_id, request.authz.READ) data["collection_id"] = entity["collection_id"] data["added_by_id"] = request.authz.id item = EntitySetItem.save(entityset, entity_id, **data) db.session.commit() if item is None: return ("", 204) return EntitySetItemSerializer.jsonify(item)
def delete(entityset_id): """Delete an entity set. --- delete: summary: Delete an entity set parameters: - description: The entity set ID. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '204': description: No Content tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.WRITE) entityset.delete() db.session.commit() return ("", 204)
def embed(entityset_id): """Return an embedded network diagram for the entityset with ID `entityset_id`. --- post: summary: Create an embedded network diagram parameters: - description: The entityset id. in: path name: entityset_id required: true schema: type: string example: 3a0d91ece2dce88ad3259594c7b642485235a048 responses: '200': content: application/json: schema: type: object properties: embed: type: string description: HTML fragment to be embedded. url: type: string format: url description: Published version of the embedded file. description: OK tags: - EntitySet """ entityset = get_entityset(entityset_id, request.authz.WRITE) if entityset.type != EntitySet.DIAGRAM: raise BadRequest(gettext("Only diagrams can be embedded!")) data = publish_diagram(entityset) return jsonify(data)
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)
def get_entityset_id(data): entityset_id = get_nested(data, "entityset", "entityset_id") if entityset_id is not None: get_entityset(entityset_id, request.authz.WRITE) return entityset_id
def bulk(collection_id): """ --- post: summary: Load entities into a collection description: > Bulk load entities into the collection with id `collection_id` parameters: - description: The collection ID. in: path name: collection_id required: true schema: minimum: 1 type: integer - description: >- This will disable checksum security measures in order to allow bulk loading of document data. in: query name: unsafe schema: type: boolean requestBody: description: Entities to be loaded. content: application/json: schema: type: array items: $ref: '#/components/schemas/EntityUpdate' responses: '204': description: No Content tags: - Collection """ collection = get_db_collection(collection_id, request.authz.WRITE) require(request.authz.can_bulk_import()) job_id = get_session_id() entityset = request.args.get("entityset_id") if entityset is not None: entityset = get_entityset(entityset, request.authz.WRITE) # This will disable checksum security measures in order to allow bulk # loading of document data: safe = get_flag("safe", default=True) # Flag is only available for admins: if not request.authz.is_admin: safe = True # Let UI tools change the entities created by this: mutable = get_flag("mutable", default=False) role_id = request.authz.id entities = ensure_list(request.get_json(force=True)) entity_ids = list() for entity_id in bulk_write(collection, entities, safe=safe, mutable=mutable, role_id=role_id): entity_ids.append(entity_id) if entityset is not None: EntitySetItem.save( entityset, entity_id, collection_id=collection.id, added_by_id=request.authz.id, ) collection.touch() db.session.commit() data = {"entity_ids": entity_ids} queue_task(collection, OP_INDEX, job_id=job_id, payload=data) return ("", 204)