Files
Chad Whitacre 7c22562ab0 Fix up images on homepage; #789
I fixed this by wrapping the three SQL query result sets in a generator
that creates a new ORM-based Participant object. Then we use the
get_img_src method on that object, which is the same one we use on
profile pages.
2013-03-28 21:56:12 -04:00

214 lines
7.3 KiB
HTML

from aspen import Response
from aspen.utils import to_age
from gittip import db, AMOUNTS
from gittip.elsewhere import github
from gittip.models.participant import Participant
def _wrap(raw):
"""This is a step on the way to ORM-ifying this simplate.
"""
for rec in raw:
out = Participant.query.get(rec['id'])
if 'amount' in rec:
out.amount = rec['amount']
yield out
def _to_age(participant):
# XXX I can't believe I'm doing this. Evolve aspen.utils.to_age!
age = to_age(participant.claimed_time, fmt_past="%(age)s")
age = age.replace('just a moment', 'just now')
age = age.replace('an ', '1 ').replace('a ', '1 ')
words = ('zero', 'one', 'two','three', 'four', 'five', 'six', 'seven',
'eight', 'nine')
for i, word in enumerate(words):
age = age.replace(word, str(i))
return age.replace(' ', ' <span class="unit">') + "</span>"
^L
try:
limit = min(int(qs.get('limit', 10)), 100)
offset = int(qs.get('offset', 0))
except ValueError:
raise Response(400)
new_participants = _wrap(db.fetchall("""
SELECT id, claimed_time FROM (
SELECT DISTINCT ON (p.id)
p.id
, claimed_time
FROM participants p
JOIN elsewhere e
ON p.id = participant_id
WHERE claimed_time IS NOT null
AND is_suspicious IS NOT true
) AS foo
ORDER BY claimed_time DESC
LIMIT %s
OFFSET %s
""", (limit, offset)))
givers = _wrap(db.fetchall("""
SELECT tipper AS 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 = _wrap(db.fetchall("""
SELECT tippee AS id, claimed_time, 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)))
^L
{% extends templates/base.html %}
{% block head %}
<script>$(document).ready(Gittip.initJumpToPerson);</script>
{% end %}
{% block heading %}
<h2 class="top"><span>A Weekly Gift Exchange</span></h2>
<h1>
<span>Inspiring Generosity</span>
</h1>
<h2 class="bottom">
<span class="button-container">
<a href="/about/"><button class="selected">About</button></a>
</span>
</h2>
{% end %}
{% block box %}
<div class="as-content">
<h1>Who inspires you?</h1>
<form id="jump">
<span class="luxury">Enter a</span> <select>
<option value="twitter">Twitter</option>
<option value="github">GitHub</option>
<option value="bitbucket">Bitbucket</option>
</select> username:
<span class="nowrap">
<input placeholder="" />
<button type="submit">Go</button>
</span>
</form>
</div>
{% end %}
{% block page %}
<div id="leaderboard">
<div class="new-participants">
<h2>New Participants</h2>
<ul class="group">
{% for i, participant in enumerate(new_participants, start=1) %}
<li{% if i > 6 %} class="luxury"{% end %}>
<a href="/{{ participant.id }}/" class="mini-user">
<span class="inner">
<span class="avatar"
style="background-image:
url('{{ participant.get_img_src() }}')">
<span class="rank">{{ i }}</span>
</span>
<span class="age">{{ _to_age(participant) }}</span>
<span class="name">{{ participant.id }}</span>
</span>
</a>
</li>
{% end %}
</ul>
</div>
<div class="top-givers">
<h2>Top Givers</h2>
<ul class="group">
{% for i, giver in enumerate(givers, start=1) %}
<li{% if i > 6 %} class="luxury"{% end %}>
{% if giver.anonymous and not user.is_admin %}
<span class="mini-user">
<span class="inner">
<span class="avatar">
<span class="rank">{{ i }}</span>
</span>
<span class="money">${{ giver.amount }}</span>
<span class="name">???</span>
</span>
</span>
{% else %}
<a href="/{{ giver.id }}/"
class="mini-user{{ ' anonymous' if giver.anonymous else '' }}">
<span class="inner">
<span class="avatar"
style="background-image: url('{{ giver.get_img_src() }}')">
<span class="rank">{{ i }}</span>
</span>
<span class="money">${{ giver.amount }}</span>
<span class="name">{{ giver.id }}</span>
</span>
</a>
{% end %}
</li>
{% end %}
</ul>
</div>
<div class="top-receivers">
<h2>Top Receivers</h2>
<ul class="group">
{% for i, receiver in enumerate(receivers, start=1) %}
<li{% if i > 6 %} class="luxury"{% end %}>
<a href="/{{ receiver.id }}/" class="mini-user">
<span class="inner">
<span class="avatar"
style="background-image: url('{{ receiver.get_img_src() }}')">
<span class="rank">{{ i }}</span>
</span>
<span class="money">${{ receiver.amount }}</span>
<span class="name">{{ receiver.id }}</span>
</span>
</a>
</li>
{% end %}
</ul>
</div>
</div>
{% end %}