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

Commit

Permalink
Clean up the tip.json API
Browse files Browse the repository at this point in the history
I've got someone asking to help with building a widget (#21, #41). I
believe this JSON API will enable that.
  • Loading branch information
chadwhitacre committed Jun 15, 2012
1 parent 9cb6053 commit f6706e5
Showing 1 changed file with 78 additions and 58 deletions.
136 changes: 78 additions & 58 deletions www/%participant_id/tip.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""Get or change the authenticated user's tip to this person.

Unauthenticated users will always get back {}, so retval.amount will be
undefined in JavaScript. Authenticated users can pass "amount" in the
querystring to change their tip. It will be ignored if you try to tip yourself,
otherwise it must be one of: "0.00", "0.08", "0.16", "0.32", "0.64", "1.28"
(you'll get a 400 otherwise). The return value for authenticated users is
{"amount": "0.08"}, where the value is guaranteed to be one of those same
strings, or None if it's yourself.

"""
import decimal

from aspen import Response
Expand All @@ -8,66 +19,75 @@ from gittip.networks import github
# ========================================================================== ^L


# Get tipper and tippee.
# ======================

tipper = user.id
if 'participant_id' in path: # gittip
tippee = path['participant_id']
elif 'login' in path: # github
tippee = github.resolve(path['login'])
else:
raise Response(400, "can't find tippee")
if tippee == tipper:
amount = "0.00"
else:

# Add a new tip record.
# =====================
# Insert instead of update. The analytics may be interesting some day.

if 'amount' in qs:
try:
amount = decimal.Decimal(qs['amount'])
if amount not in AMOUNTS:
raise ValueError
except decimal.InvalidOperation, ValueError:
raise Response(400, "bad amount")

NEW_TIP = """\

INSERT INTO tips
(ctime, tipper, tippee, amount)
VALUES ( COALESCE (( SELECT ctime
FROM tips
WHERE (tipper=%s AND tippee=%s)
LIMIT 1
), CURRENT_TIMESTAMP)
, %s, %s, %s
)

"""
db.execute(NEW_TIP, (tipper, tippee, tipper, tippee, amount))


# Fetch the current tip record.
# =============================
out = {}
if not user.ANON:

# Get tipper and tippee.
# ======================

tipper = user.id
if 'participant_id' in path: # gittip
tippee = path['participant_id']
elif 'login' in path: # github
tippee = github.resolve(path['login'])
else:
raise Response(400, "can't find tippee")


# Get and maybe set amount.
# =========================

if tippee == tipper:
amount = None
else:
FETCH = """\

SELECT amount
FROM tips
WHERE tippee=%s AND tipper=%s
ORDER BY mtime DESC
LIMIT 1

"""
rec = db.fetchone(FETCH, (tippee, tipper))
if rec is None:
amount = "0.00"

# Add a new tip record.
# =====================
# Insert instead of update. The analytics may be interesting some day.

if 'amount' in qs:
try:
amount = decimal.Decimal(qs['amount'])
if amount not in AMOUNTS:
raise ValueError
except (decimal.InvalidOperation, ValueError):
raise Response(400, "bad amount")

NEW_TIP = """\

INSERT INTO tips
(ctime, tipper, tippee, amount)
VALUES ( COALESCE (( SELECT ctime
FROM tips
WHERE (tipper=%s AND tippee=%s)
LIMIT 1
), CURRENT_TIMESTAMP)
, %s, %s, %s
)

"""
db.execute(NEW_TIP, (tipper, tippee, tipper, tippee, amount))


# Fetch the current tip record.
# =============================

else:
amount = str(rec['amount'])
FETCH = """\

SELECT amount
FROM tips
WHERE tippee=%s AND tipper=%s
ORDER BY mtime DESC
LIMIT 1

"""
rec = db.fetchone(FETCH, (tippee, tipper))
if rec is None:
amount = "0.00"
else:
amount = str(rec['amount'])

out = {"amount": amount}

response.body = {"amount": amount}
response.body = out

0 comments on commit f6706e5

Please sign in to comment.