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

Commit

Permalink
implement schema/models for participant identities
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Apr 29, 2016
1 parent de796f9 commit fd6d635
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 2 deletions.
7 changes: 7 additions & 0 deletions gratipay/models/country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from postgres.orm import Model


class Country(Model):
typname = 'countries'
49 changes: 49 additions & 0 deletions gratipay/models/participant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
from __future__ import print_function, unicode_literals

import json
from datetime import timedelta
from decimal import Decimal
import pickle
Expand Down Expand Up @@ -1072,6 +1073,54 @@ def update_is_free_rider(self, is_free_rider, cursor=None):
self.set_attributes(is_free_rider=is_free_rider)


# Identities
# ==========

def save_identity(self, country, info=None, is_verified=False):
"""
"""
if info is None:
assert is_verified is None
else:
info = json.dumps(info)
participant_id = self.id
self.db.run("""
INSERT INTO participant_identities
(ctime, participant, country, info, is_verified)
VALUES ( COALESCE (( SELECT ctime
FROM participant_identities
WHERE participant=%(participant_id)s
AND country=(SELECT id FROM countries WHERE code3=%(country)s)
LIMIT 1
), CURRENT_TIMESTAMP)
, %(participant_id)s
, (SELECT id FROM countries WHERE code3=%(country)s)
, %(info)s
, %(is_verified)s
)
""", locals())


def get_identities(self):
"""
"""
return self.db.all( """
SELECT ctime
, mtime
, c.*::countries AS country
, info
, is_verified
FROM current_participant_identities
JOIN countries c ON country=c.id
WHERE participant=%s
ORDER BY c.name
""", (self.id,))


# Random Junk
# ===========

Expand Down
2 changes: 1 addition & 1 deletion gratipay/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Harness(unittest.TestCase):
db = client.website.db
platforms = client.website.platforms
tablenames = db.all("SELECT tablename FROM pg_tables "
"WHERE schemaname='public'")
"WHERE schemaname='public' AND tablename != 'countries'")
seq = itertools.count(0)


Expand Down
3 changes: 2 additions & 1 deletion gratipay/wireup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from gratipay.elsewhere.venmo import Venmo
from gratipay.models.account_elsewhere import AccountElsewhere
from gratipay.models.community import Community
from gratipay.models.country import Country
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant
from gratipay.models.team import Team
Expand All @@ -53,7 +54,7 @@ def db(env):
maxconn = env.database_maxconn
db = GratipayDB(dburl, maxconn=maxconn)

for model in (AccountElsewhere, Community, ExchangeRoute, Participant, Team):
for model in (AccountElsewhere, Community, Country, ExchangeRoute, Participant, Team):
db.register_model(model)
gratipay.billing.payday.Payday.db = db

Expand Down
64 changes: 64 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
CREATE TABLE countries -- http://www.iso.org/iso/country_codes
( id bigserial primary key
, code2 text NOT NULL UNIQUE
, code3 text NOT NULL UNIQUE
, name text NOT NULL UNIQUE
);

\i sql/countries.sql

CREATE TABLE participant_identities
( id bigserial primary key
, ctime timestamp with time zone NOT NULL
, mtime timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
, participant bigint NOT NULL REFERENCES participants(id)
, country bigint NOT NULL REFERENCES countries(id)
, info json
, is_verified boolean DEFAULT FALSE
);


-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} false
--
-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-12 2388765 8767 {"address": "bar"} true
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} false
--
-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-13 2388765 8767 {"address": "bar"} false
-- 2014-08-10 2014-08-12 2388765 8767 {"address": "bar"} true
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} true
--
-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-14 2388765 8767 {"address": "bar"} true
-- 2014-08-10 2014-08-13 2388765 8767 {"address": "bar"} false
-- 2014-08-10 2014-08-12 2388765 8767 {"address": "foo"} true
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} false
--
-- # user removes their registration
--
-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-15 2388765 8767 NULL NULL
-- 2014-08-10 2014-08-14 2388765 8767 {"address": "bar"} true
-- 2014-08-10 2014-08-13 2388765 8767 {"address": "bar"} false
-- 2014-08-10 2014-08-12 2388765 8767 {"address": "foo"} true
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} false
--
-- # user reregisters for the same country - same ctime!
--
-- ctime mtime participant country info is_verified
-- 2014-08-10 2014-08-16 2388765 8767 {"address": "baz"} false
-- 2014-08-10 2014-08-15 2388765 8767 NULL NULL
-- 2014-08-10 2014-08-14 2388765 8767 {"address": "bar"} true
-- 2014-08-10 2014-08-13 2388765 8767 {"address": "bar"} false
-- 2014-08-10 2014-08-12 2388765 8767 {"address": "foo"} true
-- 2014-08-10 2014-08-10 2388765 8767 {"address": "foo"} false


CREATE VIEW current_participant_identities AS
SELECT * FROM (
SELECT DISTINCT ON (participant, country) *
FROM participant_identities
ORDER BY participant, country, mtime DESC
) AS _ WHERE info IS NOT NULL;
Loading

0 comments on commit fd6d635

Please sign in to comment.