Skip to content

Commit

Permalink
Rename owner id to user id (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyurtsev authored Mar 20, 2024
1 parent a27077c commit 7eff8a4
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 44 deletions.
4 changes: 2 additions & 2 deletions backend/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ def __repr__(self) -> str:


def validate_extractor_owner(
session: Session, extractor_id: UUID, owner_id: UUID
session: Session, extractor_id: UUID, user_id: UUID
) -> Extractor:
"""Validate the extractor id."""
extractor = (
session.query(Extractor).filter_by(uuid=extractor_id, owner_id=owner_id).first()
session.query(Extractor).filter_by(uuid=extractor_id, owner_id=user_id).first()
)
if extractor is None:
return False
Expand Down
12 changes: 6 additions & 6 deletions backend/server/api/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def create(
create_request: CreateExample,
*,
session: Session = Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> CreateExampleResponse:
"""Endpoint to create an example."""
if not validate_extractor_owner(session, create_request["extractor_id"], owner_id):
if not validate_extractor_owner(session, create_request["extractor_id"], user_id):
raise HTTPException(status_code=404, detail="Extractor not found for owner.")

instance = Example(
Expand All @@ -59,10 +59,10 @@ def list(
limit: int = 10,
offset: int = 0,
session=Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> List[Any]:
"""Endpoint to get all examples."""
if not validate_extractor_owner(session, extractor_id, owner_id):
if not validate_extractor_owner(session, extractor_id, user_id):
raise HTTPException(status_code=404, detail="Extractor not found for owner.")
return (
session.query(Example)
Expand All @@ -76,11 +76,11 @@ def list(

@router.delete("/{uuid}")
def delete(
uuid: UUID, *, session: Session = Depends(get_session), owner_id: UUID = Cookie(...)
uuid: UUID, *, session: Session = Depends(get_session), user_id: UUID = Cookie(...)
) -> None:
"""Endpoint to delete an example."""
extractor_id = session.query(Example).filter_by(uuid=str(uuid)).first().extractor_id
if not validate_extractor_owner(session, extractor_id, owner_id):
if not validate_extractor_owner(session, extractor_id, user_id):
raise HTTPException(status_code=404, detail="Extractor not found for owner.")
session.query(Example).filter_by(uuid=str(uuid)).delete()
session.commit()
6 changes: 2 additions & 4 deletions backend/server/api/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def extract_using_existing_extractor(
file: Optional[UploadFile] = File(None),
model_name: Optional[str] = Form(DEFAULT_MODEL),
session: Session = Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> ExtractResponse:
"""Endpoint that is used with an existing extractor.
Expand All @@ -38,9 +38,7 @@ async def extract_using_existing_extractor(
raise HTTPException(status_code=422, detail="No text or file provided.")

extractor = (
session.query(Extractor)
.filter_by(uuid=extractor_id, owner_id=owner_id)
.scalar()
session.query(Extractor).filter_by(uuid=extractor_id, owner_id=user_id).scalar()
)
if extractor is None:
raise HTTPException(status_code=404, detail="Extractor not found for owner.")
Expand Down
20 changes: 10 additions & 10 deletions backend/server/api/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def share(
uuid: UUID,
*,
session: Session = Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> ShareExtractorResponse:
"""Endpoint to share an extractor.
Expand All @@ -74,7 +74,7 @@ def share(
Returns:
The UUID for the shared extractor.
"""
if not validate_extractor_owner(session, uuid, owner_id):
if not validate_extractor_owner(session, uuid, user_id):
raise HTTPException(status_code=404, detail="Extractor not found for owner.")
# Check if the extractor is already shared
shared_extractor = (
Expand Down Expand Up @@ -110,13 +110,13 @@ def create(
create_request: CreateExtractor,
*,
session: Session = Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> CreateExtractorResponse:
"""Endpoint to create an extractor."""

instance = Extractor(
name=create_request.name,
owner_id=owner_id,
owner_id=user_id,
schema=create_request.json_schema,
description=create_request.description,
instruction=create_request.instruction,
Expand All @@ -128,11 +128,11 @@ def create(

@router.get("/{uuid}")
def get(
uuid: UUID, *, session: Session = Depends(get_session), owner_id: UUID = Cookie(...)
uuid: UUID, *, session: Session = Depends(get_session), user_id: UUID = Cookie(...)
) -> Dict[str, Any]:
"""Endpoint to get an extractor."""
extractor = (
session.query(Extractor).filter_by(uuid=str(uuid), owner_id=owner_id).scalar()
session.query(Extractor).filter_by(uuid=str(uuid), owner_id=user_id).scalar()
)
if extractor is None:
raise HTTPException(status_code=404, detail="Extractor not found for owner.")
Expand All @@ -151,12 +151,12 @@ def list(
limit: int = 10,
offset: int = 0,
session=Depends(get_session),
owner_id: UUID = Cookie(...),
user_id: UUID = Cookie(...),
) -> List[Any]:
"""Endpoint to get all extractors."""
return (
session.query(Extractor)
.filter_by(owner_id=owner_id)
.filter_by(owner_id=user_id)
.limit(limit)
.offset(offset)
.all()
Expand All @@ -165,8 +165,8 @@ def list(

@router.delete("/{uuid}")
def delete(
uuid: UUID, *, session: Session = Depends(get_session), owner_id: UUID = Cookie(...)
uuid: UUID, *, session: Session = Depends(get_session), user_id: UUID = Cookie(...)
) -> None:
"""Endpoint to delete an extractor."""
session.query(Extractor).filter_by(uuid=str(uuid), owner_id=owner_id).delete()
session.query(Extractor).filter_by(uuid=str(uuid), owner_id=user_id).delete()
session.commit()
16 changes: 8 additions & 8 deletions backend/tests/unit_tests/api/test_api_defining_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ async def test_extractors_api() -> None:
"""This will test a few of the extractors API endpoints."""
# First verify that the database is empty
async with get_async_client() as client:
owner_id = str(uuid.uuid4())
cookies = {"owner_id": owner_id}
user_id = str(uuid.uuid4())
cookies = {"user_id": user_id}
response = await client.get("/extractors", cookies=cookies)
assert response.status_code == 200
assert response.json() == []
Expand All @@ -32,7 +32,7 @@ async def test_extractors_api() -> None:
assert len(get_response) == 1

# Check cookies
bad_cookies = {"owner_id": str(uuid.uuid4())}
bad_cookies = {"user_id": str(uuid.uuid4())}
bad_response = await client.get("/extractors", cookies=bad_cookies)
assert bad_response.status_code == 200
assert len(bad_response.json()) == 0
Expand Down Expand Up @@ -84,7 +84,7 @@ async def test_extractors_api() -> None:
assert get_response.json() == []

# Verify that we can create an extractor, including other properties
owner_id = str(uuid.uuid4())
user_id = str(uuid.uuid4())
create_request = {
"name": "my extractor",
"description": "Test Description",
Expand All @@ -100,14 +100,14 @@ async def test_extractors_api() -> None:
response_data = response.json()
assert extractor_uuid == response_data["uuid"]
assert "my extractor" == response_data["name"]
assert "owner_id" not in response_data
assert "user_id" not in response_data


async def test_sharing_extractor() -> None:
"""Test sharing an extractor."""
async with get_async_client() as client:
owner_id = str(uuid.uuid4())
cookies = {"owner_id": owner_id}
user_id = str(uuid.uuid4())
cookies = {"user_id": user_id}
response = await client.get("/extractors", cookies=cookies)
assert response.status_code == 200
assert response.json() == []
Expand Down Expand Up @@ -138,7 +138,7 @@ async def test_sharing_extractor() -> None:
assert response.json()["share_uuid"] == share_uuid

# Check cookies
bad_cookies = {"owner_id": str(uuid.uuid4())}
bad_cookies = {"user_id": str(uuid.uuid4())}
response = await client.post(
f"/extractors/{uuid_str}/share", cookies=bad_cookies
)
Expand Down
6 changes: 3 additions & 3 deletions backend/tests/unit_tests/api/test_api_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async def test_examples_api() -> None:
"""Runs through a set of API calls to test the examples API."""
async with get_async_client() as client:
# First create an extractor
owner_id = str(uuid.uuid4())
cookies = {"owner_id": owner_id}
user_id = str(uuid.uuid4())
cookies = {"user_id": user_id}
create_request = {
"description": "Test Description",
"name": "Test Name",
Expand Down Expand Up @@ -53,7 +53,7 @@ async def test_examples_api() -> None:
example_id = response.json()["uuid"]

# Check cookies
bad_cookies = {"owner_id": str(uuid.uuid4())}
bad_cookies = {"user_id": str(uuid.uuid4())}
response = await client.post(
"/examples", json=create_request, cookies=bad_cookies
)
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/unit_tests/api/test_api_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def mock_embeddings(*args, **kwargs):
async def test_extract_from_file() -> None:
"""Test extract from file API."""
async with get_async_client() as client:
owner_id = str(uuid4())
cookies = {"owner_id": owner_id}
user_id = str(uuid4())
cookies = {"user_id": user_id}
# Test with invalid extractor
extractor_id = UUID(int=1027) # 1027 is a good number.
response = await client.post(
Expand Down Expand Up @@ -131,8 +131,8 @@ async def test_extract_from_file() -> None:


async def test_extract_from_large_file() -> None:
owner_id = str(uuid4())
cookies = {"owner_id": owner_id}
user_id = str(uuid4())
cookies = {"user_id": user_id}
async with get_async_client() as client:
# First create an extractor
create_request = {
Expand Down
6 changes: 3 additions & 3 deletions docs/source/notebooks/earnings_call_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@
" period_duration: int = Field(..., description=\"Duration of period, in months\")\n",
" evidence: str = Field(..., description=\"Verbatim sentence of text where figure was found.\")\n",
"\n",
"owner_id = str(uuid4())\n",
"cookies = {\"owner_id\": owner_id}\n",
"user_id = str(uuid4())\n",
"cookies = {\"user_id\": user_id}\n",
"\n",
"data = {\n",
" \"owner_id\": owner_id,\n",
" \"user_id\": user_id,\n",
" \"description\": \"Financial revenues and other figures.\",\n",
" \"schema\": FinancialData.schema(),\n",
" \"instruction\": (\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/source/notebooks/quick_start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
"outputs": [],
"source": [
"url = \"http://localhost:8000\"\n",
"owner_id = str(uuid4()) # indicates owner for extractor\n",
"cookies = {\"owner_id\": owner_id}"
"user_id = str(uuid4()) # indicates owner for extractor\n",
"cookies = {\"user_id\": user_id}"
]
},
{
Expand Down
3 changes: 1 addition & 2 deletions frontend/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { v4 as uuidv4 } from "uuid";
// Automatically set a 'user_id' cookie if one doesn't exist
// This function will run on every page request, so we can
// guarantee that a 'user_id' cookie will always be set.
/* TODO: Change this to 'user_id' */
const USER_ID_COOKIE_KEY = "owner_id";
const USER_ID_COOKIE_KEY = "user_id";

export function middleware(request: NextRequest) {
const response = NextResponse.next();
Expand Down

0 comments on commit 7eff8a4

Please sign in to comment.