Skip to content

Commit

Permalink
aux function
Browse files Browse the repository at this point in the history
  • Loading branch information
ychiucco committed Oct 8, 2024
1 parent fbaa4f4 commit 974bb07
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions fractal_server/app/routes/aux/_task_group.py
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=})."
),
)

0 comments on commit 974bb07

Please sign in to comment.