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

Commit

Permalink
Database table and model
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Sep 8, 2017
1 parent 13e36a6 commit 681c6ff
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 22 deletions.
23 changes: 23 additions & 0 deletions deploy/before.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BEGIN;
CREATE TYPE follow_up AS ENUM ('monthly', 'quarterly', 'yearly', 'never');
CREATE TABLE payments_for_open_source
( uuid text PRIMARY KEY
, ctime timestamptz NOT NULL DEFAULT now()

-- card charge
, amount bigint NOT NULL
, transaction_id text UNIQUE NOT NULL

-- contact info
, name text NOT NULL
, follow_up follow_up NOT NULL
, email_address text NOT NULL
, message_id bigint REFERENCES email_messages(id)

-- promotion details
, promotion_name text NOT NULL DEFAULT ''
, promotion_url text NOT NULL DEFAULT ''
, promotion_twitter text NOT NULL DEFAULT ''
, promotion_message text NOT NULL DEFAULT ''
);
END;
43 changes: 22 additions & 21 deletions gratipay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from gratipay import utils


def pay_for_open_source(app, raw):
parsed, errors = _parse(raw)
if not errors:
transaction_id = _charge_card(app, parsed)
if not transaction_id:
errors.append('charge_card')
if not errors:
message_id = None
if parsed['email_address']:
message_id = _send_receipt(app, parsed['email_address'])
if not message_id:
errors.append('send_receipt')
_store_info(parsed, transaction_id, message_id)
parsed = {}
return {'parsed': parsed, 'errors': errors}
from gratipay.models.payment_for_open_source import PaymentForOpenSource


def _parse(raw):
Expand Down Expand Up @@ -90,14 +74,31 @@ def _parse(raw):
return parsed, errors


def _charge_card(app, parsed):
def _charge(app, parsed):
raise NotImplementedError


def _send_receipt(app, parsed):
def _send(app, parsed):
raise NotImplementedError
app.email_queue.put()


def _store_info(parsed, transaction_id, message_id):
raise NotImplementedError
def _store(parsed, transaction_id, message_id):
PaymentForOpenSource.insert(transaction_id=transaction_id, message_id=message_id, **parsed)


def pay_for_open_source(app, raw, _parse=_parse, _charge=_charge, _send=_send, _store=_store):
parsed, errors = _parse(raw)
if not errors:
transaction_id = _charge(app, parsed)
if not transaction_id:
errors.append('charge_card')
if not errors:
message_id = None
if parsed['email_address']:
message_id = _send(app, parsed['email_address'])
if not message_id:
errors.append('send_receipt')
_store(parsed, transaction_id, message_id)
parsed= {}
return {'parsed': parsed, 'errors': errors}
4 changes: 3 additions & 1 deletion gratipay/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from .exchange_route import ExchangeRoute
from .package import Package
from .participant import Participant
from .payment_for_open_source import PaymentForOpenSource
from .team import Team


MODELS = (AccountElsewhere, Community, Country, ExchangeRoute, Package, Participant, Team)
MODELS = (AccountElsewhere, Community, Country, ExchangeRoute, Package, Participant,
PaymentForOpenSource, Team)


@contextmanager
Expand Down
37 changes: 37 additions & 0 deletions gratipay/models/payment_for_open_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

import uuid as uuidlib
from postgres.orm import Model


class PaymentForOpenSource(Model):

typname = "payments_for_open_source"

def __repr__(self):
return '<PaymentForOpenSource: %s>'.format(repr(self.amount))


@classmethod
def from_uuid(cls, uuid, cursor=None):
return (cursor or cls.db).one("""
SELECT pfos.*::payments_for_open_source
FROM payments_for_open_source pfos
WHERE uuid = %s
""", (uuid,))


@classmethod
def insert(cls, amount, transaction_id, name, follow_up, email_address, message_id,
promotion_name, promotion_url, promotion_twitter, promotion_message,
cursor=None):
uuid = uuidlib.uuid4().hex
return (cursor or cls.db).one("""
INSERT INTO payments_for_open_source
(uuid, amount, transaction_id, name, follow_up, email_address, message_id,
promotion_name, promotion_url, promotion_twitter, promotion_message)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING payments_for_open_source.*::payments_for_open_source
""", (uuid, amount, transaction_id, name, follow_up, email_address, message_id,
promotion_name, promotion_url, promotion_twitter, promotion_message))
20 changes: 20 additions & 0 deletions gratipay/testing/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.package import NPM, Package
from gratipay.models.participant import Participant, MAX_TIP, MIN_TIP
from gratipay.models.payment_for_open_source import PaymentForOpenSource
from gratipay.models.team import Team
from gratipay.security import user
from gratipay.testing import P
Expand Down Expand Up @@ -142,6 +143,24 @@ def clear_tables(self):
self.db.run("INSERT INTO worker_coordination DEFAULT VALUES")


def make_payment_for_open_source(self, **info):
defaults = dict( amount='1000'
, name='Alice Liddell'
, transaction_id='deadbeef'
, email_address='[email protected]'
, follow_up='monthly'
, message_id=None # TODO call gratipay._send and grab id
, promotion_name='Wonderland'
, promotion_url='http://www.example.com/'
, promotion_twitter='thebestbutter'
, promotion_message='Love me! Love me! Say that you love me!'
)
for key, value in defaults.items():
if key not in info:
info[key] = value
return PaymentForOpenSource.insert(**info)


def make_elsewhere(self, platform, user_id, user_name, **kw):
"""Factory for :py:class:`~gratipay.models.account_elsewhere.AccountElsewhere`.
"""
Expand Down Expand Up @@ -318,6 +337,7 @@ def make_exchange(self, route, amount, fee, participant, status='succeeded', err
record_exchange_result(self.db, e_id, status, error, participant)
return e_id


def make_payment(self, participant, team, amount, direction, payday, timestamp=utcnow()):
"""Factory for payment"""

Expand Down
16 changes: 16 additions & 0 deletions tests/py/test_payments_for_open_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from gratipay.testing import Harness
from gratipay.models.payment_for_open_source import PaymentForOpenSource


class Tests(Harness):

def test_can_insert(self):
self.make_payment_for_open_source()
assert self.db.one('SELECT * FROM payments_for_open_source').name == 'Alice Liddell'

def test_can_fetch(self):
uuid = self.make_payment_for_open_source().uuid
assert PaymentForOpenSource.from_uuid(uuid).name == 'Alice Liddell'

0 comments on commit 681c6ff

Please sign in to comment.