refactor(splits): delete attachment files when their rows are deleted

A post_delete receiver replaces the file deletes in each view, so the files
also go when an attachment is deleted through a cascade from the Django admin
or the organizer's account, and are only removed once the row is.
This commit is contained in:
Herculino Trotta committed 2026-12-05 15:34:41 -03:00
1 parent d9142f6872
commit 1298e38772
4 files changed
+33 -13

No files matched your search

-6
View File
@@ -63,8 +63,6 @@ class SplitEventViewSet(SplitsQuerysetMixin, viewsets.ModelViewSet):
def perform_destroy(self, instance):
_require_organizer(instance, self.request.user)
for attachment in instance.attachments.all():
attachment.file.delete(save=False)
instance.delete()
@extend_schema(
@@ -218,8 +216,6 @@ class SplitExpenseViewSet(SplitsQuerysetMixin, viewsets.ModelViewSet):
created_by_id=instance.created_by_participant_id,
primary_participant_id=instance.paid_by_id,
)
for attachment in instance.attachments.all():
attachment.file.delete(save=False)
instance.delete()
@@ -261,6 +257,4 @@ class SplitPaymentViewSet(SplitsQuerysetMixin, viewsets.ModelViewSet):
created_by_id=instance.created_by_participant_id,
primary_participant_id=instance.from_participant_id,
)
for attachment in instance.attachments.all():
attachment.file.delete(save=False)
instance.delete()
+10
View File
@@ -9,6 +9,8 @@ from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@@ -448,6 +450,14 @@ class SplitAttachment(models.Model):
)
# Covers every way an attachment goes, including cascades from its expense,
# payment, event or the event's organizer
@receiver(post_delete, sender=SplitAttachment)
def delete_split_attachment_file(sender, instance, **kwargs):
if instance.file.name:
instance.file.storage.delete(instance.file.name)
class SplitTransactionLink(models.Model):
class Kind(models.TextChoices):
EXPENSE = "EXPENSE", _("Expense paid")
+23
View File
@@ -745,3 +745,26 @@ class EventDeleteTests(SplitTestBase):
with self.subTest(participant=participant.name):
with self.assertRaises(RestrictedError):
participant.delete()
def test_deleting_the_organizer_removes_receipt_files(self):
storage = SplitAttachment._meta.get_field("file").storage
orig_location = storage._location
storage._location = tempfile.mkdtemp()
storage.__dict__.pop("location", None)
try:
exp = SplitExpense.objects.create(
event=self.event, paid_by=self.p_bob, description="Hotel", amount=Decimal("100")
)
receipt = SplitAttachment.objects.create(
event=self.event, expense=exp, original_name="receipt.txt",
file=SimpleUploadedFile("receipt.txt", b"hotel"),
)
# As the Django admin does: the event goes with its organizer
self.creator.delete()
self.assertFalse(SplitEvent.objects.exists())
self.assertFalse(storage.exists(receipt.file.name))
finally:
storage._location = orig_location
storage.__dict__.pop("location", None)
-7
View File
@@ -496,8 +496,6 @@ def event_delete(request, event_id):
actor = get_wygiwyh_actor(request, event_id)
if not actor.is_creator:
raise PermissionDenied
for att in actor.event.attachments.all():
att.file.delete(save=False)
actor.event.delete()
messages.success(request, _("Event deleted successfully"))
return HttpResponse(
@@ -807,8 +805,6 @@ def _handle_expense_delete(request, actor: SplitActor, expense_id: int):
):
raise PermissionDenied
for att in expense.attachments.all():
att.file.delete(save=False)
expense.delete()
messages.success(request, _("Expense deleted successfully"))
return HttpResponse(
@@ -921,8 +917,6 @@ def _handle_payment_delete(request, actor: SplitActor, payment_id: int):
):
raise PermissionDenied
for att in payment.attachments.all():
att.file.delete(save=False)
payment.delete()
messages.success(request, _("Payment deleted successfully"))
return HttpResponse(
@@ -969,7 +963,6 @@ def _handle_attachment_delete(request, actor: SplitActor, attachment_id):
if not can_delete:
raise PermissionDenied
att.file.delete(save=False)
att.delete()
messages.success(request, _("Attachment deleted"))
return HttpResponse(