Skip to content

Commit

Permalink
Update SharesView.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AnniePacheco authored Nov 5, 2024
1 parent 58acdbf commit f1c3e9c
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions backend/api/SharesView.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from backend.managers.ResourcesManager import ResourcesManager
from backend.pagination import parse_pagination_params
from datetime import datetime, timezone
from backend.schemas import ShareCreateSchema

class SharesView:
def __init__(self):
Expand All @@ -13,7 +14,7 @@ def __init__(self):
async def get(self, id: str):
share = await self.slm.retrieve_share(id)
if share is None:
return JSONResponse(headers={"error": "Share not found"}, status_code=404)
return JSONResponse({"error": "Share not found"}, status_code=404)
return JSONResponse(share.model_dump(), status_code=200)

async def post(self, body: dict):
Expand All @@ -32,21 +33,19 @@ async def post(self, body: dict):
is_revoked=False)
return JSONResponse(new_share.model_dump(), status_code=201, headers={'Location': f'{api_base_url}/shares/{new_share.id}'})

async def put(self, id: str, body: dict):
expiration_dt = None
async def put(self, id: str, body: ShareCreateSchema):
if 'expiration_dt' in body and body['expiration_dt'] is not None:
expiration_dt = datetime.fromisoformat(body['expiration_dt']).astimezone(tz=timezone.utc)
user_id = None
body['expiration_dt'] = expiration_dt
if 'user_id' in body and body['user_id']:
user_id = body['user_id']
updated_share = await self.slm.update_share(id,
resource_id=body['resource_id'],
user_id=user_id,
expiration_dt=expiration_dt,
is_revoked=body['is_revoked'])
valid = await self.slm.validate_assistant_user_id(body['resource_id'], body['user_id'])
if valid is not None:
return JSONResponse({"error": valid}, status_code=400)
await self.slm.update_share(id, body)
updated_share = await self.slm.retrieve_share(id)
if updated_share is None:
return JSONResponse({"error": "Share not found"}, status_code=404)
return JSONResponse(updated_share.model_dump(), status_code=200)
return JSONResponse(updated_share.dict(), status_code=200)

async def delete(self, id: str):
success = await self.slm.delete_share(id)
Expand All @@ -61,9 +60,14 @@ async def search(self, filter: str = None, range: str = None, sort: str = None):

offset, limit, sort_by, sort_order, filters = result

shares, total_count = await self.slm.retrieve_shares(limit=limit, offset=offset, sort_by=sort_by, sort_order=sort_order, filters=filters)
shares, total_count = await self.slm.retrieve_shares(limit=limit,
offset=offset,
sort_by=sort_by,
sort_order=sort_order,
filters=filters
)
headers = {
'X-Total-Count': str(total_count),
'Content-Range': f'shares {offset}-{offset + len(shares) - 1}/{total_count}'
}
return JSONResponse([share.model_dump() for share in shares], status_code=200, headers=headers)
return JSONResponse([share.model_dump() for share in shares], status_code=200, headers=headers)

0 comments on commit f1c3e9c

Please sign in to comment.