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

Commit

Permalink
Move decimal constants up from simplate to library
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 6, 2016
1 parent 4998156 commit f7efe7a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions gratipay/models/team/mixins/membership.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from decimal import Decimal
from .takes import ZERO, PENNY


class StubParticipantAdded(Exception): pass
Expand All @@ -14,13 +14,13 @@ def add_member(self, member):
"""
if not member.is_claimed:
raise StubParticipantAdded
self.set_take_for(member, Decimal('0.01'), self)
self.set_take_for(member, PENNY, self)


def remove_member(self, member):
"""Remove a member from this team.
"""
self.set_take_for(member, Decimal('0.00'), self)
self.set_take_for(member, ZERO, self)


def remove_all_members(self, cursor=None):
Expand Down
14 changes: 9 additions & 5 deletions gratipay/models/team/mixins/takes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from collections import OrderedDict
from decimal import Decimal
from decimal import Decimal as D


ZERO = D('0.00')
PENNY = D('0.01')


class TakesMixin(object):
Expand All @@ -26,7 +30,7 @@ def get_take_last_week_for(self, member):
)
ORDER BY mtime DESC LIMIT 1
""", (self.username, membername), default=Decimal('0.00'))
""", (self.username, membername), default=ZERO)


def get_take_for(self, member):
Expand All @@ -35,7 +39,7 @@ def get_take_for(self, member):
return self.db.one( "SELECT amount FROM current_takes "
"WHERE member=%s AND team=%s"
, (member.username, self.username)
, default=Decimal('0.00')
, default=ZERO
)


Expand Down Expand Up @@ -80,8 +84,8 @@ def update_taking(self, old_takes, new_takes, cursor=None, member=None):
for username in set(old_takes.keys()).union(new_takes.keys()):
if username == self.username:
continue
old = old_takes.get(username, {}).get('actual_amount', Decimal(0))
new = new_takes.get(username, {}).get('actual_amount', Decimal(0))
old = old_takes.get(username, {}).get('actual_amount', ZERO)
new = new_takes.get(username, {}).get('actual_amount', ZERO)
diff = new - old
if diff != 0:
r = (cursor or self.db).one("""
Expand Down
10 changes: 4 additions & 6 deletions www/%team/distributing/%member_id.json.spt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""Endpoint to record team memberships.
"""
from decimal import Decimal, InvalidOperation
from decimal import InvalidOperation

from aspen import Response
from babel.numbers import NumberFormatError
from gratipay.utils import get_team
from gratipay.models.participant import Participant

ZERO = Decimal('0.00')
A_PENNY = Decimal('0.01')
from gratipay.models.team.mixins.takes import ZERO, PENNY

[--------------------]
request.allow('GET', 'POST')
Expand Down Expand Up @@ -44,7 +42,7 @@ if request.method == 'POST':
# Only members themselves can set their take to something greater than
# a penny.

if take not in (ZERO, A_PENNY):
if take not in (ZERO, PENNY):
raise Response(400, _("You're only allowed to add or remove members, "
"you can't set their takes to specific amounts."))
if take == ZERO:
Expand All @@ -61,7 +59,7 @@ if request.method == 'POST':
# Attempted to add a stub participant
raise Response(403)

if take == A_PENNY:
if take == PENNY:
if is_member:
msg = _("{0} is already a member of this team.", member.username)
else:
Expand Down

0 comments on commit f7efe7a

Please sign in to comment.