-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Optional | ||
|
||
from fastapi import HTTPException | ||
from fastapi import status | ||
from sqlmodel import select | ||
|
||
from fractal_server.app.db import AsyncSession | ||
from fractal_server.app.models.v2 import TaskGroupV2 | ||
|
||
|
||
async def _verify_non_duplication_constraints( | ||
db: AsyncSession, | ||
pkg_name: str, | ||
user_id: int, | ||
version: Optional[str], | ||
user_group_id: Optional[int] = None, | ||
): | ||
stm = ( | ||
select(TaskGroupV2) | ||
.where(TaskGroupV2.pkg_name == pkg_name) | ||
.where(TaskGroupV2.version == version) # FIXME test with None | ||
.where(TaskGroupV2.user_id == user_id) | ||
) | ||
res = await db.execute(stm) | ||
duplicate = res.scalars().all() | ||
if duplicate: | ||
raise HTTPException( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
detail=( | ||
"There is already a TaskGroupV2 with " | ||
f"({pkg_name=}, {version=}, {user_id=})." | ||
), | ||
) | ||
|
||
if user_group_id is not None: | ||
stm = ( | ||
select(TaskGroupV2) | ||
.where(TaskGroupV2.pkg_name == pkg_name) | ||
.where(TaskGroupV2.version == version) | ||
.where(TaskGroupV2.user_group_id == user_group_id) | ||
) | ||
res = await db.execute(stm) | ||
duplicate = res.scalars().all() | ||
if duplicate: | ||
raise HTTPException( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
detail=( | ||
"There is already a TaskGroupV2 with " | ||
f"({pkg_name=}, {version=}, {user_group_id=})." | ||
), | ||
) |