This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #1198 from muffinresearch/use-db-data-for-paid-reg…

…ions-921593

Use db for list of paid regions by slug (bug 921593)
  • Loading branch information...
muffinresearch committed Sep 30, 2013
2 parents cb39401 + aea88bc commit 57a637c59141d1aa0e263cbf18ee6d10363343ad
Showing with 30 additions and 13 deletions.
  1. +11 −1 apps/market/models.py
  2. +6 −1 apps/market/tests/test_models.py
  3. +2 −6 mkt/constants/regions.py
  4. +11 −5 mkt/developers/views_payments.py
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from operator import itemgetter
from django.core.cache import cache
from django.db import connection, models
from django.dispatch import receiver
@@ -18,7 +20,7 @@
PAYMENT_METHOD_CHOICES, PROVIDER_BANGO,
PROVIDER_CHOICES)
from mkt.constants import apps
from mkt.constants.regions import WORLDWIDE
from mkt.constants.regions import WORLDWIDE, REGIONS_CHOICES_ID_DICT as RID
from stats.models import Contribution
from users.models import UserProfile
@@ -146,6 +148,14 @@ def prices(self, provider=None):
return [model_to_dict(o) for o in
self.pricecurrency_set.filter(provider=provider)]
def region_ids_by_slug(self):
"""A tuple of price region ids sorted by slug."""
price_regions_ids = [(p['region'], RID.get(p['region']).slug)
for p in self.prices() if p['paid'] is True]
if price_regions_ids:
return zip(*sorted(price_regions_ids, key=itemgetter(1)))[0]
return tuple()
class PriceCurrency(amo.models.ModelBase):
# The carrier for this currency.
@@ -12,7 +12,8 @@
from constants.payments import PROVIDER_BANGO
from market.models import AddonPremium, PreApprovalUser, Price, Refund
from mkt.constants import apps
from mkt.constants.regions import ALL_REGION_IDS, BR, HU, SPAIN, US
from mkt.constants.regions import (ALL_REGION_IDS, BR, HU,
SPAIN, US, WORLDWIDE)
from stats.models import Contribution
from users.models import UserProfile
@@ -119,6 +120,10 @@ def test_prices_provider(self):
currencies = Price.objects.get(pk=1).prices(provider=PROVIDER_BANGO)
eq_(len(currencies), 2)
def test_region_ids_by_slug(self):
eq_(Price.objects.get(pk=2).region_ids_by_slug(),
(BR.id, SPAIN.id, WORLDWIDE.id))
class TestPriceCurrencyChanges(amo.tests.TestCase):
@@ -209,17 +209,13 @@ class GR(REGION):
REGIONS_CHOICES_NAME = ([(v.id, v.name) for v in BY_SLUG] +
[(WORLDWIDE.id, WORLDWIDE.name)])
REGIONS_DICT = dict(REGIONS_CHOICES)
REGIONS_CHOICES_ID_DICT = dict(REGIONS_CHOICES_ID)
ALL_REGIONS = frozenset(REGIONS_DICT.values())
ALL_PAID_REGIONS = frozenset(r for r in ALL_REGIONS if r.has_payments)
ALL_REGION_IDS = sorted(REGIONS_CHOICES_ID_DICT.keys())
ALL_PAID_REGIONS = frozenset(r for r in ALL_REGIONS if r.has_payments)
ALL_PAID_REGION_IDS = sorted(r.id for r in ALL_PAID_REGIONS)
ALL_PAID_REGIONS_BY_SLUG = sorted(ALL_PAID_REGIONS,
key=lambda x: getattr(x, 'slug', None))
ALL_PAID_REGION_IDS_BY_SLUG = [r.id for r in ALL_PAID_REGIONS_BY_SLUG]
# Regions not including worldwide.
REGION_IDS = sorted(REGIONS_CHOICES_ID_DICT.keys())[1:]
@@ -32,8 +32,7 @@
from mkt.developers.decorators import dev_required
from mkt.developers.models import (AddonPaymentAccount, PaymentAccount,
UserInappKey, uri_to_pk)
from mkt.regions import ALL_PAID_REGION_IDS_BY_SLUG
from mkt.regions import REGIONS_CHOICES_ID_DICT
from . import forms, forms_payments
@@ -112,11 +111,18 @@ def payments(request, addon_id, addon, webapp=False):
('desktop', True), ('firefoxos', False)]))
try:
tier_zero_id = Price.objects.get(price='0.00',
active=True).pk
tier_zero = Price.objects.get(price='0.00', active=True)
tier_zero_id = tier_zero.pk
except Price.DoesNotExist:
tier_zero = None
tier_zero_id = ''
# Get the regions based on tier zero. This should be all the
# regions with payments enabled.
paid_region_ids_by_slug = []
if tier_zero:
paid_region_ids_by_slug = tier_zero.region_ids_by_slug()
return jingo.render(
request, 'developers/payments/premium.html',
{'addon': addon, 'webapp': webapp, 'premium': addon.premium,
@@ -144,7 +150,7 @@ def payments(request, addon_id, addon, webapp=False):
PAYMENT_METHOD_CARD: _('Credit card'),
PAYMENT_METHOD_OPERATOR: _('Carrier'),
},
'all_paid_region_ids_by_slug': ALL_PAID_REGION_IDS_BY_SLUG,
'all_paid_region_ids_by_slug': paid_region_ids_by_slug,
})

0 comments on commit 57a637c

Please sign in to comment.