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

rationalize cached values #3721

Merged
merged 24 commits into from
Aug 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c5c3732
Start with anonymous_receiving
chadwhitacre Aug 29, 2015
e5ae260
Remove most references to anonymous_receiving
chadwhitacre Aug 29, 2015
b1d6886
Remove the number.json endpoint
chadwhitacre Aug 29, 2015
26a53d6
Update API section of README
chadwhitacre Aug 29, 2015
9f778e2
Move receiving/ from ~user to Team and stub it
chadwhitacre Aug 29, 2015
7da0c74
Remove my_tip from public.json
chadwhitacre Aug 29, 2015
387076e
Remove npatrons
chadwhitacre Aug 29, 2015
fd534ed
Now for `receiving` ...
chadwhitacre Aug 29, 2015
8b75349
Take a first pass through the codebase, inc. tests
chadwhitacre Aug 29, 2015
56c2550
Clean up browse in test_pages
chadwhitacre Aug 29, 2015
f3e653d
Fix Participant.taking
chadwhitacre Aug 29, 2015
ec8895e
Whittle down to one failing test
chadwhitacre Aug 30, 2015
fb58bf8
Remove update_cached_amounts
chadwhitacre Aug 30, 2015
e27a228
Hack exchange_route consistently
chadwhitacre Aug 30, 2015
e01b00f
Update taking during takeover
chadwhitacre Aug 30, 2015
7306177
Add ngiving_to and ntaking_from
chadwhitacre Aug 30, 2015
e9b0fc3
Rationalize cache names on teams
chadwhitacre Aug 30, 2015
bc927c0
Here's a SQL script to reset cached values
chadwhitacre Aug 30, 2015
17856a4
Surface cached values in the UI
chadwhitacre Aug 30, 2015
2fd47c8
Fix a missing ~ in a link
chadwhitacre Aug 30, 2015
e804ce3
Fix bugs in and around payments.js
chadwhitacre Aug 30, 2015
7d9704e
Make new numbers box more or less responsive
chadwhitacre Aug 30, 2015
4e9313b
Make footer link not bold
chadwhitacre Aug 30, 2015
d5fe4f8
Actually, I want more control over running this
chadwhitacre Aug 30, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 3 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ some fake data, so that more of the site is functional, run this command:
API
===

The Gratipay API is comprised of these six endpoints:
The Gratipay API is comprised of these four endpoints:

