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

Move is_locked from elsewhere to participants #3012

Merged
merged 1 commit into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BEGIN;

ALTER TABLE participants ADD COLUMN is_locked bool NOT NULL DEFAULT FALSE;
ALTER TABLE participants ADD CONSTRAINT claimed_not_locked CHECK (NOT (claimed_time IS NOT NULL AND is_locked));

UPDATE participants p
SET is_locked = true
FROM elsewhere e
WHERE e.participant = p.username
AND e.is_locked;

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we want to remove the is_locked column from elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, I just didn't put it in the branch.sql because dropping columns means I have to recreate my dev DB when I switch to another branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, Okay.

END;
13 changes: 7 additions & 6 deletions fake_payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
CREATE TEMPORARY TABLE temp_participants ON COMMIT DROP AS
SELECT username
, claimed_time
, is_locked
, balance AS fake_balance
, 0::numeric(35,2) AS giving
, 0::numeric(35,2) AS pledging
Expand Down Expand Up @@ -41,17 +42,17 @@ CREATE TEMPORARY TABLE temp_takes
CREATE OR REPLACE FUNCTION fake_tip() RETURNS trigger AS $$
DECLARE
tipper temp_participants;
tippee_elsewhere elsewhere;
tippee temp_participants;
BEGIN
tipper := (
SELECT p.*::temp_participants
FROM temp_participants p
WHERE username = NEW.tipper
);
tippee_elsewhere := (
SELECT e.*::elsewhere
FROM elsewhere e
WHERE participant = NEW.tippee
tippee := (
SELECT p.*::temp_participants
FROM temp_participants p
WHERE username = NEW.tippee
LIMIT 1
);
IF (NEW.amount > tipper.fake_balance AND NOT tipper.credit_card_ok) THEN
Expand All @@ -62,7 +63,7 @@ CREATE OR REPLACE FUNCTION fake_tip() RETURNS trigger AS $$
SET fake_balance = (fake_balance - NEW.amount)
, giving = (giving + NEW.amount)
WHERE username = NEW.tipper;
ELSIF (NOT tippee_elsewhere.is_locked) THEN
ELSIF (NOT tippee.is_locked) THEN
UPDATE temp_participants
SET fake_balance = (fake_balance - NEW.amount)
, pledging = (pledging + NEW.amount)
Expand Down
15 changes: 0 additions & 15 deletions gratipay/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def check_db(cursor):
_check_orphans(cursor)
_check_orphans_no_tips(cursor)
_check_paydays_volumes(cursor)
_check_claimed_not_locked(cursor)


def _check_tips(cursor):
Expand Down Expand Up @@ -227,20 +226,6 @@ def _check_paydays_volumes(cursor):
assert len(ach_fees_volume) == 0


def _check_claimed_not_locked(cursor):
locked = cursor.all("""
SELECT participant
FROM elsewhere
WHERE EXISTS (
SELECT *
FROM participants
WHERE username=participant
AND claimed_time IS NOT NULL
) AND is_locked
""")
assert len(locked) == 0


def add_event(c, type, payload):
SQL = """
INSERT INTO events (type, payload)
Expand Down
8 changes: 1 addition & 7 deletions gratipay/models/account_elsewhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def opt_in(self, desired_username):
"""Given a desired username, return a User object.
"""
from gratipay.security.user import User
self.set_is_locked(False)
self.participant.set_is_locked(False)
user = User.from_username(self.participant.username)
assert not user.ANON, self.participant # sanity check
if self.participant.is_claimed:
Expand All @@ -175,12 +175,6 @@ def save_token(self, token):
""", (token, self.id))
self.set_attributes(token=token)

def set_is_locked(self, is_locked):
self.db.run( 'UPDATE elsewhere SET is_locked=%s WHERE id=%s'
, (is_locked, self.id)
)
self.set_attributes(is_locked=is_locked)


def get_account_elsewhere(website, request):
path = request.line.uri.path
Expand Down
11 changes: 8 additions & 3 deletions gratipay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ def set_email_lang(self, accept_lang):
# Random Junk
# ===========

def set_is_locked(self, is_locked):
self.db.run( 'UPDATE participants SET is_locked=%s WHERE id=%s'
, (is_locked, self.id)
)
self.set_attributes(is_locked=is_locked)

