Skip to content

validate_user_settings

validate_user_settings(*, user, backend, db) async

Get a UserSettings object and validate it based on a given Fractal backend.

Parameters:

Name Type Description Default
user UserOAuth

The user whose settings we should validate.

required
backend str

The value of FRACTAL_RUNNER_BACKEND

required
db AsyncSession

An async DB session

required

Returns:

Type Description
UserSettings

UserSetting object associated to user, if valid.

Source code in fractal_server/app/routes/aux/validate_user_settings.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
async def validate_user_settings(
    *, user: UserOAuth, backend: str, db: AsyncSession
) -> UserSettings:
    """
    Get a UserSettings object and validate it based on a given Fractal backend.

    Arguments:
        user: The user whose settings we should validate.
        backend: The value of `FRACTAL_RUNNER_BACKEND`
        db: An async DB session

    Returns:
        `UserSetting` object associated to `user`, if valid.
    """

    verify_user_has_settings(user)

    user_settings = await db.get(UserSettings, user.user_settings_id)

    if backend == "slurm_ssh":
        UserSettingsValidationModel = SlurmSshUserSettings
    elif backend == "slurm":
        UserSettingsValidationModel = SlurmSudoUserSettings
    else:
        # For other backends, we don't validate anything
        UserSettingsValidationModel = BaseModel

    try:
        UserSettingsValidationModel(**user_settings.model_dump())
    except ValidationError as e:
        error_msg = (
            "User settings are not valid for "
            f"FRACTAL_RUNNER_BACKEND='{backend}'. "
            f"Original error: {str(e)}"
        )
        logger.warning(error_msg)
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=error_msg,
        )

    return user_settings

verify_user_has_settings(user)

Check that the user.user_settings_id foreign-key is set.

NOTE: This check will become useless when we make the foreign-key column required, but for the moment (as of v2.6.0) we have to keep it in place.

Parameters:

Name Type Description Default
user UserOAuth

The user to be checked.

required
Source code in fractal_server/app/routes/aux/validate_user_settings.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def verify_user_has_settings(user: UserOAuth) -> None:
    """
    Check that the `user.user_settings_id` foreign-key is set.

    NOTE: This check will become useless when we make the foreign-key column
    required, but for the moment (as of v2.6.0) we have to keep it in place.

    Arguments:
        user: The user to be checked.
    """
    if user.user_settings_id is None:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=f"Error: user '{user.email}' has no settings.",
        )