Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Switch community leaderboards to a table
Browse files Browse the repository at this point in the history
This sets us on the road to #3127. I used the table implementation from
team.js as a starting point.
  • Loading branch information
chadwhitacre committed Mar 23, 2015
1 parent ebdb741 commit 4aa3c15
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 183 deletions.
1 change: 1 addition & 0 deletions js/gratipay.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Gratipay.init = function() {
Gratipay.signIn();
Gratipay.signOut();
Gratipay.tips.initSupportGratipay();
Gratipay.table.init();
};

Gratipay.error = function(jqXHR, textStatus, errorThrown) {
Expand Down
40 changes: 40 additions & 0 deletions js/gratipay/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Gratipay.table = (function() {
function init() {
$('table[data-url]').each(initTable);
}

function initTable(i, table) {
var dataUrl = $(table).data('url');
jQuery.get(dataUrl).success(function(data) {
$('.loading-indicator').remove();
var rows = renderRows(data.members);
$('tbody', table).html(rows);
});
}


// Render Rows
// ===========

function renderRows(records) {
nrecords = records.length;
var rows = [];

for (var i=0, len=records.length; i<len; i++) {
var record = records[i];
rows.push(Gratipay.jsonml(
[ 'tr'
, ['td', {'class': 'n'}, nrecords-i]
, ['td', ['a', {'href': '/'+record.username+'/'}, record.username]]
]
));
}
return rows;
}


// Export
// ======

return {init: init};
})();
186 changes: 10 additions & 176 deletions www/for/%slug/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -39,92 +39,6 @@ if community is None:

# Set the page title based on the communities name.
title = community.name


# Run queries for listings.
# =========================

new_participants = query_cache.all("""
-- new participants on community page

SELECT username
, id
, claimed_time
, avatar_url
, number
FROM participants p
JOIN current_community_members cc ON cc.participant = p.id
WHERE p.claimed_time IS NOT null
AND p.is_suspicious IS NOT true
AND cc.slug = %s AND cc.is_member
ORDER BY p.claimed_time DESC
LIMIT %s
OFFSET %s

""", (community.slug, limit, offset))

givers = query_cache.all("""
-- top givers on community page

SELECT username
, id
, anonymous_giving AS anonymous
, giving AS amount
, avatar_url
, number
FROM participants p
JOIN current_community_members cc ON cc.participant = p.id AND cc.slug = %s
WHERE is_suspicious IS NOT true
AND giving > 0
AND cc.is_member
ORDER BY giving DESC
LIMIT %s
OFFSET %s

""", (community.slug, limit, offset))

# XXX I'm nearly positive that one or both of givers and receivers can contain
# orphan accounts. See https://github.com/gratipay/gratipay.com/issues/650

receivers = query_cache.all("""
-- top receivers on community page

SELECT username
, id
, anonymous_receiving AS anonymous
, receiving AS amount
, avatar_url
, number
FROM participants p
JOIN current_community_members cc ON cc.participant = p.id AND cc.slug = %s
WHERE is_suspicious IS NOT true
AND receiving > 0
AND cc.is_member
ORDER BY receiving DESC
LIMIT %s
OFFSET %s

""", (community.slug, limit, offset))


# Fetch statements

ids = tuple(p.id for p in chain(new_participants, givers, receivers))
if ids:
statements = website.db.all("""
SELECT DISTINCT ON (participant) participant, content
FROM statements s
JOIN enumerate(%s) langs ON langs.value = s.lang
WHERE participant IN %s
ORDER BY participant, langs.rank ASC
""", (request.accept_langs, ids))
statements = {s.participant: s.content for s in statements}
else:
statements = {}

for p in chain(new_participants, givers, receivers):
p.__dict__['statement'] = statements.get(p.id)

[---]
{% from 'templates/avatar-url.html' import avatar_url with context %}

Expand Down Expand Up @@ -167,101 +81,21 @@ for p in chain(new_participants, givers, receivers):
{% endblock %}

{% block content %}

{% if community.nmembers == 0 %}
<h2>{{ _("You're the first one here!") }}</h2>
<p>{{ _("Bring the {0} community to Gratipay to find like-minded people to give to.",
community.name) }}</p>
{% else %}

<div id="leaderboard">
<div class="people">
<h2>{{ _("New Members") }}</h2>
<ul class="clearfix">
{% for i, participant in enumerate(new_participants, start=1) %}
<li{% if i > LUXURY %} class="luxury"{% endif %}>
<a href="/{{ participant.username }}/" class="mini-user"
data-tip="{{ excerpt_intro(participant.statement) }}">
<span class="inner">
<span class="avatar"
style="background-image: url('{{ avatar_url(participant) }}')">
</span>
<span class="age">{{ to_age(participant.claimed_time, add_direction=True) }}</span>
<span class="name">{{ participant.username }}</span>
</span>
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="people">
<h2>{{ _("Top Givers") }}</h2>
<ul class="clearfix">
{% for i, giver in enumerate(givers, start=1) %}
<li{% if i > LUXURY %} class="luxury"{% endif %}>
{% if giver.anonymous and not user.ADMIN %}
<span class="mini-user">
<span class="inner">
<span class="avatar">
</span>
<span class="money">${{ format_money(giver.amount) }}</span>
<span class="name">{{ _("Anonymous") }}</span>
</span>
</span>
{% else %}
<a href="/{{ giver.username }}/"
class="mini-user{{ ' anonymous' if giver.anonymous else '' }}"
data-tip="{{ excerpt_intro(giver.statement) }}">
<span class="inner">
<span class="avatar" style="background-image: url('{{ avatar_url(giver) }}')">
</span>
<span class="money">${{ format_money(giver.amount) }}</span>
<span class="name">{{ giver.username }}</span>
</span>
</a>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
<div class="people">
<h2>{{ _("Top Receivers") }}</h2>
<ul class="clearfix">
{% for i, receiver in enumerate(receivers, start=1) %}
<li{% if i > LUXURY %} class="luxury"{% endif %}>
{% if receiver.anonymous and not user.ADMIN %}
<span class="mini-user">
<span class="inner">
<span class="avatar">
</span>
<span class="money">${{ format_money(receiver.amount) }}</span>
<span class="name">{{ _("Anonymous") }}</span>
</span>
</span>
{% else %}
<a href="/{{ receiver.username }}/"
class="mini-user{{ ' anonymous' if receiver.anonymous else '' }}"
data-tip="{{ excerpt_intro(receiver.statement) }}">
<span class="inner">
<span class="avatar"
style="background-image: url('{{ avatar_url(receiver) }}')">
</span>
<span class="money">${{ format_money(receiver.amount) }}</span>
<span class="name">{{ receiver.username }}</span>
</span>
</a>
{% endif %}
</li>
{% endfor %}
</ul>
</div>

</div>

<div class="more-container">
<a href="/for/{{ community.slug }}/?limit={{ limit + 12 }}"><button class="selected">{{ _("More") }}</button></a>
</div>

<table class="table" data-url="./index.json">
<thead>
<tr>
<th></th>
<th>{{ _('Username') }}</th>
</tr>
</thead>
<tfoot><tr><td colspan=6><div class="loading-indicator"/></td></tr></foot>
<tbody></tbody>
</table>
{% endif %}
{% endblock %}

Expand Down
8 changes: 1 addition & 7 deletions www/for/%slug/index.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ if not community:
# Community not found
raise Response(404)

try:
limit = min(int(request.qs.get('limit', 10)), 100)
offset = int(request.qs.get('offset', 0))
except ValueError:
raise Response(400)

community_members = community.get_members(limit=limit, offset=offset)
community_members = community.get_members()

[---] application/json via json_dump
{ "name": community.name
Expand Down

0 comments on commit 4aa3c15

Please sign in to comment.