def get_by_task_id(call: APICall, company_id, _): if call.requested_endpoint_version > ModelsBackwardsCompatibility.max_version: raise errors.moved_permanently.NotSupported( "use models.get_by_id/get_all apis") task_id = call.data["task"] query = dict(id=task_id, company=company_id) task = Task.get(_only=["models"], **query) if not task: raise errors.bad_request.InvalidTaskId(**query) if not task.models or not task.models.output: raise errors.bad_request.MissingTaskFields(field="models.output") model_id = task.models.output[-1].model model = Model.objects( Q(id=model_id) & get_company_or_none_constraint(company_id)).first() if not model: raise errors.bad_request.InvalidModelId( "no such public or company model", id=model_id, company=company_id, ) model_dict = model.to_proper_dict() conform_output_tags(call, model_dict) unescape_metadata(call, model_dict) call.result.data = {"model": model_dict}
def _update_model(call: APICall, company_id, model_id=None): model_id = model_id or call.data["model"] model = ModelBLL.get_company_model_by_id(company_id=company_id, model_id=model_id) data = prepare_update_fields(call, company_id, call.data) task_id = data.get("task") iteration = data.get("iteration") if task_id and iteration is not None: TaskBLL.update_statistics( task_id=task_id, company_id=company_id, last_iteration_max=iteration, ) updated_count, updated_fields = Model.safe_update(company_id, model.id, data) if updated_count: if any(uf in updated_fields for uf in last_update_fields): model.update(upsert=False, last_update=datetime.utcnow()) new_project = updated_fields.get("project", model.project) if new_project != model.project: _reset_cached_tags(company_id, projects=[new_project, model.project]) else: _update_cached_tags(company_id, project=model.project, fields=updated_fields) conform_output_tags(call, updated_fields) unescape_metadata(call, updated_fields) return UpdateResponse(updated=updated_count, fields=updated_fields)
def update(call: APICall, company_id, req_model: UpdateRequest): data = call.data_model_for_partial_update conform_tag_fields(call, data, validate=True) escape_metadata(data) updated, fields = queue_bll.update(company_id=company_id, queue_id=req_model.queue, **data) conform_output_tags(call, fields) unescape_metadata(call, fields) call.result.data_model = UpdateResponse(updated=updated, fields=fields)
def get_by_id_ex(call: APICall, company_id, _): conform_tag_fields(call, call.data) Metadata.escape_query_parameters(call) with TimingContext("mongo", "models_get_by_id_ex"): models = Model.get_many_with_join(company=company_id, query_dict=call.data, allow_public=True) conform_output_tags(call, models) unescape_metadata(call, models) call.result.data = {"models": models}
def get_all(call: APICall): conform_tag_fields(call, call.data) ret_params = {} Metadata.escape_query_parameters(call) queues = queue_bll.get_all( company_id=call.identity.company, query_dict=call.data, ret_params=ret_params, ) conform_output_tags(call, queues) unescape_metadata(call, queues) call.result.data = {"queues": queues, **ret_params}
def get_all(call: APICall, company_id, _): conform_tag_fields(call, call.data) Metadata.escape_query_parameters(call) with TimingContext("mongo", "models_get_all"): ret_params = {} models = Model.get_many( company=company_id, parameters=call.data, query_dict=call.data, allow_public=True, ret_params=ret_params, ) conform_output_tags(call, models) unescape_metadata(call, models) call.result.data = {"models": models, **ret_params}
def edit(call: APICall, company_id, _): model_id = call.data["model"] model = ModelBLL.get_company_model_by_id(company_id=company_id, model_id=model_id) fields = parse_model_fields(call, create_fields) fields = prepare_update_fields(call, company_id, fields) for key in fields: field = getattr(model, key, None) value = fields[key] if (field and isinstance(value, dict) and isinstance(field, EmbeddedDocument)): d = field.to_mongo(use_db_field=False).to_dict() d.update(value) fields[key] = d iteration = call.data.get("iteration") task_id = model.task or fields.get("task") if task_id and iteration is not None: TaskBLL.update_statistics( task_id=task_id, company_id=company_id, last_iteration_max=iteration, ) if fields: if any(uf in fields for uf in last_update_fields): fields.update(last_update=datetime.utcnow()) updated = model.update(upsert=False, **fields) if updated: new_project = fields.get("project", model.project) if new_project != model.project: _reset_cached_tags(company_id, projects=[new_project, model.project]) else: _update_cached_tags(company_id, project=model.project, fields=fields) conform_output_tags(call, fields) unescape_metadata(call, fields) call.result.data_model = UpdateResponse(updated=updated, fields=fields) else: call.result.data_model = UpdateResponse(updated=0)
def get_by_id(call: APICall, company_id, _): model_id = call.data["model"] Metadata.escape_query_parameters(call) models = Model.get_many( company=company_id, query_dict=call.data, query=Q(id=model_id), allow_public=True, ) if not models: raise errors.bad_request.InvalidModelId( "no such public or company model", id=model_id, company=company_id, ) conform_output_tags(call, models[0]) unescape_metadata(call, models[0]) call.result.data = {"model": models[0]}
def get_by_id(call: APICall, company_id, req_model: QueueRequest): queue = queue_bll.get_by_id(company_id, req_model.queue) queue_dict = queue.to_proper_dict() conform_output_tags(call, queue_dict) unescape_metadata(call, queue_dict) call.result.data = {"queue": queue_dict}