Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow project editor to update data #616

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions backend/zeno_backend/routers/sdk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""FastAPI server endpoints for the Zeno SDK."""
import uuid
from urllib import parse

import pyarrow as pa
from amplitude import BaseEvent
Expand All @@ -20,6 +21,7 @@
from zeno_backend.classes.project import Project
from zeno_backend.database import delete, insert, select, update
from zeno_backend.database.util import match_instance_view
from zeno_backend.util import user_project_editor

# Bump only for breaking changes
MIN_CLIENT_VERSION = "0.1.9"
Expand Down Expand Up @@ -86,6 +88,40 @@ async def verify_api_key(self, api_key: str) -> bool:
router = APIRouter(tags=["zeno"], dependencies=[Depends(APIKeyBearer())])


@router.get("/project-by-name/{owner_name}/{project_name}")
async def get_project_by_name(
owner_name: str, project_name: str, api_key=Depends(APIKeyBearer())
):
"""Get a project by it's name and owner name.

Args:
owner_name (str): name of the project's owner.
project_name (str): name of the project.
api_key (_type_, optional): API key of the user making the request.
Defaults to Depends(APIKeyBearer()).

Returns:
str: uuid of the requested project.
"""
uuid = await select.project_uuid(
parse.unquote(owner_name), parse.unquote(project_name)
)
if uuid is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=(
"ERROR: Project "
+ project_name
+ " for user "
+ owner_name
+ " does not exist."
),
)
user = await select.user_by_api_key(api_key)
await user_project_editor(uuid, user)
return uuid


@router.post("/project", status_code=200, response_model=Project)
async def create_project(
project: Project, response: Response, api_key=Depends(APIKeyBearer())
Expand Down Expand Up @@ -142,6 +178,7 @@ async def upload_dataset_schema(
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down Expand Up @@ -188,6 +225,7 @@ async def upload_dataset(
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down Expand Up @@ -237,6 +275,7 @@ async def upload_system_schema(
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down Expand Up @@ -285,6 +324,7 @@ async def upload_system(
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down Expand Up @@ -315,23 +355,31 @@ async def upload_system(


@router.delete("/system/{project_uuid}/{system_name}")
async def delete_system(project_uuid: str, system_name: str):
async def delete_system(
project_uuid: str, system_name: str, api_key=Depends(APIKeyBearer())
):
"""Delete a system from a Zeno project.

Args:
project_uuid (str): the UUID of the project to delete the system from.
system_name (str): the name of the system.
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
await delete.system(project_uuid, system_name)


@router.delete("/systems/{project_uuid}")
async def delete_all_systems(project_uuid: str):
async def delete_all_systems(project_uuid: str, api_key=Depends(APIKeyBearer())):
"""Delete all systems from a Zeno project.

Args:
project_uuid (str): the UUID of the project to delete systems from.
api_key (str, optional): API key.
"""
user = await select.user_by_api_key(api_key)
await user_project_editor(project_uuid, user)
await delete.systems(project_uuid)


Expand Down
13 changes: 13 additions & 0 deletions backend/zeno_backend/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ async def project_editor(project_uuid: str, request: Request):
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized",
)
await user_project_editor(project_uuid, user)


async def user_project_editor(project_uuid: str, user: User):
"""Check if a user is a project's editor.

Args:
project_uuid (str): the project to check.
user (User): the user to check for.

Raises:
HTTPException: if the user is not an editor.
"""
available_project_ids = [
project.uuid for project in await select.projects(user) if project.editor
]
Expand Down
Loading