def get_teams(self):
"""Return a list of teams this user is a member of.
"""
Expand Down Expand Up @@ -785,7 +791,7 @@ def update_giving(self, cursor=None):
# Update giving and pledging on participant
giving, pledging = (cursor or self.db).one("""
WITH our_tips AS (
SELECT amount, tippee, p2.claimed_time
SELECT amount, tippee, p2.claimed_time, p2.is_locked
FROM current_tips
JOIN participants p2 ON p2.username = tippee
WHERE tipper = %(username)s
Expand All @@ -802,9 +808,8 @@ def update_giving(self, cursor=None):
, pledging = COALESCE((
SELECT sum(amount)
FROM our_tips
JOIN elsewhere ON elsewhere.participant = tippee
WHERE claimed_time IS NULL
AND elsewhere.is_locked = false
AND is_locked = false
), 0)
WHERE p.username = %(username)s
RETURNING giving, pledging
Expand Down
7 changes: 2 additions & 5 deletions gratipay/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,13 @@ def clear_tables(self):
self.db.run("ALTER SEQUENCE participants_id_seq RESTART WITH 1")


def make_elsewhere(self, platform, user_id, user_name, is_locked=False, **kw):
def make_elsewhere(self, platform, user_id, user_name, **kw):
info = UserInfo( platform=platform
, user_id=unicode(user_id)
, user_name=user_name
, **kw
)
account = AccountElsewhere.upsert(info)
if is_locked:
account.set_is_locked(True)
return account
return AccountElsewhere.upsert(info)


def show_table(self, table):
Expand Down
2 changes: 1 addition & 1 deletion gratipay/utils/fake_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def fake_participant(db, number="singular", is_admin=False):
, last_bill_result='' # Needed to not be suspicious
, claimed_time=faker.date_time_this_year()
, number=number
, is_locked=False
)
#Call participant constructor to perform other DB initialization
return Participant.from_username(username)
Expand Down Expand Up @@ -118,7 +119,6 @@ def fake_elsewhere(db, participant, platform):
, platform=platform
, user_id=fake_text_id()
, user_name=participant.username
, is_locked=False
, participant=participant.username
, extra_info=None
)
Expand Down
7 changes: 3 additions & 4 deletions tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from gratipay.billing.payday import NoPayday, Payday
from gratipay.exceptions import NegativeBalance
from gratipay.models.participant import Participant
from gratipay.models.account_elsewhere import AccountElsewhere
from gratipay.testing import Foobar, Harness
from gratipay.testing.balanced import BalancedHarness

Expand Down Expand Up @@ -106,8 +105,8 @@ def test_update_cached_amounts(self):
bob = self.make_participant('bob', claimed_time='now', last_bill_result=None)
carl = self.make_participant('carl', claimed_time='now', last_bill_result="Fail!")
dana = self.make_participant('dana', claimed_time='now')
emma = self.make_elsewhere('github', 58946, 'emma').participant
roy = self.make_elsewhere('github', 58947, 'roy', is_locked=True).participant
emma = self.make_participant('emma')
roy = self.make_participant('roy', is_locked=True)
alice.set_tip_to(dana, '3.00')
alice.set_tip_to(bob, '6.00')
alice.set_tip_to(emma, '1.00')
Expand All @@ -125,7 +124,7 @@ def check():
bob = Participant.from_username('bob')
carl = Participant.from_username('carl')
dana = Participant.from_username('dana')
emma = AccountElsewhere.from_user_name('github','emma').participant
emma = Participant.from_username('emma')
assert alice.giving == D('13.00')
assert alice.pledging == D('1.00')
assert alice.receiving == D('5.00')
Expand Down
2 changes: 1 addition & 1 deletion tests/py/test_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def test_pledging_only_counts_latest_tip(self):

def test_pledging_doesnt_count_locked_accounts(self):
alice = self.make_participant('alice', claimed_time='now', last_bill_result='')
bob = self.make_elsewhere('github', 58946, 'bob', is_locked=True).participant
bob = self.make_participant('bob', is_locked=True)
alice.set_tip_to(bob, '3.00')
assert alice.pledging == Decimal('0.00')

Expand Down
6 changes: 3 additions & 3 deletions www/on/%platform/%user_name/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ if account.participant.is_claimed:
request.redirect('/%s/' % account.participant.username)

title = username = user_name = account.user_name
locked = account.is_locked
participant = account.participant
locked = participant.is_locked
is_team = account.is_team
allow_pledges = not locked and (not is_team or platform.allows_team_connect)

Expand Down Expand Up @@ -194,7 +194,7 @@ else:

{% for member in members %}
{% set on_gratipay = member.participant.is_claimed %}
{% set declines_gifts = not member.participant.accepts_tips or member.is_locked %}
{% set declines_gifts = not member.participant.accepts_tips or member.participant.is_locked %}
<tr class="{{ 'declines' if declines_gifts }} {{ 'not-on-gratipay' if not on_gratipay }}">
{% if on_gratipay %}
<td>
Expand All @@ -218,7 +218,7 @@ else:
</a>
</td>
<td>
{% if member.is_locked %}
{% if member.participant.is_locked %}
declines gifts (opted out)
{% else %}
hasn't joined Gratipay yet
Expand Down
2 changes: 1 addition & 1 deletion www/on/%platform/associate.spt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ elif action == 'connect':
raise request.resource.respond(request, dispatch_result)

elif action in {'lock', 'unlock'}:
account.set_is_locked(action == 'lock')
account.participant.set_is_locked(action == 'lock')

else:
raise Response(400)
Expand Down