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

Commit

Permalink
Finish renaming social_network_users (#406)
Browse files Browse the repository at this point in the history
I am renaming this because I want the Python library nomenclature to
harmonize with the database, and I don't want to call the Python
submodule social_network_users (it was called networks before). Now
they're both called elsewhere. Secondly, I want to broaden the category
to include things that might not necessarily be social networks (is
Vimeo a social network?). Platform is more generic. This commit also
adds some more page smoke tests. There's a lot more to test. :(
  • Loading branch information
chadwhitacre committed Dec 7, 2012
1 parent b9b848e commit 5c36f6a
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 80 deletions.
57 changes: 28 additions & 29 deletions gittip/elsewhere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def resolve_unclaimed(participant_id):
"""Given a participant_id, return an URL path.
"""
typecheck(participant_id, unicode)
rec = db.fetchone("SELECT network, user_info FROM social_network_users "
rec = db.fetchone("SELECT platform, user_info FROM elsewhere "
"WHERE participant_id=%s", (participant_id,))
if rec is None:
out = None
elif rec['network'] == 'github':
elif rec['platform'] == 'github':
out = '/on/github/%s/' % rec['user_info']['login']
else:
assert rec['network'] == 'twitter'
assert rec['platform'] == 'twitter'
out = '/on/twitter/%s/' % rec['user_info']['screen_name']
return out

Expand Down Expand Up @@ -83,25 +83,24 @@ def get_a_participant_id():
return participant_id.decode('US-ASCII')


def upsert(network, user_id, username, user_info):
def upsert(platform, user_id, username, user_info):
"""Given str, unicode, unicode, and dict, return unicode and boolean.
Network is the name of a social network that we support (ASCII blah).
User_id is an immutable unique identifier for the given user on the given
social network. Username is the user's login/user_id on the given social
network. It is only used here for logging. Specifically, we don't reserve
their username for them on Gittip if they're new here. We give them a
random participant_id here, and they'll have a chance to change it if/when
they opt in. User_id and username may or may not be the same. User_info is
a dictionary of profile info per the named network. All network dicts must
have an id key that corresponds to the primary key in the underlying table
in our own db.
Platform is the name of a platform that we support (ASCII blah). User_id is
an immutable unique identifier for the given user on the given platform
Username is the user's login/user_id on the given platform. It is only
used here for logging. Specifically, we don't reserve their username for
them on Gittip if they're new here. We give them a random participant_id
here, and they'll have a chance to change it if/when they opt in. User_id
and username may or may not be the same. User_info is a dictionary of
profile info per the named platform. All platform dicts must have an id key
that corresponds to the primary key in the underlying table in our own db.
The return value is a tuple: (participant_id [unicode], is_claimed
[boolean], is_locked [boolean], balance [Decimal]).
"""
typecheck( network, str
typecheck( platform, str
, user_id, (int, unicode)
, username, unicode
, user_info, dict
Expand All @@ -114,19 +113,19 @@ def upsert(network, user_id, username, user_info):

INSERT = """\
INSERT INTO social_network_users
(network, user_id)
INSERT INTO elsewhere
(platform, user_id)
VALUES (%s, %s)
"""
try:
db.execute(INSERT, (network, user_id,))
db.execute(INSERT, (platform, user_id,))
except IntegrityError:
pass # That login is already in our db.

UPDATE = """\
UPDATE social_network_users
UPDATE elsewhere
SET user_info=%s
WHERE user_id=%s
RETURNING participant_id
Expand Down Expand Up @@ -160,14 +159,14 @@ def upsert(network, user_id, username, user_info):
new_participant = True


# Associate the social network user with the Gittip participant.
# ==============================================================
# Associate the elsewhere account with the Gittip participant.
# ============================================================

ASSOCIATE = """\
UPDATE social_network_users
UPDATE elsewhere
SET participant_id=%s
WHERE network=%s
WHERE platform=%s
AND user_id=%s
AND ( (participant_id IS NULL)
OR (participant_id=%s)
Expand All @@ -177,9 +176,9 @@ def upsert(network, user_id, username, user_info):
"""

log(u"Associating %s (%s) on %s with %s on Gittip."
% (username, user_id, network, participant_id))
% (username, user_id, platform, participant_id))
rows = db.fetchall( ASSOCIATE
, (participant_id, network, user_id, participant_id)
, (participant_id, platform, user_id, participant_id)
)
rows = list(rows)
nrows = len(rows)
Expand All @@ -200,9 +199,9 @@ def upsert(network, user_id, username, user_info):
)

rec = db.fetchone( "SELECT participant_id, is_locked "
"FROM social_network_users "
"WHERE network=%s AND user_id=%s"
, (network, user_id)
"FROM elsewhere "
"WHERE platform=%s AND user_id=%s"
, (platform, user_id)
)
if rec is not None:

Expand All @@ -219,7 +218,7 @@ def upsert(network, user_id, username, user_info):

raise Exception("We're bailing on associating %s user %s (%s) with"
" a Gittip participant."
% (network, username, user_id))
% (platform, username, user_id))

rec = db.fetchone( "SELECT claimed_time, balance FROM participants "
"WHERE id=%s"
Expand Down
4 changes: 2 additions & 2 deletions gittip/elsewhere/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def resolve(login):
FETCH = """\
SELECT participant_id
FROM social_network_users
WHERE network='github'
FROM elsewhere
WHERE platform = 'github'
AND user_info -> 'login' = %s
""" # XXX Uniqueness constraint on login?
Expand Down
4 changes: 2 additions & 2 deletions gittip/elsewhere/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def resolve(user_id):
FETCH = """\
SELECT participant_id
FROM social_network_users
WHERE network='twitter'
FROM elsewhere
WHERE platform='twitter'
AND user_info -> 'user_id' = %s
""" # XXX Uniqueness constraint on screen_name?
Expand Down
10 changes: 5 additions & 5 deletions gittip/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ def get_details(self):


@require_id
def get_social_network_accounts(self):
"""Return a two-tuple of social_network_account dicts.
def get_accounts_elsewhere(self):
"""Return a two-tuple of elsewhere dicts.
"""
ACCOUNTS = """
SELECT * FROM social_network_users WHERE participant_id=%s;
SELECT * FROM elsewhere WHERE participant_id=%s;
"""
accounts = gittip.db.fetchall(ACCOUNTS, (self.id,))
assert accounts is not None
twitter_account = None
github_account = None
for account in accounts:
if account['network'] == 'github':
if account['platform'] == 'github':
github_account = account
else:
assert account['network'] == 'twitter'
assert account['platform'] == 'twitter'
twitter_account = account
return (github_account, twitter_account)

Expand Down
2 changes: 1 addition & 1 deletion gittip/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def tearDown(self):
# TODO: hack for now, truncate all tables.
tables = [
'participants',
'social_network_users',
'elsewhere',
'tips',
'transfers',
'paydays',
Expand Down
7 changes: 7 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,10 @@ ALTER TABLE participants ADD COLUMN payin_suspended bool NOT NULL DEFAULT FALSE;
ALTER TABLE participants ADD COLUMN is_suspicious bool DEFAULT NULL;
UPDATE participants SET is_suspicious=true WHERE payin_suspended;
ALTER TABLE participants DROP COLUMN payin_suspended;


-------------------------------------------------------------------------------
-- https://github.com/whit537/www.gittip.com/issues/406

ALTER TABLE social_network_users RENAME TO elsewhere;
ALTER TABLE elsewhere RENAME COLUMN network TO platform;
30 changes: 30 additions & 0 deletions tests/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ def test_credit_card():
actual = serve_request('/credit-card.html').body
assert expected in actual, actual

def test_github_associate():
expected = "Bad request, program!"
actual = serve_request('/on/github/associate').body
assert expected in actual, actual

def test_twitter_associate():
expected = "Bad request, program!"
actual = serve_request('/on/twitter/associate').body
assert expected in actual, actual

def test_about():
expected = "pretty-smart grants"
actual = serve_request('/about/').body
assert expected in actual, actual

def test_about_stats():
expected = "have joined Gittip"
actual = serve_request('/about/stats.html').body
assert expected in actual, actual

def test_about_charts():
expected = "growth since it launched"
actual = serve_request('/about/charts.html').body
assert expected in actual, actual

def test_about_unclaimed():
expected = "Unclaimed"
actual = serve_request('/about/unclaimed.html').body
assert expected in actual, actual


# These hit the network. XXX add a knob to skip these

Expand Down
19 changes: 5 additions & 14 deletions www/%participant_id/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
"""Show information about a single participant.

There is an implicit participant for every account on the social networks. When
someone claims a social network account on Gittip, the underlying participant
is upgraded to an explicit participant. Participant accounts can be folded into
other participant accounts. All of the social network profiles associated with
the old participant are re-associated with the new participant, and the old
participant is pruned. All the money attached to the old social network
profiles is rolled over to the new participant.

"""
import locale

Expand Down Expand Up @@ -46,9 +37,9 @@

elif deets['claimed_time'] is None:

# This is a stub participant record for someone on a social network who
# hasn't actually registered yet. Let's bounce the viewer over to the
# appropriate social network page.
# This is a stub participant record for someone on another platform who
# hasn't actually registered with Gittip yet. Let's bounce the viewer over
# to the appropriate platform page.

to = resolve_unclaimed(participant.id)
if to is None:
Expand Down Expand Up @@ -79,8 +70,8 @@
can_tip = True
tip_or_pledge = "tip"
title = participant.id # used in <title>
username = participant.id # used in footer shared with on/network/ pages
github_account, twitter_account = participant.get_social_network_accounts()
username = participant.id # used in footer shared with on/$platform/ pages
github_account, twitter_account = participant.get_accounts_elsewhere()

# ========================================================================== ^L
{% extends templates/participant.html %}
Expand Down
4 changes: 2 additions & 2 deletions www/about/unclaimed.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
FROM tips
JOIN participants p ON p.id = tipper
JOIN participants p2 ON p2.id = tippee
JOIN social_network_users snu ON snu.participant_id = tippee
JOIN elsewhere ON elsewhere.participant_id = tippee
WHERE p.last_bill_result = ''
AND p2.claimed_time IS NULL
AND p.is_suspicious IS NOT true
AND p2.is_suspicious IS NOT true
AND snu.is_locked = false
AND elsewhere.is_locked = false
ORDER BY tipper, tippee, mtime DESC
) AS foo
GROUP BY tippee
Expand Down
4 changes: 2 additions & 2 deletions www/assets/%version/gittip.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,12 @@ Gittip.initJumpToPerson = function()
{
function jump(e)
{
var network = $('#jump SELECT').val().trim();
var platform = $('#jump SELECT').val().trim();
var val = $('#jump INPUT').val().trim();
e.preventDefault();
e.stopPropagation();
if (val !== '')
window.location = '/on/' + network + '/' + val + '/';
window.location = '/on/' + platform + '/' + val + '/';
return false;
}
$('#jump').submit(jump);
Expand Down
8 changes: 4 additions & 4 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
, tippee
FROM tips
JOIN participants p ON p.id = tipper
JOIN social_network_users snu ON snu.participant_id = tippee
JOIN elsewhere ON elsewhere.participant_id = tippee
WHERE last_bill_result = ''
AND snu.is_locked = false
AND elsewhere.is_locked = false
AND is_suspicious IS NOT true
ORDER BY tipper, tippee, mtime DESC
) AS foo
Expand All @@ -39,10 +39,10 @@
, tipper
FROM tips
JOIN participants p ON p.id = tipper
JOIN social_network_users snu ON snu.participant_id = tippee
JOIN elsewhere ON elsewhere.participant_id = tippee
WHERE last_bill_result = ''
AND is_suspicious IS NOT true
AND snu.is_locked = false
AND elsewhere.is_locked = false
ORDER BY tipper, tippee, mtime DESC
) AS foo
JOIN participants p ON p.id = tipper
Expand Down
14 changes: 7 additions & 7 deletions www/on/github/%login/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# Try to load from GitHub.
# ========================

rec = db.fetchone( "SELECT user_info FROM social_network_users "
"WHERE network='github' AND user_info->'login' = %s"
rec = db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='github' AND user_info->'login' = %s"
, (path['login'],)
)
if rec is not None:
Expand Down Expand Up @@ -64,12 +64,12 @@
logins = [member['login'] for member in members]
ON_GITTIP = """\

SELECT snu.user_info -> 'login' AS login
FROM social_network_users snu
SELECT elsewhere.user_info -> 'login' AS login
FROM elsewhere
JOIN participants p
ON p.id = snu.participant_id
WHERE snu.network='github'
AND snu.user_info -> 'login' = any(%s)
ON p.id = elsewhere.participant_id
WHERE elsewhere.platform = 'github'
AND elsewhere.user_info -> 'login' = any(%s)
AND p.claimed_time IS NOT NULL

"""
Expand Down
10 changes: 5 additions & 5 deletions www/on/github/associate
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Associate a Github account with a Gittip account.

First we do the OAuth dance with Github. Once we've authenticated the user
against Github, we record them in our social_network_users table. This table
contains information for Github users whether or not they are explicit
participants in the Gittip community.
against Github, we record them in our elsewhere table. This table contains
information for Github users whether or not they are explicit participants in
the Gittip community.

"""
from aspen import log, Response
from gittip import db
from gittip.networks import change_participant_id, github, set_as_claimed
from gittip.elsewhere import change_participant_id, github, set_as_claimed
from gittip.authentication import User
from psycopg2 import IntegrityError

Expand Down Expand Up @@ -59,7 +59,7 @@ else: # lock or unlock
= github.upsert(user_info)
assert participant_id != login, login # sanity check

db.execute( "UPDATE social_network_users "
db.execute( "UPDATE elsewhere "
"SET is_locked=%s "
"WHERE participant_id=%s"
, (action == 'lock', participant_id)
Expand Down
4 changes: 2 additions & 2 deletions www/on/twitter/%screen_name/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# Try to load from Twitter.
# =========================

rec = db.fetchone( "SELECT user_info FROM social_network_users "
"WHERE network='twitter' AND user_info->'screen_name' = %s"
rec = db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='twitter' AND user_info->'screen_name' = %s"
, (path['screen_name'],)
)
if rec is not None:
Expand Down
Loading

0 comments on commit 5c36f6a

Please sign in to comment.