Add additional, model-imposed failsafe for (main) character deletion.

This commit is contained in:
Casper V. Kristensen 2020-09-24 21:18:43 +02:00
parent 0bf64182f5
commit 41eff3bec2
Signed by: caspervk
GPG key ID: 289CA03790535054

View file

@ -123,5 +123,12 @@ class Character(models.Model):
def clean(self):
self.name = self.name.capitalize()
def delete(self, *args, **kwargs):
if self.user.main == self:
# Sanity check: this is already checked by the profile and admin forms which allow character deletion, but
# we check it again to avoid unintentionally cascading the delete to the entire user.
raise ValidationError("Can't delete user's main character.")
return super().delete(*args, **kwargs)
def __str__(self):
return self.name