def replace_context(
    base_type: Type[T], httpreq: Request, id: str, request: ReplaceContextRequest
) -> T:
    """Replaces context in an arbitrary Yeti Object."""
    obj = base_type.get(id)
    if not obj:
        raise HTTPException(
            status_code=404, detail=f"{base_type.__name__} {id} not found"
        )

    for item in request.context:
        if "source" not in item:
            raise HTTPException(
                status_code=400, detail=f"Source is required in context {item}"
            )

    old_context = obj.context.copy()
    obj.context = request.context
    refreshed_obj = obj.save()
    obj.context = old_context
    audit.log_timeline(httpreq.state.username, refreshed_obj, old=obj)
    return refreshed_obj
Replaces context in an arbitrary Yeti Object.
core/web/apiv2/context.py