This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4495 from gratipay/bulk-prefactor
Refactor ahead of bulk npm
- Loading branch information
Showing
8 changed files
with
4,887 additions
and
2,824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from gratipay.exceptions import NoPackages | ||
|
||
|
||
class Packages(object): | ||
|
||
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`). | ||
""" | ||
if not packages: | ||
raise NoPackages() | ||
|
||
# We want to make a single db call to insert all claims, so we need to | ||
# do a little SQL construction. Do it in such a way that we still avoid | ||
# Python string interpolation (~= SQLi vector). | ||
|
||
extra_sql, values = [], [] | ||
for p in packages: | ||
extra_sql.append('(%s, %s)') | ||
values += [nonce, p.id] | ||
c.run('INSERT INTO claims (nonce, package_id) VALUES' + ', '.join(extra_sql), values) | ||
self.app.add_event( c | ||
, 'participant' | ||
, dict( id=self.id | ||
, action='start-claim' | ||
, values=dict(package_ids=[p.id for p in packages]) | ||
) | ||
) | ||
|
||
|
||
def get_packages_claiming(self, cursor, nonce): | ||
"""Given a nonce, return :py:class:`~gratipay.models.package.Package` | ||
objects associated with it. | ||
""" | ||
return cursor.all(""" | ||
SELECT p.*::packages | ||
FROM packages p | ||
JOIN claims c | ||
ON p.id = c.package_id | ||
WHERE c.nonce=%s | ||
ORDER BY p.name ASC | ||
""", (nonce,)) | ||
|
||
|
||
def finish_package_claims(self, cursor, nonce, *packages): | ||
"""Create teams if needed and associate them with the packages. | ||
""" | ||
if not packages: | ||
raise NoPackages() | ||
|
||
package_ids, teams, team_ids = [], [], [] | ||
for package in packages: | ||
package_ids.append(package.id) | ||
team = package.get_or_create_linked_team(cursor, self) | ||
teams.append(team) | ||
team_ids.append(team.id) | ||
review_url = self.app.project_review_repo.create_issue(*teams) | ||
|
||
cursor.run('DELETE FROM claims WHERE nonce=%s', (nonce,)) | ||
cursor.run('UPDATE teams SET review_url=%s WHERE id=ANY(%s)', (review_url, team_ids,)) | ||
self.app.add_event( cursor | ||
, 'participant' | ||
, dict( id=self.id | ||
, action='finish-claim' | ||
, values=dict(package_ids=package_ids) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,13 @@ def make_participant(self, username, **kw): | |
RETURNING participants.*::participants | ||
""", (username, username.lower())) | ||
|
||
if 'id' in kw: | ||
new_id = kw.pop('id') | ||
self.db.run( 'UPDATE participants SET id=%s WHERE id=%s' | ||
, (new_id, participant.id) | ||
) | ||
participant.set_attributes(id=new_id) | ||
|
||
if 'elsewhere' in kw or 'claimed_time' in kw: | ||
platform = kw.pop('elsewhere', 'github') | ||
self.db.run(""" | ||
|
@@ -256,6 +263,13 @@ def make_participant(self, username, **kw): | |
route = ExchangeRoute.insert(participant, 'paypal', '[email protected]') | ||
route.update_error(kw.pop('last_paypal_result')) | ||
|
||
# Handle email address | ||
if 'email_address' in kw: | ||
address = kw.pop('email_address') | ||
if address: | ||
self.add_and_verify_email(participant, address) | ||
self.app.email_queue.purge() | ||
|
||
# Update participant | ||
verified_in = kw.pop('verified_in', []) | ||
if kw: | ||
|
Oops, something went wrong.