Skip to content

_aux_functions_templates

_check_template_duplication(*, user_id, name, version, db) async

Ensure that no WorkflowTemplate with the same (user_id, name, version) already exists.

PARAMETER DESCRIPTION
user_id

TYPE: int

name

TYPE: str

version

TYPE: int

db

TYPE: AsyncSession

RAISES DESCRIPTION
HTTPException(status_code=HTTP_422_UNPROCESSABLE_CONTENT)

If a duplicate template is found.

Source code in fractal_server/app/routes/api/v2/_aux_functions_templates.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
async def _check_template_duplication(
    *, user_id: int, name: str, version: int, db: AsyncSession
) -> None:
    """
    Ensure that no `WorkflowTemplate` with the same
    (`user_id`, `name`, `version`) already exists.

    Args:
        user_id:
        name:
        version:
        db:

    Raises:
        HTTPException(status_code=HTTP_422_UNPROCESSABLE_CONTENT):
            If a duplicate template is found.


    """
    res = await db.execute(
        select(WorkflowTemplate)
        .where(WorkflowTemplate.user_id == user_id)
        .where(WorkflowTemplate.name == name)
        .where(WorkflowTemplate.version == version)
    )
    duplicate = res.one_or_none()
    if duplicate:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
            detail=(
                "The current user already own a workflow template with "
                f"{name=} and {version=}."
            ),
        )

_get_template_full_access(*, user_id, template_id, db) async

Retrieve a WorkflowTemplate and ensure the user is its owner.

PARAMETER DESCRIPTION
user_id

TYPE: int

template_id

TYPE: int

db

TYPE: AsyncSession

RETURNS DESCRIPTION
WorkflowTemplate

The WorkflowTemplate object.

RAISES DESCRIPTION
HTTPException(status_code=404_NOT_FOUND)

If no template exists with the given ID.

HTTPException(status_code=403_FORBIDDEN)

If the user is not the owner.

Source code in fractal_server/app/routes/api/v2/_aux_functions_templates.py
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
async def _get_template_full_access(
    *, user_id: int, template_id: int, db: AsyncSession
) -> WorkflowTemplate:
    """
    Retrieve a `WorkflowTemplate` and ensure the user is its owner.

    Args:
        user_id:
        template_id:
        db:

    Returns:
        The `WorkflowTemplate` object.

    Raises:
        HTTPException(status_code=404_NOT_FOUND):
            If no template exists with the given ID.
        HTTPException(status_code=403_FORBIDDEN):
            If the user is not the owner.
    """
    template = await _get_template_or_404(template_id=template_id, db=db)
    if template.user_id != user_id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=(
                f"You are not the owner of WorkflowTemplate[{template_id}]."
            ),
        )
    return template

_get_template_or_404(*, template_id, db) async

Retrieve a WorkflowTemplate by ID.

PARAMETER DESCRIPTION
template_id

TYPE: int

db

TYPE: AsyncSession

RETURNS DESCRIPTION
WorkflowTemplate

The WorkflowTemplate object.

RAISES DESCRIPTION
HTTPException(status_code=404_NOT_FOUND)

If no template exists with the given ID.

Source code in fractal_server/app/routes/api/v2/_aux_functions_templates.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
async def _get_template_or_404(
    *, template_id: int, db: AsyncSession
) -> WorkflowTemplate:
    """
    Retrieve a `WorkflowTemplate` by ID.

    Args:
        template_id:
        db:

    Returns:
        The `WorkflowTemplate` object.

    Raises:
        HTTPException(status_code=404_NOT_FOUND):
            If no template exists with the given ID.
    """
    template = await db.get(WorkflowTemplate, template_id)
    if template is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"WorkflowTemplate[{template_id}] not found.",
        )
    return template

_get_template_read_access(*, user_id, template_id, db) async

Retrieve a WorkflowTemplate and ensure the user has read access.

Access is granted if the user is the owner of the template or belongs to the template's user group.

PARAMETER DESCRIPTION
user_id

TYPE: int

template_id

TYPE: int

db

TYPE: AsyncSession

RETURNS DESCRIPTION
WorkflowTemplate

The WorkflowTemplate object.

RAISES DESCRIPTION
HTTPException(status_code=404_NOT_FOUND)

If no template exists with the given ID.

HTTPException(status_code=403_FORBIDDEN)

If the user has not read access.

Source code in fractal_server/app/routes/api/v2/_aux_functions_templates.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
async def _get_template_read_access(
    *, user_id: int, template_id: int, db: AsyncSession
) -> WorkflowTemplate:
    """
    Retrieve a `WorkflowTemplate` and ensure the user has read access.

    Access is granted if the user is the owner of the template or belongs to
    the template's user group.

    Args:
        user_id:
        template_id:
        db:

    Returns:
        The `WorkflowTemplate` object.

    Raises:
        HTTPException(status_code=404_NOT_FOUND):
            If no template exists with the given ID.
        HTTPException(status_code=403_FORBIDDEN):
            If the user has not read access.
    """
    template = await _get_template_or_404(template_id=template_id, db=db)
    if template.user_id != user_id:
        link = await db.get(LinkUserGroup, (template.user_group_id, user_id))
        if link is None:
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
                detail=(
                    "You are not authorized to view "
                    f"WorkflowTemplate[{template_id}]."
                ),
            )
    return template