Skip to content

Commit

Permalink
Support free to amount price ranges
Browse files Browse the repository at this point in the history
Some optional activities can have a price range of Free to
an upper limit/max. e.g. Free-10CAD
  • Loading branch information
Nagyman committed Mar 17, 2015
1 parent a6829ce commit 475b77f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
History
-------

0.1.34 (2015-03-12)
0.1.36 (2015-03-17)
-------------------

* Support free to amount price range formatting (e.g. Free-10CAD)

0.1.35 (2015-03-12)
-------------------

* Added `duration_min` & `duration_max` to `ActivityDossier` model
Expand Down
2 changes: 1 addition & 1 deletion gapipy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = '0.1.35'
__version__ = '0.1.36'
__title__ = 'gapipy'


Expand Down
30 changes: 16 additions & 14 deletions gapipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ def get_available_resource_classes():
return [getattr(resource_module, r) for r in available_resources]


def is_free(amount):
"""
Explit zero amounts are interpreted as Free!
"""
return (
amount == 0 or
amount == 0.00 or
amount == '0' or
amount == '0.00'
)


def humanize_amount(amount, force_decimal=False):
"""
Takes an `amount` (float) and removes any unnecessary decimals,
or adds them if there is any partial amount.
TODO: Internationalization support
"""
if is_free(amount):
return 'Free'

amount = float(amount)
if amount % 1 or force_decimal:
return '%.2f' % amount
Expand All @@ -37,22 +52,9 @@ def humanize_price(amount_min, amount_max, currency):
"""
Format a single price or price range for display.
"""
# TODO: Better currency support; requires Currency API resource

def is_free(amount):
return (
amount == 0 or
amount == 0.00 or
amount == '0' or
amount == '0.00'
)

# No price, nothing to display
if amount_min is None:
return ''
# Explit zero amounts are interpreted as Free!
elif is_free(amount_min):
return 'Free'

# Price Range
if amount_max:
Expand All @@ -68,7 +70,7 @@ def is_free(amount):
# Single Price
return '{}{}'.format(
humanize_amount(amount_min),
currency,
currency if not is_free(amount_min) else '',
)


Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_humanize_price_free(self):
self.assertEqual(humanize_price(0.00, None, 'CAD'), FREE)
self.assertEqual(humanize_price('0', None, 'CAD'), FREE)
self.assertEqual(humanize_price('0.00', None, 'CAD'), FREE)
self.assertEqual(humanize_price('0.00', 5.00, 'CAD'), 'Free-5CAD')

def test_humanize_price_none(self):
out = humanize_price(None, None, 'CAD')
Expand Down

0 comments on commit 475b77f

Please sign in to comment.