Files
Chad Whitacre e781476c62 Fix bug in jsonp; #873
When I brought over the code from the jsonp branch in Aspen I lost the
actual serialization step.
2013-04-24 07:36:17 -04:00

109 lines
3.1 KiB
JSON

"""JSON endpoint for a Gittip widget.
"""
import re
from aspen import json
from gittip.utils import get_participant
callback_pattern = re.compile(r'^[_A-Za-z0-9.]+$')
# ========================================================================== ^L
participant = get_participant(request, restrict=False)
receiving = participant.get_dollars_receiving()
output = { "id": participant.id
, "username": participant.username
, "receiving": str(receiving)
, "avatar": participant.get_img_src()
}
# Generate goal key
# =================
# Display values:
#
# undefined - user is not here to receive tips, but will generally regift them
# null - user has no funding goal
# 3.00 - user wishes to receive at least this amount
if participant.goal != 0:
if participant.goal > 0:
goal = str(participant.goal)
else:
goal = None
output["goal"] = goal
# Generate giving key
# ===================
# Display values:
#
# null - user is giving anonymously
# 3.00 - user gives this amount in tips
if not participant.anonymous:
giving = str(participant.get_dollars_giving())
else:
giving = None
output["giving"] = giving
# Generate my_tip key
# ===================
# Display values:
#
# undefined - user is not authenticated
# "self" - user == participant
# null - user has never tipped this person
# 0.00 - user used to tip this person but now doesn't
# 3.00 - user tips this person this amount
if not user.ANON:
if user.username == path['username']:
my_tip = "self"
else:
my_tip = user.get_tip_to(path['username'])
output["my_tip"] = str(my_tip)
# Accounts Elsewhere
# ==================
# For Twitter we can use an immutable id. For GitHub we have an immutable id
# but we can't use it. For Bitbucket we don't have an immutable id. It's nice
# that Twitter lets us use an immutable id. We should do that ourselves:
#
# https://github.com/gittip/www.gittip.com/issues/680
github, twitter, bitbucket = participant.get_accounts_elsewhere()
if bitbucket is not None:
bitbucket = "https://bitbucket.org/api/1.0/users/%s" % \
bitbucket.user_info['username']
if github is not None:
github = "https://api.github.com/users/%s" % github.user_info['login']
if twitter is not None:
twitter = "https://api.twitter.com/1/users/show.json?id=%s" \
"&include_entities=1" % twitter.user_id
output['elsewhere'] = { 'bitbucket': bitbucket
, 'github': github
, 'twitter': twitter
}
response.body = output
# CORS - see https://github.com/gittip/aspen-python/issues/138
response.headers["Access-Control-Allow-Origin"] = "*"
# JSONP - see https://github.com/gittip/aspen-python/issues/138
callback = qs.get('callback')
if callback is not None:
if callback_pattern.match(callback) is None:
response.code = 400
response.body = {"error": "bad callback"}
else:
response.body = "%s(%s)" % (callback, json.dumps(response.body))
response.headers['Content-Type'] = 'application/javascript'