felicity-lims/felicity/api/rest/api_v1/endpoints/setup.py

81 lines
2.1 KiB
Python
Raw Normal View History

2022-04-22 03:33:54 +08:00
import logging
2022-11-06 20:09:44 +08:00
from typing import Any, Optional
2023-04-10 20:23:31 +08:00
from apps.setup import models, schemas
2022-04-22 03:33:54 +08:00
from fastapi import APIRouter
2023-04-10 20:23:31 +08:00
from init import default_setup, requisite_setup
2022-11-06 20:09:44 +08:00
from pydantic import BaseModel
2022-04-22 03:33:54 +08:00
router = APIRouter()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class InstallResponse(BaseModel):
laboratory: Optional[schemas.Laboratory]
installed: bool
2023-04-25 22:42:39 +08:00
message: str | None
2022-04-22 03:33:54 +08:00
class LabNameIn(BaseModel):
2022-11-06 20:09:44 +08:00
name: str
2022-04-22 03:33:54 +08:00
class SetupResponse(BaseModel):
success: bool
2023-04-25 22:42:39 +08:00
message: str | None
2022-04-22 03:33:54 +08:00
@router.get("/installation", response_model=Optional[InstallResponse])
async def laboratory_lookup() -> Any:
"""
Retrieve instance of installed laboratory
"""
laboratory = await models.Laboratory.get_by_setup_name("felicity")
return {
"laboratory": laboratory,
"installed": True if laboratory else False,
"message": "" if laboratory else "Laboratory installation required",
}
@router.post("/installation", response_model=InstallResponse)
async def register_laboratory(*, form: LabNameIn) -> Any:
"""
2022-12-28 05:29:14 +08:00
Install a laboratory and initialise departments example post: curl -X POST
http://localhost:8000/api/v1/setup/installation -d '{"name":"Felicity Lims"}' -H "Content-Type: application/json"
2022-04-22 03:33:54 +08:00
"""
try:
2022-12-28 05:29:14 +08:00
await requisite_setup(form.name)
except Exception as e:
return {
"laboratory": None,
"installed": False,
2022-12-28 05:29:14 +08:00
"message": f"Failed to load requisite setup: {e}",
}
2022-04-22 03:33:54 +08:00
laboratory = await models.Laboratory.get_by_setup_name("felicity")
2023-03-19 23:21:32 +08:00
return {
"laboratory": laboratory,
"installed": True,
"message": "installation success",
}
2022-04-22 03:33:54 +08:00
@router.post("/load-default-setup", response_model=SetupResponse)
async def load_setup_data() -> Any:
"""
Run initial setup to load setup data
"""
try:
2022-12-28 05:29:14 +08:00
await default_setup()
2022-04-22 03:33:54 +08:00
except Exception as e:
2022-11-06 20:09:44 +08:00
return {"success": False, "message": f"Failed to load setup data: {e}"}
2022-04-22 03:33:54 +08:00
2022-11-06 20:09:44 +08:00
return {"success": True, "message": "Setup data was successfully loaded"}
2022-04-22 03:33:54 +08:00
# TODO: UPLOAD SETUP DATA VIA CSV