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

Stub out Package model #4155

Merged
merged 11 commits into from
Feb 8, 2017
42 changes: 42 additions & 0 deletions gratipay/models/package/__init__.py
Original file line number Diff line number Diff line change
@@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there plan to create table for package manager and reference its id in packages table?

Before making that change, shall we make 'npm' a constant shared across the app to avoid issue with raw string? Like using "Npm" by mistake, etc.

Or maybe, we use API from_name(cls, name) before adding support for other package managers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there plan to create table for package manager and reference its id in packages table?

Not before May. We won't look at expanding beyond npm until later this year or possibly next.

Before making that change, shall we make 'npm' a constant shared across the app to avoid issue with raw string? Like using "Npm" by mistake, etc.

Like 9c05276?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like 9c05276?

Yes. Not sure how helpful it would be though.

11 changes: 6 additions & 5 deletions gratipay/testing/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=['[email protected]']):
"""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):
Expand Down
3 changes: 2 additions & 1 deletion gratipay/wireup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
16 changes: 16 additions & 0 deletions tests/py/test_packages.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.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'
2 changes: 1 addition & 1 deletion tests/py/test_www_npm_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestAnon(Harness):

def setUp(self):
self.make_package('npm', 'foo', 'The foo package', ['[email protected]'])
self.make_package()

def test_gets_signin_page(self):
assert 'npm/foo</a> has not been claimed' in self.client.GET('/on/npm/foo/').body
4 changes: 2 additions & 2 deletions www/on/npm/%package/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down