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.
- Loading branch information
1 parent
7a8e7cf
commit 64c256b
Showing
1 changed file
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from gratipay.models.team import Team | ||
|
||
from postgres.orm import Model | ||
|
||
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._from_thing("id", id) | ||
|
||
@classmethod | ||
def from_team_slug(cls, slug): | ||
"""Return an existing package based on team's slug. | ||
""" | ||
return cls._from_thing("team", slug) | ||
|
||
@classmethod | ||
def _from_thing(cls, thing, value): | ||
assert thing in ("id", "team") | ||
return cls.db.one(""" | ||
SELECT packages.*::packages | ||
FROM packages | ||
WHERE {}=%s | ||
""".format(thing), (value,)) | ||
|
||
@classmethod | ||
def insert(cls, owner, **fields): | ||
return cls.db.one(""" | ||
INSERT INTO packages | ||
(package_manager_id, name, description, | ||
long_description, long_description_raw, | ||
long_description_type) | ||
VALUES (%(package_manager_id)s, %(name)s, %(description)s, | ||
%(long_description)s, %(long_description_raw)s, | ||
%(long_description_type)s) | ||
RETURNING packages.*::packages | ||
""", fields) | ||
|
||
def set_team(self, team): | ||
""" Set team for a package. | ||
""" | ||
if not isinstance(team, Team): | ||
raise NotAllowed("Not a team!") | ||
elif team.is_closed: | ||
raise NotAllowed("team is closed") | ||
elif not team.is_approved: | ||
raise NotAllowed("team not approved") | ||
|
||
package_id = self.id | ||
slug = team.slug | ||
with self.db.get_cursor() as c: | ||
# TODO add event | ||
c.run(""" | ||
UPDATE packages | ||
SET team=%(slug)s | ||
WHERE id=%(package_id)s | ||
""", locals()) | ||
self.set_attributes(team=team) | ||
|
||
|
||
class NotAllowed(Exception): | ||
pass |