felicity-lims/felicity/lims/seeds/setup_inventory.py

66 lines
1.9 KiB
Python
Raw Normal View History

2024-05-29 14:00:08 +08:00
import logging
2024-07-24 04:30:01 +08:00
from felicity.apps.inventory import schemas
from felicity.apps.inventory.services import HazardService, StockCategoryService, StockUnitService
2024-05-29 14:00:08 +08:00
from felicity.core.config import get_settings
from .data import get_seeds
settings = get_settings()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def seed_stock_units():
logger.info("Setting up stock units .....")
2024-07-24 04:30:01 +08:00
stock_unit_service = StockUnitService()
2024-05-29 14:00:08 +08:00
data = get_seeds("inventory")
stock_units = data.get("units", [])
for _unit in stock_units:
2024-07-24 04:30:01 +08:00
stock_unit = await stock_unit_service.get(name=_unit.get("name"))
2024-05-29 14:00:08 +08:00
if not stock_unit:
su_in = schemas.StockUnitCreate(
name=_unit.get("name"),
description=_unit.get("description"),
synonym="",
)
2024-07-24 04:30:01 +08:00
await stock_unit_service.create(su_in)
2024-05-29 14:00:08 +08:00
async def seed_stock_categories():
logger.info("Setting up stock categories .....")
2024-07-24 04:30:01 +08:00
stock_category_service = StockCategoryService()
2024-05-29 14:00:08 +08:00
data = get_seeds("inventory")
stock_categories = data.get("categories", [])
for _cat in stock_categories:
2024-07-24 04:30:01 +08:00
stock_category = await stock_category_service.get(name=_cat.get("name"))
2024-05-29 14:00:08 +08:00
if not stock_category:
cu_in = schemas.StockCategoryCreate(
name=_cat.get("name"),
description=_cat.get("description"),
)
2024-07-24 04:30:01 +08:00
await stock_category_service.create(cu_in)
2024-05-29 14:00:08 +08:00
async def seed_stock_hazards():
logger.info("Setting up stock hazards .....")
2024-07-24 04:30:01 +08:00
hazard_service = HazardService()
2024-05-29 14:00:08 +08:00
data = get_seeds("inventory")
hazards = data.get("hazards", [])
for _hzd in hazards:
2024-07-24 04:30:01 +08:00
hazard = await hazard_service.get(name=_hzd.get("name"))
2024-05-29 14:00:08 +08:00
if not hazard:
sh_in = schemas.HazardCreate(
name=_hzd.get("name"),
description=_hzd.get("description"),
)
2024-07-24 04:30:01 +08:00
await hazard_service.create(sh_in)