felicity-lims/felicity/lims/seeds/setup_instruments.py

53 lines
2.1 KiB
Python
Raw Normal View History

2024-06-24 00:27:04 +08:00
import logging
from felicity.apps.instrument import schemas
2024-07-28 03:52:31 +08:00
from felicity.apps.instrument.services import (InstrumentService,
InstrumentTypeService,
LaboratoryInstrumentService,
MethodService)
2024-06-24 00:27:04 +08:00
from felicity.core.config import get_settings
2024-07-28 03:52:31 +08:00
2024-06-24 00:27:04 +08:00
from .data import get_seeds
settings = get_settings()
2024-07-24 04:30:01 +08:00
2024-06-24 00:27:04 +08:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def seed_instrument_categories() -> None:
logger.info("Setting up instrument categories .....")
2024-07-24 04:30:01 +08:00
instrument_type_service = InstrumentTypeService()
instrument_service = InstrumentService()
method_service = MethodService()
laboratory_instrument_service = LaboratoryInstrumentService()
2024-06-24 00:27:04 +08:00
data = get_seeds("instrument")
for inst_type in data.get("categories"):
2024-07-24 04:30:01 +08:00
instrument_type = await instrument_type_service.get(name=inst_type)
2024-06-24 00:27:04 +08:00
if not instrument_type:
2024-07-28 03:52:31 +08:00
inst_type_in = schemas.InstrumentTypeCreate(
name=inst_type, description=inst_type
)
2024-07-24 04:30:01 +08:00
await instrument_type_service.create(inst_type_in)
2024-06-24 00:27:04 +08:00
for _meth in data.get("methods"):
2024-07-24 04:30:01 +08:00
if not (await method_service.get(name=_meth)):
2024-06-24 00:27:04 +08:00
method_in = schemas.MethodCreate(name=_meth, description="", keyword="")
2024-07-24 04:30:01 +08:00
await method_service.create(method_in)
2024-06-24 00:27:04 +08:00
2024-06-28 03:47:02 +08:00
for _inst in data.get("instruments"):
2024-07-24 04:30:01 +08:00
if not (await instrument_service.get(name=_inst)):
2024-06-28 03:47:02 +08:00
inst_in = schemas.InstrumentCreate(name=_inst, description="", keyword="")
2024-07-24 04:30:01 +08:00
instrument = await instrument_service.create(inst_in)
2024-06-28 03:47:02 +08:00
if _inst == "Manual":
2024-07-24 04:30:01 +08:00
if not (await laboratory_instrument_service.get(lab_name=_inst)):
2024-06-28 03:47:02 +08:00
lab_inst_in = schemas.LaboratoryInstrumentCreate(
2024-07-28 03:52:31 +08:00
instrument_uid=instrument.uid,
lab_name=_inst,
serial_number="Man-001",
2024-06-28 03:47:02 +08:00
)
2024-07-24 04:30:01 +08:00
await laboratory_instrument_service.create(lab_inst_in)