Make RegionForm aware of price points (bug 884889)

This commit is contained in:
Wraithan (Chris McDonald) 2013-06-24 15:27:28 -07:00
Родитель 1fe4081b48
Коммит da5a96de4f
4 изменённых файлов: 51 добавлений и 26 удалений

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

@ -658,6 +658,7 @@ class RegionForm(forms.Form):
def __init__(self, *args, **kw):
self.product = kw.pop('product', None)
self.request = kw.pop('request', None)
self.region_ids = self.product.get_region_ids()
super(RegionForm, self).__init__(*args, **kw)
is_paid = self._product_is_paid()
@ -687,12 +688,12 @@ class RegionForm(forms.Form):
# If the app is paid, disable regions that use payments.
if is_paid:
self.disabled_regions.update(set(mkt.regions.ALL_REGION_IDS)
.difference(self.product.get_possible_price_region_ids()))
self.disabled_regions.add(mkt.regions.WORLDWIDE.id)
self.fields['other_regions'].widget.attrs['disabled'] = 'disabled'
self.fields['other_regions'].label = _(u'Other regions')
for region in mkt.regions.ALL_REGIONS:
if not region.has_payments:
self.disabled_regions.add(region.id)
self.disabled_regions = list(self.disabled_regions)
@ -708,10 +709,8 @@ class RegionForm(forms.Form):
def has_inappropriate_regions(self):
"""Returns whether the app is listed in regions that it shouldn't
otherwise be registered in."""
return (self._product_is_paid() and
set(self.product.get_region_ids()) -
set(mkt.regions.ALL_PAID_REGION_IDS))
set(self.region_ids).intersection(self.disabled_regions))
def clean(self):
data = self.cleaned_data
@ -720,10 +719,16 @@ class RegionForm(forms.Form):
raise forms.ValidationError(
_('You must select at least one region or '
'"Other and new regions."'))
if data.get('regions'):
self.region_ids = [int(r) for r in data['regions']]
if self.has_inappropriate_regions():
raise forms.ValidationError(
_('You have selected a region that is not valid for your '
'price point.'))
return data
def save(self):
# Don't save regions if we are toggling.
if self.is_toggling():
return
@ -733,7 +738,7 @@ class RegionForm(forms.Form):
# If the app is paid, disable regions that do not use payments.
if self._product_is_paid():
after &= set(mkt.regions.ALL_PAID_REGION_IDS)
after &= set(self.product.get_possible_price_region_ids())
# Add new region exclusions.
to_add = before - after

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

@ -110,7 +110,7 @@
<div class="island warning">
{%- trans %}
Your app is currently incomplete. Select a payment account and
price tier to restore it.
price point to restore it.
{% endtrans -%}
</div>
{% endif %}

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

@ -16,6 +16,7 @@ from amo.tests import app_factory
from amo.tests.test_helpers import get_image_path
from addons.models import Addon, AddonCategory, Category, CategorySupervisor
from files.helpers import copyfileobj
from market.models import AddonPremium, Price
from users.models import UserProfile
import mkt
@ -135,22 +136,12 @@ class TestCategoryForm(amo.tests.WebappTestCase):
class TestRegionForm(amo.tests.WebappTestCase):
fixtures = ['webapps/337141-steamcube']
fixtures = fixture('webapp_337141', 'prices')
def setUp(self):
super(TestRegionForm, self).setUp()
self.request = RequestFactory()
self.kwargs = {'product': self.app, 'request': self.request}
def test_is_toggling_free(self):
form = forms.RegionForm(data=None, **self.kwargs)
self.request.POST = {'toggle-paid': 'free'}
eq_(form.is_toggling(), 'free')
def test_is_toggling_paid(self):
form = forms.RegionForm(data=None, **self.kwargs)
self.request.POST = {'toggle-paid': 'paid'}
eq_(form.is_toggling(), 'paid')
self.kwargs = {'product': self.app}
def test_initial_empty(self):
form = forms.RegionForm(data=None, **self.kwargs)
@ -171,8 +162,7 @@ class TestRegionForm(amo.tests.WebappTestCase):
def test_initial_excluded_in_regions_and_future_regions(self):
for region in [mkt.regions.BR, mkt.regions.UK, mkt.regions.WORLDWIDE]:
AER.objects.create(addon=self.app,
region=region.id)
AER.objects.create(addon=self.app, region=region.id)
regions = list(mkt.regions.REGION_IDS)
regions.remove(mkt.regions.BR.id)
@ -185,25 +175,50 @@ class TestRegionForm(amo.tests.WebappTestCase):
eq_(form.initial['other_regions'], False)
def test_disable_regions_on_paid(self):
mkt.regions.BR.has_payments = True
eq_(self.app.get_region_ids(), mkt.regions.REGION_IDS)
self.app.update(premium_type=amo.ADDON_PREMIUM)
AddonPremium.objects.create(addon=self.app,
price=Price.objects.get(id=1))
form = forms.RegionForm(data=None, **self.kwargs)
assert form.has_inappropriate_regions()
assert not form.is_valid()
assert form.has_inappropriate_regions()
form = forms.RegionForm(
data={'regions': mkt.regions.ALL_PAID_REGION_IDS}, **self.kwargs)
assert not form.is_valid()
assert form.has_inappropriate_regions()
form = forms.RegionForm(data={'regions': [mkt.regions.PL.id]},
**self.kwargs)
assert form.is_valid(), form.errors
assert not form.has_inappropriate_regions()
form.save()
self.assertSetEqual(self.app.get_region_ids(),
mkt.regions.ALL_PAID_REGION_IDS)
[mkt.regions.PL.id])
form = forms.RegionForm(data=None, **self.kwargs)
def test_paid_enable_region(self):
for region in mkt.regions.ALL_REGION_IDS:
AER.objects.create(addon=self.app, region=region)
self.app.update(premium_type=amo.ADDON_PREMIUM)
AddonPremium.objects.create(addon=self.app,
price=Price.objects.get(id=1))
form = forms.RegionForm(data={'regions': []}, **self.kwargs)
assert not form.is_valid() # Fails due to needing at least 1 region
assert not form.has_inappropriate_regions(), form.has_inappropriate_regions()
form = forms.RegionForm(data={'regions': [mkt.regions.PL.id]},
**self.kwargs)
assert form.is_valid(), form.errors
assert not form.has_inappropriate_regions()
form = forms.RegionForm(data={'regions': [mkt.regions.BR.id]},
**self.kwargs)
assert not form.is_valid()
assert form.has_inappropriate_regions()
def test_worldwide_only(self):
form = forms.RegionForm(data={'other_regions': 'on'}, **self.kwargs)
assert form.is_valid(), form.errors

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

@ -573,6 +573,11 @@ class Webapp(Addon):
.values_list('region', flat=True))
return sorted(list(set(all_ids) - set(excluded)))
def get_possible_price_region_ids(self):
if self.premium:
ids = [p['region'] for p in self.premium.price.prices()]
return sorted(set(ids))
def get_regions(self):
"""
Return regions, e.g.: