diff --git a/gratipay/models/package/__init__.py b/gratipay/models/package/__init__.py new file mode 100644 index 0000000000..24af56d724 --- /dev/null +++ b/gratipay/models/package/__init__.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +from postgres.orm import Model + + +NPM = 'npm' # We are starting with a single package manager. If we see + # traction we will expand. + + +class Package(Model): + """Represent a gratipackage. :-) + """ + + typname = 'packages' + + def __eq__(self, other): + if not isinstance(other, Package): + return False + return self.id == other.id + + def __ne__(self, other): + if not isinstance(other, Package): + return True + return self.id != other.id + + + # Constructors + # ============ + + @classmethod + def from_id(cls, id): + """Return an existing package based on id. + """ + return cls.db.one("SELECT packages.*::packages FROM packages WHERE id=%s", (id,)) + + @classmethod + def from_names(cls, package_manager, name): + """Return an existing package based on package manager and package names. + """ + return cls.db.one("SELECT packages.*::packages FROM packages " + "WHERE package_manager=%s and name=%s", (package_manager, name)) diff --git a/gratipay/testing/harness.py b/gratipay/testing/harness.py index fb9da6c765..bf8da42fe2 100644 --- a/gratipay/testing/harness.py +++ b/gratipay/testing/harness.py @@ -181,13 +181,14 @@ def make_team(self, *a, **kw): return team - def make_package(self, package_manager, name, description, emails): + def make_package(self, package_manager='npm', name='foo', description='Foo', + emails=['alice@example.com']): """Factory for packages. """ - self.db.one( 'INSERT INTO packages (package_manager, name, description, emails) ' - 'VALUES (%s, %s, %s, %s) RETURNING *' - , (package_manager, name, description, emails) - ) + return self.db.one( 'INSERT INTO packages (package_manager, name, description, emails) ' + 'VALUES (%s, %s, %s, %s) RETURNING *' + , (package_manager, name, description, emails) + ) def make_participant(self, username, **kw): diff --git a/gratipay/wireup.py b/gratipay/wireup.py index 66c0f18df8..8e5b36df59 100644 --- a/gratipay/wireup.py +++ b/gratipay/wireup.py @@ -33,6 +33,7 @@ from gratipay.models.community import Community from gratipay.models.country import Country from gratipay.models.exchange_route import ExchangeRoute +from gratipay.models.package import Package from gratipay.models.participant import Participant from gratipay.models.participant.mixins import Identity from gratipay.models.team import Team @@ -56,7 +57,7 @@ def db(env): maxconn = env.database_maxconn db = GratipayDB(dburl, maxconn=maxconn) - for model in (AccountElsewhere, Community, Country, ExchangeRoute, Participant, Team): + for model in (AccountElsewhere, Community, Country, ExchangeRoute, Package, Participant, Team): db.register_model(model) gratipay.billing.payday.Payday.db = db diff --git a/tests/py/test_packages.py b/tests/py/test_packages.py new file mode 100644 index 0000000000..b97e888377 --- /dev/null +++ b/tests/py/test_packages.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +from gratipay.models.package import NPM, Package +from gratipay.testing import Harness + + +class TestPackage(Harness): + + def test_can_be_instantiated_from_id(self): + p = self.make_package() + assert Package.from_id(p.id).id == p.id + + def test_can_be_instantiated_from_names(self): + self.make_package() + assert Package.from_names(NPM, 'foo').name == 'foo' diff --git a/tests/py/test_www_npm_package.py b/tests/py/test_www_npm_package.py index f719cb6930..8f9c5bec45 100644 --- a/tests/py/test_www_npm_package.py +++ b/tests/py/test_www_npm_package.py @@ -7,7 +7,7 @@ class TestAnon(Harness): def setUp(self): - self.make_package('npm', 'foo', 'The foo package', ['alice@example.com']) + self.make_package() def test_gets_signin_page(self): assert 'npm/foo has not been claimed' in self.client.GET('/on/npm/foo/').body diff --git a/www/on/npm/%package/index.html.spt b/www/on/npm/%package/index.html.spt index 626306c03c..e9d0693bff 100644 --- a/www/on/npm/%package/index.html.spt +++ b/www/on/npm/%package/index.html.spt @@ -2,10 +2,10 @@ import requests from aspen import Response from gratipay.utils import markdown +from gratipay.models.package import Package [---] package_name = request.path['package'] -package = website.db.one("select * from packages where package_manager='npm' " - "and name=%s", (package_name,)) +package = Package.from_names('npm', package_name) if package is None: raise Response(404) banner = package_name