Use db for list of paid regions by slug (bug 921593)

This commit is contained in:
Stuart Colville 2013-09-27 19:54:55 +00:00
Родитель eeeb1ec998
Коммит aea88bcc7e
4 изменённых файлов: 30 добавлений и 13 удалений

Просмотреть файл

@ -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 @@ from constants.payments import (CARRIER_CHOICES, PAYMENT_METHOD_ALL,
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 @@ class Price(amo.models.ModelBase):
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 addons.models import Addon, AddonUser
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 @@ class TestPrice(amo.tests.TestCase):
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 @@ REGIONS_CHOICES_ID = ([(WORLDWIDE.id, WORLDWIDE)] +
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.constants import DEVICE_LOOKUP
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,
})