**[/about/charts.json](https://gratipay.com/about/charts.json)**
([source](https://github.com/gratipay/gratipay.com/tree/master/www/about/charts.json.spt))&mdash;<i>public</i>&mdash;Returns
Expand All @@ -440,28 +440,12 @@ charts page used to use this.
an object giving a point-in-time snapshot of Gratipay. The
[stats](https://gratipay.com/about/stats.html) page displays the same info.

**/`%username`/charts.json**
([example](https://gratipay.com/Gratipay/charts.json),
[source](https://github.com/gratipay/gratipay.com/tree/master/www/%25username/charts.json.spt))&mdash;<i>public</i>&mdash;Returns
an array of objects, one per week, showing aggregate numbers over time for the
given user.

**/`%username`/public.json**
([example](https://gratipay.com/Gratipay/public.json),
[source](https://github.com/gratipay/gratipay.com/tree/master/www/%25username/public.json.spt))&mdash;<i>public</i>&mdash;Returns an object with these keys:

- "receiving"&mdash;an estimate of the amount the given participant will
receive this week

- "my_tip"&mdash;logged-in user's tip to the Gratipay participant in
question; possible values are:

- `undefined` (key not present)&mdash;there is no logged-in user
- "self"&mdash;logged-in user is the participant in question
- `null`&mdash;user has never tipped this participant
- "0.00"&mdash;user used to tip this participant
- "3.00"&mdash;user tips this participant the given amount
<br><br>
- "taking"&mdash;an estimate of the amount the given participant will
take from Teams this week

- "elsewhere"&mdash;participant's connected accounts elsewhere; returns an object with these keys:

Expand All @@ -479,34 +463,6 @@ given user.
- `http://www.openstreetmap.org/user/%openstreetmap_username`


**/`%username`/tips.json**
([source](https://github.com/gratipay/gratipay.com/tree/master/www/%25username/tips.json.spt))&mdash;<i>private</i>&mdash;Responds
to `GET` with an array of objects representing your current tips. `POST` the
same structure back in order to update tips in bulk (be sure to set
`Content-Type` to `application/json` instead of
`application/x-www-form-urlencoded`). You can `POST` a partial array to update
a subset of your tips. The response to a `POST` will be only the subset you
updated. If the `amount` is `"error"` then there will also be an `error`
attribute with a one-word error code. If you include an `also_prune` key in the
querystring (not the body!) with a value of `yes`, `true`, or `1`, then any
tips not in the array you `POST` will be zeroed out.

NOTE: The amounts must be encoded as a string (rather than a number).
Additionally, currently, the only supported platform is 'gratipay' ('gittip'
still works for backwards-compatibility).

This endpoint requires authentication. Look for your user ID and API key on your
[account page](https://gratipay.com/about/me/settings/), and pass them using basic
auth. E.g.:

```
curl https://gratipay.com/foobar/tips.json \
-u $userid:$api_key \
-X POST \
-d'[{"username":"bazbuz", "platform":"gratipay", "amount": "1.00"}]' \
-H"Content-Type: application/json"
```

API Implementations
-------------------

Expand Down
3 changes: 2 additions & 1 deletion gratipay/billing/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def record_exchange_result(db, exchange_id, status, error, participant):
""", locals())
assert participant.username == username
assert isinstance(route, ExchangeRoute)
route.set_attributes(participant=participant) # XXX Red hot hack!

if amount < 0:
amount -= fee
Expand All @@ -325,7 +326,7 @@ def propagate_exchange(cursor, participant, route, error, amount):
"""Propagates an exchange's result to the participant's balance and the
route's status.
"""
route.update_error(error or '', propagate=False)
route.update_error(error or '')
new_balance = cursor.one("""
UPDATE participants
SET balance=(balance + %s)
Expand Down
11 changes: 0 additions & 11 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
with open('sql/payday.sql') as f:
PAYDAY = f.read()

with open('sql/fake_payday.sql') as f:
FAKE_PAYDAY = f.read()


class ExceptionWrapped(Exception): pass

Expand Down Expand Up @@ -80,7 +77,6 @@ class Payday(object):
update_balances
take_over_balances
update_stats
update_cached_amounts
end

"""
Expand Down Expand Up @@ -137,7 +133,6 @@ def run(self):
self.mark_stage_done()
if self.stage < 2:
self.update_stats()
self.update_cached_amounts()
self.mark_stage_done()

self.end()
Expand Down Expand Up @@ -462,12 +457,6 @@ def update_stats(self):
log("Updated payday stats.")


def update_cached_amounts(self):
with self.db.get_cursor() as cursor:
cursor.execute(FAKE_PAYDAY)
log("Updated receiving amounts.")


def end(self):
self.ts_end = self.db.one("""\

Expand Down
33 changes: 24 additions & 9 deletions gratipay/models/exchange_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ def __bool__(self):

@classmethod
def from_id(cls, id):
return cls.db.one("""
r = cls.db.one("""
SELECT r.*::exchange_routes
FROM exchange_routes r
WHERE id = %(id)s
""", locals())
if r:
from gratipay.models.participant import Participant # XXX Red hot hack!
r.set_attributes(participant=Participant.from_id(r.participant))
return r

@classmethod
def from_network(cls, participant, network):
Expand All @@ -31,7 +35,7 @@ def from_network(cls, participant, network):
AND network = %(network)s
""", locals())
if r:
r.__dict__['participant'] = participant
r.set_attributes(participant=participant)
return r

@classmethod
Expand All @@ -45,7 +49,7 @@ def from_address(cls, participant, network, address):
AND address = %(address)s
""", locals())
if r:
r.__dict__['participant'] = participant
r.set_attributes(participant=participant)
return r

@classmethod
Expand All @@ -57,9 +61,9 @@ def insert(cls, participant, network, address, error='', fee_cap=None):
VALUES (%(participant_id)s, %(network)s, %(address)s, %(error)s, %(fee_cap)s)
RETURNING exchange_routes.*::exchange_routes
""", locals())
if network == 'balanced-cc':
if network == 'braintree-cc':
participant.update_giving_and_teams()
r.__dict__['participant'] = participant
r.set_attributes(participant=participant)
return r

def invalidate(self):
Expand All @@ -69,11 +73,12 @@ def invalidate(self):
# For Paypal, we remove the record entirely to prevent
# an integrity error if the user tries to add the route again
if self.network == 'paypal':
# XXX This doesn't sound right. Doesn't this corrupt history pages?
self.db.run("DELETE FROM exchange_routes WHERE id=%s", (self.id,))
else:
self.update_error('invalidated')

def update_error(self, new_error, propagate=True):
def update_error(self, new_error):
id = self.id
old_error = self.error
if old_error == 'invalidated':
Expand All @@ -85,9 +90,19 @@ def update_error(self, new_error, propagate=True):
""", locals())
self.set_attributes(error=new_error)

# Update the receiving amounts of tippees if requested and necessary
if not propagate or self.network != 'balanced-cc':
# Update cached amounts if requested and necessary
if self.network != 'braintree-cc':
return
if self.participant.is_suspicious or bool(new_error) == bool(old_error):
return
self.participant.update_giving_and_teams()


# XXX *White* hot hack!
# =====================
# During payday, participant is a record from a select of
# payday_participants (or whatever), *not* an actual Participant
# object. We need the real deal so we can use a method on it ...

from gratipay.models.participant import Participant
participant = Participant.from_username(self.participant.username)
participant.update_giving_and_teams()
Loading