From 2989eb31e6cb1e668d950330873de376b660d3d5 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Tue, 10 Jun 2014 12:18:49 -0400 Subject: [PATCH 1/2] Add an is_closed attribute to Participant This is useful for showing an "Account closed by user" page for that case. --- branch.sql | 5 +++++ gittip/models/participant.py | 23 ++++++++++++++++++++++- tests/py/test_cancel.py | 27 +++++++++++++++++++++++++++ tests/py/test_participant.py | 6 ++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 branch.sql diff --git a/branch.sql b/branch.sql new file mode 100644 index 0000000000..20c086f178 --- /dev/null +++ b/branch.sql @@ -0,0 +1,5 @@ +BEGIN; + + ALTER TABLE participants ADD COLUMN is_closed bool NOT NULL DEFAULT FALSE; + +END; diff --git a/gittip/models/participant.py b/gittip/models/participant.py index b7cc85bcc2..d00158be77 100644 --- a/gittip/models/participant.py +++ b/gittip/models/participant.py @@ -241,12 +241,13 @@ def set_as_claimed(self): UPDATE participants SET claimed_time=CURRENT_TIMESTAMP + , is_closed=false WHERE username=%s AND claimed_time IS NULL RETURNING claimed_time """, (self.username,)) - self.set_attributes(claimed_time=claimed_time) + self.set_attributes(claimed_time=claimed_time, is_closed=False) # Canceling @@ -274,6 +275,7 @@ def cancel(self, disbursement_strategy): self.clear_tips_giving(cursor) self.clear_tips_receiving(cursor) self.clear_personal_information(cursor) + self.update_is_closed(True, cursor) return self.archive(cursor) @@ -551,6 +553,25 @@ def update_goal(self, goal): ) self.set_attributes(goal=goal) + def update_is_closed(self, is_closed, cursor=None): + ctx = None + if cursor is None: + ctx = self.db.get_cursor() + cursor = ctx.__enter__() + try: + cursor.run( "UPDATE participants SET is_closed=%(is_closed)s " + "WHERE username=%(username)s" + , dict(username=self.username, is_closed=is_closed) + ) + add_event( cursor + , 'participant' + , dict(id=self.id, action='set', values=dict(is_closed=is_closed)) + ) + self.set_attributes(is_closed=is_closed) + finally: + if ctx is not None: + ctx.__exit__(None, None, None) + def set_tip_to(self, tippee, amount, cursor=None): """Given a Participant or username, and amount as str, return a tuple. diff --git a/tests/py/test_cancel.py b/tests/py/test_cancel.py index 514e1f4219..9f6cca97d5 100644 --- a/tests/py/test_cancel.py +++ b/tests/py/test_cancel.py @@ -293,3 +293,30 @@ def test_cpi_clears_teams(self): alice.clear_personal_information(cursor) assert len(team.get_takes()) == 1 + + + # uic = update_is_closed + + def test_uic_updates_is_closed(self): + alice = self.make_participant('alice') + alice.update_is_closed(True) + + assert alice.is_closed + assert Participant.from_username('alice').is_closed + + def test_uic_updates_is_closed_False(self): + alice = self.make_participant('alice') + alice.update_is_closed(True) + alice.update_is_closed(False) + + assert not alice.is_closed + assert not Participant.from_username('alice').is_closed + + def test_uic_uses_supplied_cursor(self): + alice = self.make_participant('alice') + + with self.db.get_cursor() as cursor: + alice.update_is_closed(True, cursor) + assert alice.is_closed + assert not Participant.from_username('alice').is_closed + assert Participant.from_username('alice').is_closed diff --git a/tests/py/test_participant.py b/tests/py/test_participant.py index 75764d2c9b..16f803182f 100644 --- a/tests/py/test_participant.py +++ b/tests/py/test_participant.py @@ -329,6 +329,12 @@ def test_claiming_participant(self): expected = datetime.timedelta(seconds=0.1) assert actual < expected + def test_claiming_participant_resets_is_closed_to_false(self): + self.participant.update_is_closed(True) + self.participant.set_as_claimed() + assert not self.participant.is_closed + assert not Participant.from_username('user1').is_closed + def test_changing_username_successfully(self): self.participant.change_username('user2') actual = Participant.from_username('user2') From bb00262463d35143b53ab0ca462dce1c54e7326e Mon Sep 17 00:00:00 2001 From: Changaco Date: Tue, 10 Jun 2014 19:34:39 +0200 Subject: [PATCH 2/2] set is_closed=False in opt_in() instead of in set_as_claimed() --- gittip/models/account_elsewhere.py | 2 ++ gittip/models/participant.py | 3 +-- tests/py/test_elsewhere.py | 8 ++++++++ tests/py/test_participant.py | 6 ------ 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/gittip/models/account_elsewhere.py b/gittip/models/account_elsewhere.py index 9b2da78bc1..0e3b23739a 100644 --- a/gittip/models/account_elsewhere.py +++ b/gittip/models/account_elsewhere.py @@ -154,6 +154,8 @@ def opt_in(self, desired_username): user.participant.change_username(desired_username) except ProblemChangingUsername: pass + if user.participant.is_closed: + user.participant.update_is_closed(False) return user, newly_claimed def save_token(self, token, refresh_token=None, expires=None): diff --git a/gittip/models/participant.py b/gittip/models/participant.py index d00158be77..2005d2f774 100644 --- a/gittip/models/participant.py +++ b/gittip/models/participant.py @@ -241,13 +241,12 @@ def set_as_claimed(self): UPDATE participants SET claimed_time=CURRENT_TIMESTAMP - , is_closed=false WHERE username=%s AND claimed_time IS NULL RETURNING claimed_time """, (self.username,)) - self.set_attributes(claimed_time=claimed_time, is_closed=False) + self.set_attributes(claimed_time=claimed_time) # Canceling diff --git a/tests/py/test_elsewhere.py b/tests/py/test_elsewhere.py index d5351b48c3..cdd11969dc 100644 --- a/tests/py/test_elsewhere.py +++ b/tests/py/test_elsewhere.py @@ -2,6 +2,7 @@ from gittip.elsewhere import UserInfo from gittip.models.account_elsewhere import AccountElsewhere +from gittip.models.participant import Participant from gittip.testing import Harness import gittip.testing.elsewhere as user_info_examples @@ -33,6 +34,13 @@ def test_opt_in_doesnt_have_to_change_username(self): actual = account.opt_in('bob')[0].participant.username assert actual == expected + def test_opt_in_resets_is_closed_to_false(self): + alice = self.make_elsewhere('twitter', 1, 'alice') + alice.participant.update_is_closed(True) + user = alice.opt_in('alice')[0] + assert not user.participant.is_closed + assert not Participant.from_username('alice').is_closed + def test_redirect_csrf(self): response = self.client.GxT('/on/github/redirect') assert response.code == 405 diff --git a/tests/py/test_participant.py b/tests/py/test_participant.py index 16f803182f..75764d2c9b 100644 --- a/tests/py/test_participant.py +++ b/tests/py/test_participant.py @@ -329,12 +329,6 @@ def test_claiming_participant(self): expected = datetime.timedelta(seconds=0.1) assert actual < expected - def test_claiming_participant_resets_is_closed_to_false(self): - self.participant.update_is_closed(True) - self.participant.set_as_claimed() - assert not self.participant.is_closed - assert not Participant.from_username('user1').is_closed - def test_changing_username_successfully(self): self.participant.change_username('user2') actual = Participant.from_username('user2')