Files
Chad Whitacre 739ec4084f Suggest Bitbucket login on Bitbucket pages; #149
We suggest GitHub on GitHub profile pages, we want Bitbucket here.
2013-03-22 16:12:12 -04:00

299 lines
9.7 KiB
HTML

"""Bitbucket user page on Gittip.
"""
import decimal
import os
import re
import requests
from aspen import json, Response
from gittip import AMOUNTS, CARDINALS, db
from gittip.elsewhere import bitbucket
from gittip.models import Participant
# ========================================================================== ^L
# Try to load from Bitbucket.
# ===========================
user_info = bitbucket.get_user_info(path['username'])
# Try to load from Gittip.
# ========================
# We can only tip Users, not Organizations (or whatever else type can be).
username = user_info['username']
name = user_info.get('display_name')
if not name:
name = username
# XXX Hack to work around our stringification of hstore values, which we do in
# gittip/elsewhere/__init__.py. :-/
is_team = user_info.get("is_team", None) in (True, u"True")
usertype = "unknown type of account"
if is_team is None:
can_tip = False
img_src = "/assets/-/avatar-default.gif"
elif not is_team:
usertype = "user"
account = bitbucket.BitbucketAccount(unicode(username), user_info)
locked = account.is_locked
lock_action = "unlock" if account.is_locked else "lock"
if account.is_claimed:
request.redirect('/%s/' % account.participant_id)
participant = Participant.query.get(account.participant_id)
if not user.ANON:
my_tip = user.get_tip_to(participant.id)
tip_or_pledge = "pledge"
number = participant.get_number_of_backers()
img_src = participant.get_img_src(128)
elif is_team:
usertype = "organization"
bb_url = "https://bitbucket.org/api/2.0/users/%s/members" % username
members = []
resp = requests.get(bb_url)
if resp.status_code is not 200:
raise Response(404, "Bitbucket is currently unavailable")
else:
bb_data = json.loads(resp.text)
for m in bb_data['values']:
members.append(m)
while 'next' in bb_data:
next_url = u"https://bitbucket.org%s" % bb_data['next']
resp = requests.get(next_url)
bb_data = json.loads(resp.text)
for m in bb_data['values']:
members.append(m)
usernames = [member['username'] for member in members]
ON_GITTIP = """\
SELECT elsewhere.user_info -> 'username' AS username
FROM elsewhere
JOIN participants p
ON p.id = elsewhere.participant_id
WHERE elsewhere.platform = 'bitbucket'
AND elsewhere.user_info -> 'username' = any(%s)
AND p.claimed_time IS NOT NULL
"""
_on_gittip = [rec['username'] for rec in db.fetchall(ON_GITTIP, (usernames,))]
on_gittip = []
not_on_gittip = []
for member in members:
seq = on_gittip if member['username'] in _on_gittip else not_on_gittip
tip_to = None if user.ANON else user.get_tip_to(member['username'])
seq.append((member, tip_to))
can_tip = user.ANON
number = len(members)
img_src = user_info['avatar'] + "&s=128"
url = "https://bitbucket.org/%s" % username
title = username
# ========================================================================== ^L
{% extends templates/base.html %}
{% block heading %}<h2 class="top"><span>Bitbucket</span></h2>{% end %}
{% block box %}
<table class="on-elsewhere">
<tr>
<td class="picture">
<img src="{{ img_src }}" />
</td>
<td class="ready">
<h2><a href="{{ url }}">{{ username }}</a> has</h2>
<div class="number">{{ number }}</div>
{% if usertype == "user" %}
<div class="unit">{{ 'person' if number == 1 else 'people' }} ready to give</div>
{% elif usertype == "organization" %}
<div class="unit">public member{{ '' if number == 1 else 's' }}</div>
{% end %}
</td>
<td class="offset"></td>
</tr>
</table>
{% if user.ANON or usertype == "user" %}
<div class="nav level-1">
{% if user.ANON %}
{% if usertype == "user" %}
<h2>Your Weekly Gift</h2>
{% for amount in AMOUNTS %}
<button class="tip-anon">${{ amount }}</button>
{% end %}
{% end %}
<div class="help with-padding">Sign in using
<a href="{{ twitter.oauth_url(website, u'opt-in', path.decoded) }}">Twitter</a> or
<a class="highlight flash-me"
href="{{ bitbucket.oauth_url(website, u'opt-in', path.decoded) }}">Bitbucket</a>
to pledge to <b>{{ username }}</b>.</div>
{% elif usertype == "user" and account.is_locked %}
<div class="help with-padding">
This account has been locked by its owner.
</div>
{% elif usertype == "user" %}
{% set my_tip = user.get_tip_to(participant.id) %}
<h2>Your Weekly Pledge</h2>
{% for amount in AMOUNTS %}
<button amount="{{ amount }}" tippee="{{ participant.id }}"
class="tip{{ ' selected' if amount == my_tip else '' }}">${{ amount }}</button>
{% end %}
{% if my_tip not in AMOUNTS %}
<span class="old-amount">
<button class="tip disabled selected">{{ my_tip }}</button>
<span class="old-amount-link">&mdash;
<a href="http://blog.gittip.com/post/26505682007/is-personal-funding-viable" target="_blank">old amount</a>!</span>
</span>
{% end %}
{% if user.last_bill_result is None %}
<div id="payment-prompt" class="help{% if my_tip > 0 %} needed{% end %}">
Back your gift with a <a href="/credit-card.html"
class="highlight">credit card</a> to make sure it goes
through!</div>
{% elif user.last_bill_result %}
<div id="payment-prompt" class="help really-needed">Fix your <a
href="/credit-card.html" class="highlight">credit
card</a> to make sure your gifts go through!</div>
{% end %}
{% end %}
</div>
{% end %}
{% end %}
{% block page %}
{% if usertype == "user" %}
<div class="col0">
{% if account.is_locked %}
<h2>{{ username }} has opted out of Gittip.</h2>
<p>If you are <a href="{{ user_info.get('html_url', '') }}">{{ username }}</a>
on Bitbucket, you can unlock your account to allow people to pledge tips
to you on Gittip.</p>
{% if 0 %}
<a href="{{ bitbucket.oauth_url(website, u'unlock', username) }}"
><button>Unlock</button></a>
{% end %}
{% else %}
<script>$(document).ready(Gittip.initTipButtons);</script>
<h2>{{ name }} has not joined Gittip.</h2>
<p>Is this you?
{% if user.ANON %}
{% if 0 %}
<a href="{{ bitbucket.oauth_url(website, u'opt-in', username) }}">Click
here</a> to opt in to Gittip. We never collect money for you until
you do.
{% end %}
{% else %}
<a href="/sign-out.html">Sign out</a> and sign back in
to claim this account
{% end %}
</p>
{% if user.ANON %}
<h2>What is Gittip?</h2>
<p>Gittip is a way to thank and support your favorite artists,
musicians, writers, programmers, etc. by setting up a small weekly cash
gift to them. <a href="/about/">Read more ...</a></p>
<h2>Don't like what you see?</h2>
<p>If you are {{ username }} you can explicitly opt out of Gittip by
locking this account. We don't allow new pledges to locked
accounts.</p>
{% if 0 %}
<a href="{{ bitbucket.oauth_url(website, u'lock', username) }}"
><button>Lock</button></a>
{% end %}
{% end %}
{% end %}
</div>
{% elif usertype == "organization" %}
<script>$(document).ready(Gittip.initTipButtons);</script>
<table id="members" class="centered">
{% for i, sequence in enumerate([on_gittip, not_on_gittip]) %}
{% set nsequence = len(sequence) %}
{% if sequence %}<tr><td colspan="2">
<h2>{{ nsequence }}
{% if number > 0 %}
({{ "%.1f" % (nsequence * 100 / float(number)) }}%)
{% end %}
{{ 'is' if nsequence == 1 else 'are' }}
{{ i == 0 and "also on" or "not on" }} Gittip</h2>
</td></tr>
{% end %}
{% for member, tip in sequence %}
<tr class="not-over"><td>
{% if not user.ANON %}
<th>
<a href="/on/bitbucket/{{ member['username'] }}/"><img src="{{ member.get('avatar_url', '/assets/%s/no-avatar.png' % __version__) }}" />
{{ member['username'] }}</a>
</th>
<td>
{% for amount in AMOUNTS %}
<button amount="{{ amount }}" tippee="{{ member['username'] }}"
class="tip small {{ 'selected' if amount == tip else 'empty' }}">{{ amount }}</button>
{% end %}
{% if tip not in AMOUNTS %}
<span class="old-amount">
<button class="tip small disabled selected">{{ tip }}</button>
<span class="old-amount-link">&mdash;
<a href="http://blog.gittip.com/post/26505682007/is-personal-funding-viable" target="_blank">old amount</a>!</span>
</span>
{% end %}
</td>
{% else %}
<td>
<a href="/on/bitbucket/{{ member['username'] }}/"><img src="{{ member.get('avatar_url', '/assets/%s/no-avatar.png' % __version__) }}" />
{{ member['username'] }}</a>
</td>
{% end %}
</td></tr>
{% end %}
{% end %}
</table>
{% else %}
<h2>Not sure what to do with {{ name }}.</h2>
I don't recognize the &ldquo;{{ usertype }}&rdquo; type of user on Bitbucket.<br />
Sorry. :-(
{% end %}
{% end %}