31 lines
1000 B
Python
31 lines
1000 B
Python
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.lab.db_guard import retry_locked
|
|
from app.lab.models import (
|
|
LabAnimalProfile,
|
|
LabProfileNorm,
|
|
LabRecipeRation,
|
|
)
|
|
@retry_locked
|
|
def delete_animal_profile(profile_id: str, user_id: str = "system") -> dict:
|
|
pid = (profile_id or "").strip()
|
|
if not pid:
|
|
raise LookupError("Профиль не найден")
|
|
|
|
profile = LabAnimalProfile.query.filter_by(id=pid, is_deleted=False).first()
|
|
if profile is None:
|
|
raise LookupError("Профиль не найден")
|
|
|
|
LabProfileNorm.query.filter_by(profile_id=profile.id).delete(synchronize_session=False)
|
|
|
|
rations = LabRecipeRation.query.filter_by(animal_profile_id=profile.id, is_deleted=False).all()
|
|
for header in rations:
|
|
header.animal_profile_id = None
|
|
header.updated_by = user_id
|
|
|
|
profile.updated_by = user_id
|
|
profile.soft_delete(user_id)
|
|
db.session.commit()
|
|
return {"id": profile.id, "deleted": True}
|