1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 13:15:18 +02:00

fix(models): ensure associated Attachment files are deleted and their filesystem cleaned up on Adventure deletion

This commit is contained in:
Sean Morley 2025-06-23 18:44:11 -04:00
parent 07346bf03d
commit bed72e92da

View file

@ -660,6 +660,9 @@ class Adventure(models.Model):
# Delete all associated AdventureImages first to trigger their filesystem cleanup
for image in self.images.all():
image.delete()
# Delete all associated Attachment files first to trigger their filesystem cleanup
for attachment in self.attachments.all():
attachment.delete()
super().delete(*args, **kwargs)
def __str__(self):
@ -861,6 +864,11 @@ class Attachment(models.Model):
adventure = models.ForeignKey(Adventure, related_name='attachments', on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True, blank=True)
def delete(self, *args, **kwargs):
if self.file and os.path.isfile(self.file.path):
os.remove(self.file.path)
super().delete(*args, **kwargs)
def __str__(self):
return self.file.url