-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,23 +127,43 @@ def set_take_for(self, member, take, recorder): | |
def __set_take_for(self, member, amount, recorder): | ||
assert self.IS_PLURAL | ||
# XXX Factored out for testing purposes only! :O Use .set_take_for. | ||
self.db.run(""" | ||
INSERT INTO takes (ctime, member, team, amount, recorder) | ||
VALUES ( COALESCE (( SELECT ctime | ||
FROM takes | ||
WHERE member=%(member)s | ||
AND team=%(team)s | ||
LIMIT 1 | ||
), CURRENT_TIMESTAMP) | ||
, %(member)s | ||
, %(team)s | ||
, %(amount)s | ||
, %(recorder)s | ||
) | ||
""", dict(member=member.username, team=self.username, amount=amount, | ||
recorder=recorder.username)) | ||
with self.db.get_cursor() as cursor: | ||
# Lock to avoid race conditions | ||
cursor.run("LOCK TABLE takes IN EXCLUSIVE MODE") | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
chadwhitacre
Contributor
|
||
# Compute the current takes | ||
old_takes = self.compute_actual_takes(cursor) | ||
# Insert the new take | ||
cursor.run(""" | ||
INSERT INTO takes (ctime, member, team, amount, recorder) | ||
VALUES ( COALESCE (( SELECT ctime | ||
FROM takes | ||
WHERE member=%(member)s | ||
AND team=%(team)s | ||
LIMIT 1 | ||
), CURRENT_TIMESTAMP) | ||
, %(member)s | ||
, %(team)s | ||
, %(amount)s | ||
, %(recorder)s | ||
) | ||
""", dict(member=member.username, team=self.username, amount=amount, | ||
recorder=recorder.username)) | ||
# Compute the new takes | ||
new_takes = self.compute_actual_takes(cursor) | ||
# Update receiving amounts in the participants table | ||
for username in set(old_takes.keys()).union(new_takes.keys()): | ||
old = old_takes.get(username, {}).get('actual_amount', Decimal(0)) | ||
new = new_takes.get(username, {}).get('actual_amount', Decimal(0)) | ||
diff = new - old | ||
if diff != 0: | ||
cursor.run(""" | ||
UPDATE participants | ||
SET takes = (takes + %(diff)s) | ||
, receiving = (receiving + %(diff)s) | ||
WHERE username=%(username)s | ||
""", dict(username=username, diff=diff)) | ||
|
||
def get_takes(self, for_payday=False, cursor=None): | ||
"""Return a list of member takes for a team. | ||
|
2 comments
on commit 2b9eea3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with this commit.
First time we've used this, yeah? What are the implications? What can and can't be done by other users while this lock is held?