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

Commit

Permalink
Merge branch 'andym-871839'
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar303 committed May 15, 2013
2 parents b000df5 + ca43b1d commit c6a5d0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
12 changes: 8 additions & 4 deletions apps/market/models.py
Expand Up @@ -75,10 +75,14 @@ def get_price_data(self, currency=None):
locale = get_locale_from_lang(lang)
if not currency:
currency = amo.LOCALE_CURRENCY.get(locale.language)
if currency:
price_currency = Price._currencies.get((currency, self.id), None)
if price_currency:
return price_currency.price, currency, locale
# If the currency is USD, we just use the price, not the price tier.
if currency and currency != 'USD':
price_currency = Price._currencies.get((currency, self.id))
if not price_currency:
# Asking for a currency that doesn't exist.
raise KeyError('No currency found: %s' % currency)

return price_currency.price, currency, locale

return self.price, currency or self.currency, locale

Expand Down
7 changes: 7 additions & 0 deletions apps/market/tests/test_models.py
Expand Up @@ -124,6 +124,13 @@ def test_prices(self):
eq_(currencies[0]['currency'], 'USD')
eq_(currencies[1], {'currency': 'CAD', 'amount': Decimal('3.01')})

def test_wrong_currency(self):
with self.assertRaises(KeyError):
Price.objects.get(pk=1).get_price('foo')

with self.assertRaises(KeyError):
Price.objects.get(pk=1).get_price_locale('foo')

@mock.patch('market.models.PROVIDER_CURRENCIES', {'bango': ['USD', 'EUR']})
def test_prices_provider(self):
currencies = Price.objects.get(pk=1).prices(provider='bango')
Expand Down
24 changes: 20 additions & 4 deletions mkt/webapps/models.py
Expand Up @@ -497,13 +497,29 @@ def has_price(self):
return bool(self.is_premium() and self.premium and
self.premium.has_price())

def get_price(self, currency):
def get_price(self, currency=None):
"""
A shortcut to get the price as decimal. Returns None if their is no
price for the app.
:param optional currency: If you do not pass in a currency, one
will be chosen for you. If you ask for a currency that does
not exist, you'll get an error.
"""
if self.has_price():
return self.premium.get_price(currency)
return self.premium.get_price(currency=currency)

def get_price_locale(self, currency):
def get_price_locale(self, currency=None):
"""
A shortcut to get the localised price with currency. Returns None if
their is no price for the app.
:param optional currency: If you do not pass in a currency, one
will be chosen for you. If you ask for a currency that does
not exist, you'll get an error.
"""
if self.has_price():
return self.premium.get_price_locale(currency)
return self.premium.get_price_locale(currency=currency)

@amo.cached_property
def promo(self):
Expand Down

0 comments on commit c6a5d0d

Please sign in to comment.