mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-05 22:46:38 +01:00
Merge pull request #712 from officialasishkumar/fix/split-event-delete
fix(splits): let events with expenses or payments be deleted
This commit is contained in:
6 files changed
+195
-14
No files matched your search
@@ -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")
|
||||
|
||||
@@ -216,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()
|
||||
|
||||
|
||||
@@ -259,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()
|
||||
@@ -0,0 +1,54 @@
|
||||
# Generated by Django 5.2.17 on 2026-12-05 00:17
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("splits", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="splitexpense",
|
||||
name="paid_by",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.RESTRICT,
|
||||
related_name="expenses_paid",
|
||||
to="splits.splitparticipant",
|
||||
verbose_name="Paid by",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="splitexpenseshare",
|
||||
name="participant",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.RESTRICT,
|
||||
related_name="expense_shares",
|
||||
to="splits.splitparticipant",
|
||||
verbose_name="Participant",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="splitpayment",
|
||||
name="from_participant",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.RESTRICT,
|
||||
related_name="payments_sent",
|
||||
to="splits.splitparticipant",
|
||||
verbose_name="From",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="splitpayment",
|
||||
name="to_participant",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.RESTRICT,
|
||||
related_name="payments_received",
|
||||
to="splits.splitparticipant",
|
||||
verbose_name="To",
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -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 _
|
||||
|
||||
@@ -209,9 +211,11 @@ class SplitExpense(models.Model):
|
||||
related_name="expenses",
|
||||
verbose_name=_("Event"),
|
||||
)
|
||||
# RESTRICT, here and on the other links to participants: someone with
|
||||
# expenses or payments can't be deleted alone, but goes with the event
|
||||
paid_by = models.ForeignKey(
|
||||
SplitParticipant,
|
||||
on_delete=models.PROTECT,
|
||||
on_delete=models.RESTRICT,
|
||||
related_name="expenses_paid",
|
||||
verbose_name=_("Paid by"),
|
||||
)
|
||||
@@ -267,7 +271,7 @@ class SplitExpenseShare(models.Model):
|
||||
)
|
||||
participant = models.ForeignKey(
|
||||
SplitParticipant,
|
||||
on_delete=models.PROTECT,
|
||||
on_delete=models.RESTRICT,
|
||||
related_name="expense_shares",
|
||||
verbose_name=_("Participant"),
|
||||
)
|
||||
@@ -317,13 +321,13 @@ class SplitPayment(models.Model):
|
||||
)
|
||||
from_participant = models.ForeignKey(
|
||||
SplitParticipant,
|
||||
on_delete=models.PROTECT,
|
||||
on_delete=models.RESTRICT,
|
||||
related_name="payments_sent",
|
||||
verbose_name=_("From"),
|
||||
)
|
||||
to_participant = models.ForeignKey(
|
||||
SplitParticipant,
|
||||
on_delete=models.PROTECT,
|
||||
on_delete=models.RESTRICT,
|
||||
related_name="payments_received",
|
||||
verbose_name=_("To"),
|
||||
)
|
||||
@@ -446,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")
|
||||
|
||||
@@ -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
|
||||
@@ -739,3 +740,93 @@ 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()
|
||||
|
||||
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)
|
||||
@@ -805,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(
|
||||
@@ -919,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(
|
||||
@@ -967,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(
|
||||
|
||||
Reference in new issue
Block a user