1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import registration
import itertools, datetime

def p2user(p): return p.sale.user.pk

email = registration.models.Email.objects.get(pk = 70)

for contact, total_amount in ((registration.models.User.objects.get(
    pk = uid).contacts.all().order_by('pk')[0],
  sum(c.amount for c in g))
 for uid, g in itertools.groupby(
     sorted(registration.models.Payment.objects.exclude(
         sale__creditcardinfo = None).filter(
                sale__pk__gte = 334).exclude(
                sale__pk__in = [509, 493, 464]).exclude(
                sale__canceled = True).filter(   # <= This is what I forgot
                received = False,
                due_date__lte = datetime.date(2014, 9, 26)),
            key = p2user),
     key = p2user)):
    email.send([contact], total_amount = total_amount)