Skip to content

Commit

Permalink
refactor: remove API v1 workspaces exception captures (#4897)
Browse files Browse the repository at this point in the history
# Description

This PR remove all exception capture blocks for API v1 workspaces
handler.

Refs #4871 

**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [x] Refactor (change restructuring the codebase without changing
functionality)
- [ ] Improvement (change adding some improvement to an existing
functionality)
- [ ] Documentation update

**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- [x] Improving and passing existent tests.

**Checklist**

- [ ] I added relevant documentation
- [ ] follows the style guidelines of this project
- [ ] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [ ] I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
  • Loading branch information
jfcalvo authored May 31, 2024
1 parent 6aa7aad commit b6b53af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
36 changes: 9 additions & 27 deletions argilla-server/src/argilla_server/apis/v1/handlers/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

from argilla_server.contexts import accounts, datasets
from argilla_server.database import get_async_db
from argilla_server.errors import EntityAlreadyExistsError
from argilla_server.errors.future import NotUniqueError
from argilla_server.errors.future import NotFoundError, UnprocessableEntityError
from argilla_server.models import User, Workspace
from argilla_server.policies import WorkspacePolicyV1, WorkspaceUserPolicyV1, authorize
from argilla_server.schemas.v1.users import User as UserSchema
Expand Down Expand Up @@ -48,9 +47,7 @@ async def get_workspace(
):
await authorize(current_user, WorkspacePolicyV1.get(workspace_id))

workspace = await Workspace.get_or_raise(db, workspace_id)

return workspace
return await Workspace.get_or_raise(db, workspace_id)


@router.post("/workspaces", status_code=status.HTTP_201_CREATED, response_model=WorkspaceSchema)
Expand All @@ -62,12 +59,7 @@ async def create_workspace(
):
await authorize(current_user, WorkspacePolicyV1.create)

try:
workspace = await accounts.create_workspace(db, workspace_create.dict())
except NotUniqueError as e:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e))

return workspace
return await accounts.create_workspace(db, workspace_create.dict())


@router.delete("/workspaces/{workspace_id}", response_model=WorkspaceSchema)
Expand All @@ -82,12 +74,7 @@ async def delete_workspace(

workspace = await Workspace.get_or_raise(db, workspace_id)

if await datasets.list_datasets_by_workspace_id(db, workspace_id):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Cannot delete the workspace {workspace_id}. This workspace has some feedback datasets linked",
)

# TODO: Once we move to v2.0 remove the following check because it's only required by old datasets
if await datasets_service.list(current_user, workspaces=[workspace.name]):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
Expand Down Expand Up @@ -141,17 +128,12 @@ async def create_workspace_user(

workspace = await Workspace.get_or_raise(db, workspace_id)

user = await User.get(db, workspace_user_create.user_id)
if user is None:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"User with id `{workspace_user_create.user_id}` not found",
)

try:
workspace_user = await accounts.create_workspace_user(db, {"workspace_id": workspace.id, "user_id": user.id})
except NotUniqueError as e:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e))
user = await User.get_or_raise(db, workspace_user_create.user_id)
except NotFoundError as e:
raise UnprocessableEntityError(e.message)

workspace_user = await accounts.create_workspace_user(db, {"workspace_id": workspace.id, "user_id": user.id})

return workspace_user.user

Expand Down
10 changes: 8 additions & 2 deletions argilla-server/src/argilla_server/contexts/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session, selectinload

from argilla_server.contexts import datasets
from argilla_server.enums import UserRole
from argilla_server.errors.future import NotUniqueError, UnprocessableEntityError
from argilla_server.models import User, Workspace, WorkspaceUser
Expand All @@ -42,7 +43,7 @@ async def create_workspace_user(db: AsyncSession, workspace_user_attrs: dict) ->
workspace_id = workspace_user_attrs["workspace_id"]
user_id = workspace_user_attrs["user_id"]

if (await get_workspace_user_by_workspace_id_and_user_id(db, workspace_id, user_id)) is not None:
if await get_workspace_user_by_workspace_id_and_user_id(db, workspace_id, user_id) is not None:
raise NotUniqueError(f"Workspace user with workspace_id `{workspace_id}` and user_id `{user_id}` is not unique")

workspace_user = await WorkspaceUser.create(db, workspace_id=workspace_id, user_id=user_id)
Expand Down Expand Up @@ -78,13 +79,18 @@ async def list_workspaces_by_user_id(db: AsyncSession, user_id: UUID) -> List[Wo


async def create_workspace(db: AsyncSession, workspace_attrs: dict) -> Workspace:
if (await get_workspace_by_name(db, workspace_attrs["name"])) is not None:
if await get_workspace_by_name(db, workspace_attrs["name"]) is not None:
raise NotUniqueError(f"Workspace name `{workspace_attrs['name']}` is not unique")

return await Workspace.create(db, name=workspace_attrs["name"])


async def delete_workspace(db: AsyncSession, workspace: Workspace):
if await datasets.list_datasets_by_workspace_id(db, workspace.id):
raise NotUniqueError(
f"Cannot delete the workspace {workspace.id}. This workspace has some feedback datasets linked"
)

return await workspace.delete(db)


Expand Down

0 comments on commit b6b53af

Please sign in to comment.