def notify_participants(self):
        log("Notifying participants.")
        ts_start, ts_end = self.ts_start, self.ts_end
        exchanges = self.db.all("""
            SELECT e.id, amount, fee, note, status, p.*::participants AS participant
              FROM exchanges e
              JOIN participants p ON e.participant = p.username
             WHERE "timestamp" >= %(ts_start)s
               AND "timestamp" < %(ts_end)s
               AND amount > 0
               AND p.notify_charge > 0
        """, locals())
        for e in exchanges:
            if e.status not in ('failed', 'succeeded'):
                log('exchange %s has an unexpected status: %s' % (e.id, e.status))
                continue
            i = 1 if e.status == 'failed' else 2
            p = e.participant
            if p.notify_charge & i == 0:
                continue
            username = p.username
            nteams, top_team = self.db.one("""
                WITH tippees AS (
                         SELECT t.slug, amount
                           FROM ( SELECT DISTINCT ON (team) team, amount
                                    FROM payment_instructions
                                   WHERE mtime < %(ts_start)s
                                     AND participant = %(username)s
                                ORDER BY team, mtime DESC
                                ) s
                           JOIN teams t ON s.team = t.slug
                           JOIN participants p ON t.owner = p.username
                          WHERE s.amount > 0
                            AND t.is_approved IS true
                            AND t.is_closed IS NOT true
                            AND (SELECT count(*)
                                   FROM current_exchange_routes er
                                  WHERE er.participant = p.id
                                    AND network = 'paypal'
                                    AND error = ''
                                ) > 0
                     )
                SELECT ( SELECT count(*) FROM tippees ) AS nteams
                     , ( SELECT slug
                           FROM tippees
                       ORDER BY amount DESC
                          LIMIT 1
                       ) AS top_team
            """, locals())
            p.queue_email(
                'charge_'+e.status,
                exchange=dict(id=e.id, amount=e.amount, fee=e.fee, note=e.note),
                nteams=nteams,
                top_team=top_team,
            )
dataset/ETHPy150Open gratipay/gratipay.com/gratipay/billing/payday.py/Payday.notify_participants/original