Skip to content

Commit

Permalink
Sorted past payments on donation management page
Browse files Browse the repository at this point in the history
Thanks to Alex Gómez for the bug report
  • Loading branch information
bmispelon committed Nov 5, 2024
1 parent e995213 commit 1729f82
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 17 additions & 2 deletions fundraising/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
from datetime import datetime
from operator import attrgetter
from unittest.mock import patch

import stripe
Expand Down Expand Up @@ -124,14 +126,18 @@ def setUpTestData(cls):
subscription_amount=5,
)
cls.payment1 = cls.donation1.payment_set.create(
amount="5", stripe_charge_id="c1"
amount="5",
stripe_charge_id="c1",
date=datetime(2023, 1, 1),
)
cls.donation2 = cls.hero.donation_set.create(
interval="yearly",
subscription_amount=10,
)
cls.payment2 = cls.donation2.payment_set.create(
amount="10", stripe_charge_id="c2"
amount="10",
stripe_charge_id="c2",
date=datetime(2024, 1, 1),
)
cls.url = reverse("fundraising:manage-donations", kwargs={"hero": cls.hero.id})

Expand Down Expand Up @@ -164,6 +170,15 @@ def test_no_past_donations(self):
response = self.client.get(url)
self.assertNotContains(response, self.past_donations_header)

def test_past_donations_sorted(self):
url = reverse("fundraising:manage-donations", kwargs={"hero": self.hero.id})
response = self.client.get(url)
self.assertQuerySetEqual(
response.context["past_payments"],
["c2", "c1"],
transform=attrgetter("stripe_charge_id"),
)


class TestWebhooks(TestCase):
def setUp(self):
Expand Down
6 changes: 4 additions & 2 deletions fundraising/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ def thank_you(request):
def manage_donations(request, hero):
hero = get_object_or_404(DjangoHero, pk=hero)
recurring_donations = hero.donation_set.exclude(stripe_subscription_id="")
past_payments = Payment.objects.filter(donation__donor=hero).select_related(
"donation"
past_payments = (
Payment.objects.filter(donation__donor=hero)
.select_related("donation")
.order_by("-date")
)

ModifyDonationsFormset = modelformset_factory(Donation, form=DonationForm, extra=0)
Expand Down

0 comments on commit 1729f82

Please sign in to comment.