From 4c8a170857b292c5b2888a6b3e0a696d8a006a6d Mon Sep 17 00:00:00 2001 From: Andy McKay Date: Fri, 1 Feb 2013 14:12:53 -0800 Subject: [PATCH] cope with install_type on the postback (bug 837257) --- apps/market/models.py | 12 ++++++++++-- apps/market/tests/test_models.py | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/market/models.py b/apps/market/models.py index 85640790f7..d2a15c3395 100644 --- a/apps/market/models.py +++ b/apps/market/models.py @@ -11,6 +11,7 @@ import amo.models from amo.decorators import write from amo.utils import get_locale_from_lang, memoize_key from constants.payments import PROVIDER_CURRENCIES +from mkt.constants import apps from stats.models import Contribution from users.models import UserProfile @@ -173,9 +174,16 @@ def create_addon_purchase(sender, instance, **kw): data = {'addon': instance.addon, 'user': instance.user} purchase, created = AddonPurchase.objects.safer_get_or_create(**data) purchase.update(type=amo.CONTRIB_PURCHASE) - from mkt.webapps.models import Installed # Circular import. + from mkt.webapps.models import Installed # Circular import + # Ensure that devs have the correct installed object found + # or created. + # + is_dev = instance.addon.has_author(instance.user, + (amo.AUTHOR_ROLE_OWNER, amo.AUTHOR_ROLE_DEV)) + install_type = (apps.INSTALL_TYPE_DEVELOPER if is_dev + else apps.INSTALL_TYPE_USER) Installed.objects.safer_get_or_create(user=instance.user, - addon=instance.addon) + addon=instance.addon, install_type=install_type) elif instance.type in [amo.CONTRIB_REFUND, amo.CONTRIB_CHARGEBACK]: purchases = AddonPurchase.objects.filter(addon=instance.addon, diff --git a/apps/market/tests/test_models.py b/apps/market/tests/test_models.py index 5c6ae25166..f509c92e86 100644 --- a/apps/market/tests/test_models.py +++ b/apps/market/tests/test_models.py @@ -8,9 +8,10 @@ from nose.tools import eq_ import amo import amo.tests -from addons.models import Addon +from addons.models import Addon, AddonUser from market.models import (AddonPremium, PreApprovalUser, Price, PriceCurrency, Refund) +from mkt.constants import apps from stats.models import Contribution from users.models import UserProfile @@ -226,7 +227,20 @@ class TestContribution(ContributionMixin, amo.tests.TestCase): self.create(amo.CONTRIB_REFUND) eq_(self.addon.addonpurchase_set.filter(user=other).count(), 1) - def test_user_installed(self): + def set_role(self, role): + AddonUser.objects.create(addon=self.addon, user=self.user, role=role) + self.create(amo.CONTRIB_PURCHASE) + installed = self.user.installed_set.filter(addon=self.addon) + eq_(installed.count(), 1) + eq_(installed[0].install_type, apps.INSTALL_TYPE_DEVELOPER) + + def test_user_dev(self): + self.set_role(amo.AUTHOR_ROLE_DEV) + + def test_user_owner(self): + self.set_role(amo.AUTHOR_ROLE_OWNER) + + def test_user_installed_dev(self): self.create(amo.CONTRIB_PURCHASE) eq_(self.user.installed_set.filter(addon=self.addon).count(), 1)