Do not return any mongodb instructions as a result of task update operations

This commit is contained in:
allegroai 2024-06-20 17:44:17 +03:00
parent 0496582d96
commit cb9812caee
2 changed files with 20 additions and 22 deletions

View File

@ -4,6 +4,7 @@ from typing import Sequence
import attr
import six
from mongoengine import Q
from mongoengine.base import UPDATE_OPERATORS
from apiserver.apierrors import errors
from apiserver.bll.util import update_project_time
@ -78,8 +79,16 @@ class ChangeStatusRequest(object):
update_project_time(project_id)
# make sure that _raw_ queries are not returned back to the client
fields.pop("__raw__", None)
def is_mongo_operator(field: str) -> bool:
head, _, tail = field.partition("__")
return tail and (head in UPDATE_OPERATORS)
# make sure to not return _raw_ queries or any of the update operators
fields = {
key: value
for key, value in fields.items()
if not (key == "__raw__" or is_mongo_operator(key))
}
return dict(updated=updated, fields=fields)

View File

@ -1027,14 +1027,7 @@ def reset(call: APICall, company_id, request: ResetRequest):
clear_all=request.clear_all,
delete_external_artifacts=request.delete_external_artifacts,
)
res = ResetResponse(**updates, dequeued=dequeued)
# do not return artifacts since they are not serializable
res.fields.pop("execution.artifacts", None)
for key, value in attr.asdict(cleanup_res).items():
setattr(res, key, value)
res = ResetResponse(**updates, **attr.asdict(cleanup_res), dequeued=dequeued)
call.result.data_model = res
@ -1058,23 +1051,19 @@ def reset_many(call: APICall, company_id, request: ResetManyRequest):
ids=request.ids,
)
def clean_res(res: dict) -> dict:
# do not return artifacts since they are not serializable
fields = res.get("fields")
if fields:
fields.pop("execution.artifacts", None)
return res
call.result.data_model = ResetManyResponse(
succeeded=[
succeeded = []
for _id, (dequeued, cleanup, res) in results:
succeeded.append(
ResetBatchItem(
id=_id,
dequeued=bool(dequeued.get("removed")) if dequeued else False,
**attr.asdict(cleanup),
**clean_res(res),
**res,
)
for _id, (dequeued, cleanup, res) in results
],
)
call.result.data_model = ResetManyResponse(
succeeded=succeeded,
failed=failures,
)