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

Commit

Permalink
First pass at get_packages_for_claiming
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed May 31, 2017
1 parent fc36e0c commit cff3a47
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
37 changes: 37 additions & 0 deletions gratipay/models/participant/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@

class Packages(object):

def get_packages_for_claiming(self, manager):

"""Return a list of packages on the named ``manager`` for which the
participant has verified an email address on Gratipay, along with the
current package owner on Gratipay (if any).
:param string manager: the name of the package manager on which to look
for potential packages
:return: a list of (:py:class:`~gratipay.models.package.Package`,
:py:class:`Participant`) tuples, where the participant is the one who
has already claimed the package (or ``None``)
"""
return self.db.all('''
WITH verified_emails AS (
SELECT address
FROM emails
WHERE participant_id=%s
AND verified is true
)
SELECT pkg.*::packages package
, p.*::participants claimed_by
FROM packages pkg
LEFT JOIN teams_to_packages tp
ON pkg.id = tp.package_id
LEFT JOIN teams t
ON t.id = tp.team_id
LEFT JOIN participants p
ON t.owner = p.username
WHERE package_manager=%s
AND emails && array(SELECT * FROM verified_emails)
''', (self.id, manager))


def start_package_claims(self, c, nonce, *packages):
"""Takes a cursor, nonce and list of packages, inserts into ``claims``
and returns ``None`` (or raise :py:exc:`NoPackages`).
Expand Down
35 changes: 35 additions & 0 deletions tests/py/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,38 @@ def test_ordering(self):
, ('email', UNLINKED)
, ('bmail', OTHER)
]


class GetPackagesForClaiming(Harness):

def test_gets_packages_for_claiming(self):
foo = self.make_package()
alice = self.make_participant('alice', email_address='[email protected]')
assert alice.get_packages_for_claiming(NPM) == [(foo, None)]

def test_excludes_packages_for_half_verified_emails(self):
foo = self.make_package()
alice = self.make_participant('alice', email_address='[email protected]')
self.make_package(name='bar', emails=['[email protected]'])
alice.start_email_verification('[email protected]')
assert alice.get_packages_for_claiming(NPM) == [(foo, None)]

def test_includes_packages_for_verified_but_non_primary_emails(self):
foo = self.make_package()
alice = self.make_participant('alice', email_address='[email protected]')
bar = self.make_package(name='bar', emails=['[email protected]'])
self.add_and_verify_email(alice, '[email protected]')
assert alice.get_packages_for_claiming(NPM) == [(foo, None), (bar, None)]

def test_includes_packages_already_claimed_by_self(self):
foo = self.make_package()
alice = self.make_participant('alice', email_address='[email protected]')
foo.get_or_create_linked_team(self.db, alice)
assert alice.get_packages_for_claiming(NPM) == [(foo, alice)]

def test_includes_packages_already_claimed_by_other(self):
foo = self.make_package(emails=['[email protected]', '[email protected]'])
alice = self.make_participant('alice', email_address='[email protected]')
foo.get_or_create_linked_team(self.db, alice)
bob = self.make_participant('bob', email_address='[email protected]')
assert bob.get_packages_for_claiming(NPM) == [(foo, alice)]

0 comments on commit cff3a47

Please sign in to comment.