fix: prevent delete if animal type is still being referenced
This commit is contained in:
parent
ec67340e66
commit
338da0208c
2 changed files with 22 additions and 0 deletions
|
|
@ -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):
|
def __str__(self):
|
||||||
return self.name or ""
|
return self.name or ""
|
||||||
|
|
||||||
|
|
@ -103,6 +111,7 @@ class AnimalAsset(AssetMixin, EggMixin, model.Base):
|
||||||
doc="""
|
doc="""
|
||||||
Reference to the animal type.
|
Reference to the animal type.
|
||||||
""",
|
""",
|
||||||
|
back_populates="animal_assets",
|
||||||
)
|
)
|
||||||
|
|
||||||
birthdate = sa.Column(
|
birthdate = sa.Column(
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,19 @@ class AnimalTypeView(AssetTypeMasterView):
|
||||||
|
|
||||||
return buttons
|
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):
|
def get_row_grid_data(self, animal_type):
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
session = self.Session()
|
session = self.Session()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue