2022-03-29 00:01:12 +00:00
|
|
|
"""
|
|
|
|
/device endpoints.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
|
|
|
from ..db import Device, DeviceCreate, DeviceRead, User
|
2022-03-29 23:36:23 +00:00
|
|
|
from ._common import (Responses, get_current_user, get_device_by_id,
|
|
|
|
get_user_by_name)
|
2022-03-29 00:01:12 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/device", tags=["device"])
|
|
|
|
|
|
|
|
|
|
|
|
@router.post(
|
2022-03-29 23:36:23 +00:00
|
|
|
"/{user_name}",
|
2022-03-29 00:01:12 +00:00
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: Responses.OK,
|
|
|
|
status.HTTP_400_BAD_REQUEST: Responses.NOT_INSTALLED,
|
|
|
|
status.HTTP_401_UNAUTHORIZED: Responses.NEEDS_USER,
|
2022-03-30 02:02:45 +00:00
|
|
|
status.HTTP_403_FORBIDDEN: Responses.NEEDS_PERMISSION,
|
2022-03-29 00:01:12 +00:00
|
|
|
status.HTTP_404_NOT_FOUND: Responses.ENTRY_DOESNT_EXIST,
|
|
|
|
status.HTTP_409_CONFLICT: Responses.ENTRY_EXISTS,
|
|
|
|
},
|
|
|
|
response_model=DeviceRead,
|
|
|
|
)
|
|
|
|
async def add_device(
|
|
|
|
device: DeviceCreate,
|
2022-03-29 23:36:23 +00:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
owner: User = Depends(get_user_by_name),
|
2022-03-29 00:01:12 +00:00
|
|
|
) -> Device:
|
|
|
|
"""
|
|
|
|
POST ./: Create a new device in the database.
|
|
|
|
"""
|
|
|
|
|
2022-03-29 23:36:23 +00:00
|
|
|
# check permission
|
2022-03-30 01:51:43 +00:00
|
|
|
if not current_user.can_create(Device, owner):
|
2022-03-29 23:36:23 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
2022-03-31 16:48:52 +00:00
|
|
|
# fail if owner doesn't exist
|
|
|
|
if owner is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
2022-03-29 00:01:12 +00:00
|
|
|
# create the new device
|
|
|
|
new_device = Device.create(
|
2022-03-31 16:48:52 +00:00
|
|
|
owner=owner,
|
2022-03-29 00:01:12 +00:00
|
|
|
device=device,
|
|
|
|
)
|
|
|
|
|
|
|
|
# fail if creation was unsuccessful
|
|
|
|
if new_device is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
# return the created device on success
|
|
|
|
return new_device
|
2022-03-29 15:56:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.delete(
|
|
|
|
"/{device_id}",
|
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: Responses.OK,
|
|
|
|
status.HTTP_400_BAD_REQUEST: Responses.NOT_INSTALLED,
|
|
|
|
status.HTTP_401_UNAUTHORIZED: Responses.NEEDS_USER,
|
2022-03-30 02:07:22 +00:00
|
|
|
status.HTTP_403_FORBIDDEN: Responses.NEEDS_PERMISSION,
|
2022-03-29 15:56:12 +00:00
|
|
|
status.HTTP_404_NOT_FOUND: Responses.ENTRY_DOESNT_EXIST,
|
|
|
|
},
|
|
|
|
response_model=User,
|
|
|
|
)
|
|
|
|
async def remove_device(
|
2022-03-29 23:36:23 +00:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
device: Device = Depends(get_device_by_id),
|
2022-03-29 15:56:12 +00:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
DELETE ./{device_id}: Remove a device from the database.
|
|
|
|
"""
|
|
|
|
|
2022-03-29 23:36:23 +00:00
|
|
|
# check permission
|
2022-03-30 01:51:43 +00:00
|
|
|
if not current_user.can_edit(device):
|
2022-03-29 23:36:23 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
2022-03-29 15:56:12 +00:00
|
|
|
# delete device
|
|
|
|
device.delete()
|