Skip to content

project

create_project(project, user=Depends(current_active_user), db=Depends(get_async_db)) async

Create new poject

Source code in fractal_server/app/routes/api/v2/project.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@router.post("/project/", response_model=ProjectReadV2, status_code=201)
async def create_project(
    project: ProjectCreateV2,
    user: UserOAuth = Depends(current_active_user),
    db: AsyncSession = Depends(get_async_db),
) -> Optional[ProjectReadV2]:
    """
    Create new poject
    """

    # Check that there is no project with the same user and name
    await _check_project_exists(
        project_name=project.name, user_id=user.id, db=db
    )

    db_project = ProjectV2(**project.dict())
    db_project.user_list.append(user)

    db.add(db_project)
    await db.commit()
    await db.refresh(db_project)
    await db.close()

    return db_project

delete_project(project_id, user=Depends(current_active_user), db=Depends(get_async_db)) async

Delete project

Source code in fractal_server/app/routes/api/v2/project.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
@router.delete("/project/{project_id}/", status_code=204)
async def delete_project(
    project_id: int,
    user: UserOAuth = Depends(current_active_user),
    db: AsyncSession = Depends(get_async_db),
) -> Response:
    """
    Delete project
    """

    project = await _get_project_check_owner(
        project_id=project_id, user_id=user.id, db=db
    )
    logger = set_logger(__name__)

    # Fail if there exist jobs that are submitted and in relation with the
    # current project.
    stm = _get_submitted_jobs_statement().where(JobV2.project_id == project_id)
    res = await db.execute(stm)
    jobs = res.scalars().all()
    if jobs:
        string_ids = str([job.id for job in jobs])[1:-1]
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=(
                f"Cannot delete project {project.id} because it "
                f"is linked to active job(s) {string_ids}."
            ),
        )

    # Cascade operations

    # Workflows
    stm = select(WorkflowV2).where(WorkflowV2.project_id == project_id)
    res = await db.execute(stm)
    workflows = res.scalars().all()
    logger.info("Start of cascade operations on Workflows.")
    for wf in workflows:
        # Cascade operations: set foreign-keys to null for jobs which are in
        # relationship with the current workflow
        stm = select(JobV2).where(JobV2.workflow_id == wf.id)
        res = await db.execute(stm)
        jobs = res.scalars().all()
        for job in jobs:
            logger.info(f"Setting Job[{job.id}].workflow_id to None.")
            job.workflow_id = None
        # Delete workflow
        logger.info(f"Adding Workflow[{wf.id}] to deletion.")
        await db.delete(wf)
    logger.info("End of cascade operations on Workflows.")

    # Dataset
    stm = select(DatasetV2).where(DatasetV2.project_id == project_id)
    res = await db.execute(stm)
    datasets = res.scalars().all()
    logger.info("Start of cascade operations on Datasets.")
    for ds in datasets:
        # Cascade operations: set foreign-keys to null for jobs which are in
        # relationship with the current dataset
        stm = select(JobV2).where(JobV2.dataset_id == ds.id)
        res = await db.execute(stm)
        jobs = res.scalars().all()
        for job in jobs:
            logger.info(f"Setting Job[{job.id}].dataset_id to None.")
            job.dataset_id = None
        # Delete dataset
        logger.info(f"Adding Dataset[{ds.id}] to deletion.")
        await db.delete(ds)
    logger.info("End of cascade operations on Datasets.")

    # Job
    logger.info("Start of cascade operations on Jobs.")
    stm = select(JobV2).where(JobV2.project_id == project_id)
    res = await db.execute(stm)
    jobs = res.scalars().all()
    for job in jobs:
        logger.info(f"Setting Job[{job.id}].project_id to None.")
        job.project_id = None
    logger.info("End of cascade operations on Jobs.")

    logger.info(f"Adding Project[{project.id}] to deletion.")
    await db.delete(project)

    logger.info("Committing changes to db...")
    await db.commit()

    logger.info("Everything  has been deleted correctly.")
    reset_logger_handlers(logger)

    return Response(status_code=status.HTTP_204_NO_CONTENT)

get_list_project(user=Depends(current_active_user), db=Depends(get_async_db)) async

Return list of projects user is member of

Source code in fractal_server/app/routes/api/v2/project.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@router.get("/project/", response_model=list[ProjectReadV2])
async def get_list_project(
    user: UserOAuth = Depends(current_active_user),
    db: AsyncSession = Depends(get_async_db),
) -> list[ProjectV2]:
    """
    Return list of projects user is member of
    """
    stm = (
        select(ProjectV2)
        .join(LinkUserProjectV2)
        .where(LinkUserProjectV2.user_id == user.id)
    )
    res = await db.execute(stm)
    project_list = res.scalars().all()
    await db.close()
    return project_list

read_project(project_id, user=Depends(current_active_user), db=Depends(get_async_db)) async

Return info on an existing project

Source code in fractal_server/app/routes/api/v2/project.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@router.get("/project/{project_id}/", response_model=ProjectReadV2)
async def read_project(
    project_id: int,
    user: UserOAuth = Depends(current_active_user),
    db: AsyncSession = Depends(get_async_db),
) -> Optional[ProjectReadV2]:
    """
    Return info on an existing project
    """
    project = await _get_project_check_owner(
        project_id=project_id, user_id=user.id, db=db
    )
    await db.close()
    return project