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