From f6706e57973102e01c5370e373b197cda1aaa6db Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Fri, 15 Jun 2012 17:41:53 -0400 Subject: [PATCH] Clean up the tip.json API I've got someone asking to help with building a widget (#21, #41). I believe this JSON API will enable that. --- www/%participant_id/tip.json | 136 ++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 58 deletions(-) diff --git a/www/%participant_id/tip.json b/www/%participant_id/tip.json index f373a0e320..1745ce3b1a 100644 --- a/www/%participant_id/tip.json +++ b/www/%participant_id/tip.json @@ -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 @@ -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