From 338da0208cb6171ed3128cddf5f7d3b7076f9f6f Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 27 Feb 2026 16:49:56 -0600 Subject: [PATCH] fix: prevent delete if animal type is still being referenced --- src/wuttafarm/db/model/asset_animal.py | 9 +++++++++ src/wuttafarm/web/views/animals.py | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/wuttafarm/db/model/asset_animal.py b/src/wuttafarm/db/model/asset_animal.py index 768b0f9..cf88b83 100644 --- a/src/wuttafarm/db/model/asset_animal.py +++ b/src/wuttafarm/db/model/asset_animal.py @@ -80,6 +80,14 @@ class AnimalType(model.Base): """, ) + animal_assets = orm.relationship( + "AnimalAsset", + doc=""" + List of animal assets of this type. + """, + back_populates="animal_type", + ) + def __str__(self): return self.name or "" @@ -103,6 +111,7 @@ class AnimalAsset(AssetMixin, EggMixin, model.Base): doc=""" Reference to the animal type. """, + back_populates="animal_assets", ) birthdate = sa.Column( diff --git a/src/wuttafarm/web/views/animals.py b/src/wuttafarm/web/views/animals.py index 1c9fdfe..fc4c646 100644 --- a/src/wuttafarm/web/views/animals.py +++ b/src/wuttafarm/web/views/animals.py @@ -106,6 +106,19 @@ class AnimalTypeView(AssetTypeMasterView): return buttons + def delete(self): + animal_type = self.get_instance() + + if animal_type.animal_assets: + self.request.session.flash( + "Cannot delete animal type which is still referenced by animal assets.", + "warning", + ) + url = self.get_action_url("view", animal_type) + return self.redirect(self.request.get_referrer(default=url)) + + return super().delete() + def get_row_grid_data(self, animal_type): model = self.app.model session = self.Session()