Files
2013-03-24 17:48:36 -04:00

73 lines
2.2 KiB
JSON

"""This is a horribly WET duplicate of www/index.html.
"""
from gittip import db
^L
try:
limit = min(int(qs.get('limit', 10)), 100)
offset = int(qs.get('offset', 0))
except ValueError:
raise Response(400)
givers = list(db.fetchall("""
SELECT tipper AS participant_id, anonymous, sum(amount) AS amount
FROM ( SELECT DISTINCT ON (tipper, tippee)
amount
, tipper
FROM tips
JOIN participants p ON p.id = tipper
JOIN participants p2 ON p2.id = tippee
JOIN elsewhere ON elsewhere.participant_id = tippee
WHERE p.last_bill_result = ''
AND p.is_suspicious IS NOT true
AND p2.claimed_time IS NOT NULL
AND elsewhere.is_locked = false
ORDER BY tipper, tippee, mtime DESC
) AS foo
JOIN participants p ON p.id = tipper
WHERE is_suspicious IS NOT true
GROUP BY tipper, anonymous
ORDER BY amount DESC
LIMIT %s
OFFSET %s
""", (limit, offset)))
# XXX I'm nearly positive that one or both of givers and receivers can contain
# orphan accounts. See https://github.com/gittip/www.gittip.com/issues/650
receivers = list(db.fetchall("""
SELECT tippee AS participant_id, sum(amount) AS amount
FROM ( SELECT DISTINCT ON (tipper, tippee)
amount
, tippee
FROM tips
JOIN participants p ON p.id = tipper
JOIN elsewhere ON elsewhere.participant_id = tippee
WHERE last_bill_result = ''
AND elsewhere.is_locked = false
AND is_suspicious IS NOT true
AND claimed_time IS NOT null
ORDER BY tipper, tippee, mtime DESC
) AS foo
JOIN participants p ON p.id = tippee
WHERE is_suspicious IS NOT true
GROUP BY tippee, claimed_time
ORDER BY amount DESC
LIMIT %s
OFFSET %s
""", (limit, offset)))
# anonymize
for giver in givers:
anon = giver.pop('anonymous')
if anon:
giver['participant_id'] = None
response.body = { "givers": givers
, "receivers": receivers
}