Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort corporate donors by donation amount within each bracket #1867

Open
jacobian opened this issue Jan 9, 2025 · 2 comments
Open

Sort corporate donors by donation amount within each bracket #1867

jacobian opened this issue Jan 9, 2025 · 2 comments

Comments

@jacobian
Copy link
Member

jacobian commented Jan 9, 2025

I'm looking at the corporate donors on https://www.djangoproject.com/fundraising/. The intent there is that, within each donation bracket, donors are meant to be sorted by donation amount, then alphabetically (e.g. so if a Silver donor donated $7,000, the'd be sorted above one who donated $5,000, even though both are in the Silver bracket).

However, responding to a question from a donor, I think I've discovered two problems:

  1. The code actually sorts by total amount donated all time, not the current donation level:
    class CorporateMemberManager(models.Manager):
    def for_public_display(self):
    objs = (
    self.get_queryset()
    .filter(
    invoice__expiration_date__gte=timezone_today(),
    )
    .annotate(donated_amount=models.Sum("invoice__amount"))
    )
    return objs.order_by("-donated_amount", "display_name")
    def by_membership_level(self):
    members_by_type = defaultdict(list)
    members = self.for_public_display()
    for member in members:
    key = MEMBERSHIP_TO_KEY[member.membership_level]
    members_by_type[key].append(member)
    return members_by_type
  2. And it doesn't appear to be working. I don't want to publish donation amounts, but anyone with access to the live site data should check out Bronze sponsors. The sort there appears purely alphabetical, but there are large differences in donation amounts there. (If whoever ends up debugging this doesn't have access to live donation data, feel free to contact me privately and I'll get you that access to debug.)

(1) is arguably by design and not a bug, so I'm gonna discuss this with the board and the fundraising team to double check that my understanding of the intent (sort by current donation amount, not total amount) is correct.

But (2) is definitely a bug -- the code clear intends to sort by donation amount, but the sort seems to be lost somewhere somehow.

@jefftriplett
Copy link
Member

I always mess annotate up because it doesn't fit my brain. I don't see anything obvious, but may there is a default that's being weird that a filter kwargs might help work around. <-- totally grasping.

@adamzap
Copy link
Member

adamzap commented Jan 10, 2025

I'm having trouble reproducing the issue locally, and I don't have access to production data. @jacobian, could it be that some of the Invoices in question are past their expiration_date and aren't being summed? That's the only thing I can think of. CorporateMemberManager.for_public_display is called by the code you referenced, and it filters out expired Invoices here.

The order_by logic seems fine to me because the call at the end of for_public_display should overwrite the model's default ordering and persist through the dictionary construction until the template is rendered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants