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

Commit

Permalink
support using compute_actual_takes() inside a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Jun 14, 2014
1 parent 4410397 commit 0e1aa9e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions gittip/models/_mixin_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __set_take_for(self, member, amount, recorder):
""", dict(member=member.username, team=self.username, amount=amount,
recorder=recorder.username))

def get_takes(self, for_payday=False):
def get_takes(self, for_payday=False, cursor=None):
"""Return a list of member takes for a team.
This is implemented parallel to Participant.get_tips_and_total. See
Expand Down Expand Up @@ -194,14 +194,15 @@ def get_takes(self, for_payday=False):
"""

return self.db.all(TAKES, args, back_as=dict)
records = (cursor or self.db).all(TAKES, args)
return [r._asdict() for r in records]

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Jun 16, 2014

Contributor

Interesting. Where is the _asdict API coming from? Is that a psycopg2 thing?

This comment has been minimized.

Copy link
@Changaco

Changaco Jun 16, 2014

Author Contributor

@whit537 The use of _asdict is a workaround for liberapay/postgres.py#39, I didn't check where the method actually comes from.

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Jun 16, 2014

Contributor

The use of _asdict is a workaround for liberapay/postgres.py#39

Right, figured as much.

I didn't check where the method actually comes from.

Interesting! It's actually on Python's namedtuple objects. Noted on liberapay/postgres.py#39. Good find. :-)


def get_team_take(self):
def get_team_take(self, cursor=None):
"""Return a single take for a team, the team itself's take.
"""
assert self.IS_PLURAL
TAKE = "SELECT sum(amount) FROM current_takes WHERE team=%s"
total_take = self.db.one(TAKE, (self.username,), default=0)
total_take = (cursor or self.db).one(TAKE, (self.username,), default=0)
team_take = max(self.receiving - total_take, 0)
membership = { "ctime": None
, "mtime": None
Expand All @@ -210,12 +211,12 @@ def get_team_take(self):
}
return membership

def compute_actual_takes(self):
def compute_actual_takes(self, cursor=None):
"""Get the takes, compute the actual amounts, and return an OrderedDict.
"""
actual_takes = OrderedDict()
nominal_takes = self.get_takes()
nominal_takes.append(self.get_team_take())
nominal_takes = self.get_takes(cursor=cursor)
nominal_takes.append(self.get_team_take(cursor=cursor))
budget = balance = self.receiving
for take in nominal_takes:
nominal_amount = take['nominal_amount'] = take.pop('amount')
Expand Down

1 comment on commit 0e1aa9e

@chadwhitacre
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit lgtm.

Please sign in to comment.