Skip to content

add_model_to_user

fastagency.studio.helpers.add_model_to_user async #

add_model_to_user(
    user_uuid: str,
    type_name: str,
    model_name: str,
    model_uuid: str,
    model: dict[str, Any],
    background_tasks: BackgroundTasks,
) -> dict[str, Any]
Source code in fastagency/studio/helpers.py
async def add_model_to_user(
    user_uuid: str,
    type_name: str,
    model_name: str,
    model_uuid: str,
    model: dict[str, Any],
    background_tasks: BackgroundTasks,
) -> dict[str, Any]:
    try:
        registry = Registry.get_default()
        validated_model = registry.validate(type_name, model_name, model)

        validated_model_dict = validated_model.model_dump()
        validated_model_json = validated_model.model_dump_json()
        saas_app = None

        if type_name == "deployment":
            saas_app = await validate_tokens_and_create_gh_repo(
                validated_model_dict, model_uuid
            )

            validated_model_dict["app_deploy_status"] = "inprogress"
            validated_model_dict["gh_repo_url"] = saas_app.gh_repo_url

            updated_validated_model_dict = json.loads(validated_model_json)
            updated_validated_model_dict["app_deploy_status"] = "inprogress"
            updated_validated_model_dict["gh_repo_url"] = saas_app.gh_repo_url
            validated_model_json = json.dumps(updated_validated_model_dict)

        await DefaultDB.frontend().get_user(user_uuid=user_uuid)
        await DefaultDB.backend().create_model(
            model_uuid=model_uuid,
            user_uuid=user_uuid,
            type_name=type_name,
            model_name=model_name,
            json_str=validated_model_json,
        )

        if saas_app is not None:
            background_tasks.add_task(
                deploy_saas_app,
                saas_app,
                user_uuid,
                model_uuid,
                type_name,
                model_name,
            )

        return validated_model_dict

    except InvalidGHTokenError as e:
        raise HTTPException(status_code=422, detail=str(e)) from e

    except InvalidFlyTokenError as e:
        raise HTTPException(status_code=422, detail=str(e)) from e

    except Exception as e:
        msg = "Oops! Something went wrong. Please try again later."
        raise HTTPException(status_code=422, detail=msg) from e