diff --git a/pinax/stripe/models.py b/pinax/stripe/models.py index f9e8f8521..7e9f215d4 100644 --- a/pinax/stripe/models.py +++ b/pinax/stripe/models.py @@ -86,7 +86,6 @@ def __repr__(self): class Coupon(models.Model): stripe_id = models.CharField(max_length=191) created_at = models.DateTimeField(default=timezone.now) - stripe_account = models.ForeignKey( "pinax_stripe.Account", on_delete=models.CASCADE, @@ -138,6 +137,13 @@ def __repr__(self): str(self.stripe_id), )) + @property + def stripe_coupon(self): + return stripe.Coupon.retrieve( + self.stripe_id, + stripe_account=self.stripe_account.stripe_id, + ) + @python_2_unicode_compatible class EventProcessingException(models.Model): diff --git a/pinax/stripe/tests/test_models.py b/pinax/stripe/tests/test_models.py index f60d3052b..8af56c9d1 100644 --- a/pinax/stripe/tests/test_models.py +++ b/pinax/stripe/tests/test_models.py @@ -8,7 +8,7 @@ from django.test import TestCase from django.utils import timezone -from mock import patch +from mock import call, patch from ..models import ( Account, @@ -89,6 +89,13 @@ def test_coupon_absolute(self): c = Coupon(amount_off=decimal.Decimal(50.00), duration="once", currency="usd") self.assertEquals(str(c), "Coupon for $50, once") + @patch("stripe.Coupon.retrieve") + def test_coupon_stripe_coupon(self, RetrieveMock): + c = Coupon(stripe_id="coupon", stripe_account=Account(stripe_id="acct_A")) + self.assertEqual(c.stripe_coupon, RetrieveMock.return_value) + self.assertTrue(RetrieveMock.call_args_list, [ + call("coupon", stripe_account="acct_A")]) + def test_model_table_name(self): self.assertEquals(Customer()._meta.db_table, "pinax_stripe_customer")