fix(splits): let events with expenses or payments be deleted

Deleting a split event failed with a server error as soon as it had an
expense or a payment, from the event page and from the API alike
(DELETE /api/splits/events/{id}/). Deleting the event cascades to its
participants, and the expenses, shares and payments pointing at them
used on_delete=PROTECT, which refuses even when those rows are being
deleted with the same event. Only empty events could be removed, and an
admin couldn't delete a user who organized one.

- The participant foreign keys on expenses, shares and payments now use
  RESTRICT. A participant with expenses or payments still can't be
  deleted on their own (the people screen and the API already check
  this first), but they now go along with their event. The migration
  only changes Django's state; the database is untouched.
- Deleting an event also removes its attachments' files, as deleting an
  expense or a payment already does, so receipts aren't left behind in
  the private media folder.
This commit is contained in:
Asish Kumar committed 2026-12-05 05:51:12 +05:30
1 parent 6d56958deb
commit d9142f6872
6 files changed
+166 -5

No files matched your search

+68
View File
@@ -5,6 +5,7 @@ from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import RestrictedError
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
@@ -677,3 +678,70 @@ class PaymentPermissionAndReportTests(SplitTestBase):
)
resp = self.client.get(reverse("split_external_event_report", kwargs={"share_token": token}))
self.assertContains(resp, "Beach Trip")
class EventDeleteTests(SplitTestBase):
def test_organizer_deletes_event_with_expenses_and_payments(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")
)
SplitExpenseShare.objects.create(expense=exp, participant=self.p_alice, amount=Decimal("50"))
SplitExpenseShare.objects.create(expense=exp, participant=self.p_bob, amount=Decimal("50"))
payment = SplitPayment.objects.create(
event=self.event, from_participant=self.p_alice, to_participant=self.p_bob,
amount=Decimal("50"),
)
receipt = SplitAttachment.objects.create(
event=self.event, expense=exp, original_name="receipt.txt",
file=SimpleUploadedFile("receipt.txt", b"hotel"),
)
transaction = Transaction.objects.create(
owner=self.creator, account=self.account, type=Transaction.Type.EXPENSE,
amount=Decimal("50"), date=timezone.localdate(), description="Pay Bob",
)
SplitTransactionLink.objects.create(
event=self.event, user=self.creator, transaction=transaction,
kind=SplitTransactionLink.Kind.PAYMENT_OUT, payment=payment,
)
self.client.force_login(self.creator)
resp = self.client.delete(
reverse("split_event_delete", kwargs={"event_id": self.event.id}),
HTTP_HX_REQUEST="true",
)
self.assertEqual(resp.status_code, 204)
self.assertFalse(SplitEvent.objects.exists())
self.assertFalse(SplitParticipant.objects.exists())
self.assertFalse(SplitExpense.objects.exists())
self.assertFalse(SplitPayment.objects.exists())
self.assertFalse(SplitAttachment.objects.exists())
self.assertFalse(storage.exists(receipt.file.name))
# The user's own transaction stays; only its link to the event goes
self.assertFalse(SplitTransactionLink.objects.exists())
self.assertTrue(Transaction.objects.filter(id=transaction.id).exists())
finally:
storage._location = orig_location
storage.__dict__.pop("location", None)
def test_participant_with_contributions_cant_be_deleted_alone(self):
dana = SplitParticipant.objects.create(event=self.event, name="Dana", is_external=True)
exp = SplitExpense.objects.create(
event=self.event, paid_by=self.p_alice, description="Fuel", amount=Decimal("30")
)
SplitExpenseShare.objects.create(expense=exp, participant=self.p_bob, amount=Decimal("30"))
SplitPayment.objects.create(
event=self.event, from_participant=self.p_charlie, to_participant=dana,
amount=Decimal("5"),
)
# Payer, share, payment sender and payment recipient
for participant in (self.p_alice, self.p_bob, self.p_charlie, dana):
with self.subTest(participant=participant.name):
with self.assertRaises(RestrictedError):
participant.delete()