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

+34 -1
View File
@@ -1,12 +1,20 @@
import json
import tempfile
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
from rest_framework.test import APITestCase
from apps.currencies.models import Currency
from apps.splits.models import SplitEvent, SplitExpense, SplitParticipant, SplitPayment
from apps.splits.models import (
SplitAttachment,
SplitEvent,
SplitExpense,
SplitParticipant,
SplitPayment,
)
from apps.users.models import APIToken
User = get_user_model()
@@ -103,6 +111,31 @@ class SplitsAPITests(APITestCase):
# The organizer can't leave
self.assertEqual(self._post(f"/api/splits/events/{self.event.id}/leave/", {}, user=self.alice).status_code, 403)
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:
expense = self._post("/api/splits/expenses/", {"event": self.event.id, "description": "x", "amount": "9", "date": "2026-11-20", "paid_by": self.p_ana}).json()
self._post("/api/splits/payments/", {"event": self.event.id, "amount": "3", "date": "2026-11-20", "from_participant": self.p_bob, "to_participant": self.p_ana})
receipt = SplitAttachment.objects.create(
event=self.event, expense_id=expense["id"], original_name="r.txt",
file=SimpleUploadedFile("r.txt", b"x"),
)
# Only the organizer can delete it
self.client.force_authenticate(self.bob)
self.assertEqual(self.client.delete(f"/api/splits/events/{self.event.id}/").status_code, 403)
self.client.force_authenticate(self.alice)
self.assertEqual(self.client.delete(f"/api/splits/events/{self.event.id}/").status_code, 204)
self.assertFalse(SplitEvent.objects.exists())
self.assertFalse(SplitExpense.objects.exists())
self.assertFalse(SplitPayment.objects.exists())
self.assertFalse(storage.exists(receipt.file.name))
finally:
storage._location = orig_location
storage.__dict__.pop("location", None)
@override_settings(MCP_ENABLED=True, PUBLIC_BASE_URL="https://money.example")
def test_reachable_over_mcp(self):
_token, raw = APIToken.objects.create_token(user=self.alice, name="mcp")