Repository: gratipay/gratipay.com

Issue:
Member page broken during payday
https://app.getsentry.com/gittip/gittip/group/26715022/

```
TooMany: Got 2 rows; expecting 0 or 1.

Stacktrace (most recent call last):

  File "site-packages/algorithm.py", line 288, in run
    new_state = function(**deps.as_kwargs)
  File "aspen/algorithms/website.py", line 75, in get_response_for_resource
    return {'response': resource.respond(request)}
  File "aspen/resources/dynamic_resource.py", line 72, in respond
    response = self.get_response(context)
  File "aspen/resources/negotiated_resource.py", line 102, in get_response
    response.body = render(context)
  File "aspen/renderers/__init__.py", line 103, in __call__
    return self.render_content(context)
  File "aspen/renderers/json_dump.py", line 22, in render_content
    return json.dumps(eval(self.compiled, globals(), context))
  File "string>", line 1, in <module>
  File "gittip/models/_mixin_team.py", line 288, in get_members
    member['last_week'] = last_week = self.get_take_last_week_for(member)
  File "gittip/models/_mixin_team.py", line 91, in get_take_last_week_for
    """, (self.username, membername), default=Decimal('0.00'))
  File "postgres/__init__.py", line 463, in one
    return cursor.one(sql, parameters, default)
  File "postgres/cursors.py", line 105, in one
    out = self._some(sql, parameters, lo=0, hi=1)
  File "postgres/cursors.py", line 132, in _some
    raise TooMany(self.rowcount, lo, hi)
```

Member page broken during payday
https://app.getsentry.com/gittip/gittip/group/26715022/

```
TooMany: Got 2 rows; expecting 0 or 1.

Stacktrace (most recent call last):

  File "site-packages/algorithm.py", line 288, in run
    new_state = function(**deps.as_kwargs)
  File "aspen/algorithms/website.py", line 75, in get_response_for_resource
    return {'response': resource.respond(request)}
  File "aspen/resources/dynamic_resource.py", line 72, in respond
    response = self.get_response(context)
  File "aspen/resources/negotiated_resource.py", line 102, in get_response
    response.body = render(context)
  File "aspen/renderers/__init__.py", line 103, in __call__
    return self.render_content(context)
  File "aspen/renderers/json_dump.py", line 22, in render_content
    return json.dumps(eval(self.compiled, globals(), context))
  File "string>", line 1, in <module>
  File "gittip/models/_mixin_team.py", line 288, in get_members
    member['last_week'] = last_week = self.get_take_last_week_for(member)
  File "gittip/models/_mixin_team.py", line 91, in get_take_last_week_for
    """, (self.username, membername), default=Decimal('0.00'))
  File "postgres/__init__.py", line 463, in one
    return cursor.one(sql, parameters, default)
  File "postgres/cursors.py", line 105, in one
    out = self._some(sql, parameters, lo=0, hi=1)
  File "postgres/cursors.py", line 132, in _some
    raise TooMany(self.rowcount, lo, hi)
```



Discussion:

Resolving patch:
diff --git a/gittip/models/_mixin_team.py b/gittip/models/_mixin_team.py
--- a/gittip/models/_mixin_team.py
+++ b/gittip/models/_mixin_team.py
@@ -87,6 +87,7 @@ def get_take_last_week_for(self, member):
                         WHERE ts_end > ts_start
                      ORDER BY ts_start DESC LIMIT 1
                    )
+          ORDER BY timestamp ASC LIMIT 1
 
         """, (self.username, membername), default=Decimal('0.00'))
 


Test patch:
diff --git a/tests/py/test_take.py b/tests/py/test_take.py
--- a/tests/py/test_take.py
+++ b/tests/py/test_take.py
@@ -204,7 +204,13 @@ def test_changes_to_others_take_can_increase_members_take(self):
     def test_gtlwf_works_during_payday(self):
         team = self.make_team()
         alice = self.make_participant('alice', take_last_week='30.00', claimed_time='now')
+        take_this_week = D('42.00')
         team.add_member(alice)
-        team.set_take_for(alice, D('42.00'), alice)
+        team.set_take_for(alice, take_this_week, alice)
         self.db.run("INSERT INTO paydays DEFAULT VALUES")
         assert team.get_take_last_week_for(alice) == 30
+        self.db.run("""
+            INSERT INTO transfers (timestamp, tipper, tippee, amount, context)
+            VALUES (now(), %(tipper)s, 'alice', %(amount)s, 'take')
+        """, dict(tipper=team.username, amount=take_this_week))
+        assert team.get_take_last_week_for(alice) == 30