Skip to content

Commit

Permalink
Add UniquePerAccountSripeObject for Plans
Browse files Browse the repository at this point in the history
The `stripe_id` for plans is not unique by itself globally (it is not generated
by Stripe), and therefore needs to be unique per account.
  • Loading branch information
blueyed committed Nov 21, 2017
1 parent db02bc3 commit 2e3f173
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pinax/stripe/migrations/0011_auto_20171121_1648.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-21 16:48
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pinax_stripe', '0010_connect'),
]

operations = [
migrations.AlterField(
model_name='plan',
name='stripe_id',
field=models.CharField(max_length=191),
),
migrations.AlterUniqueTogether(
name='plan',
unique_together=set([('stripe_id', 'stripe_account')]),
),
]
20 changes: 18 additions & 2 deletions pinax/stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Meta:
abstract = True


class AccountRelatedStripeObject(StripeObject):
class AccountRelatedStripeObjectMixin(models.Model):

stripe_account = models.ForeignKey(
"pinax_stripe.Account",
Expand All @@ -44,6 +44,22 @@ class Meta:
abstract = True


class AccountRelatedStripeObject(AccountRelatedStripeObjectMixin, StripeObject):
"""Uses a mixin to support Django 1.8 (name clash for stripe_id)"""

class Meta:
abstract = True


class UniquePerAccountStripeObject(AccountRelatedStripeObjectMixin):
stripe_id = models.CharField(max_length=191)
created_at = models.DateTimeField(default=timezone.now)

class Meta:
abstract = True
unique_together = ("stripe_id", "stripe_account")


class StripeAccountFromCustomerMixin(object):
@property
def stripe_account(self):
Expand All @@ -56,7 +72,7 @@ def stripe_account_stripe_id(self):


@python_2_unicode_compatible
class Plan(AccountRelatedStripeObject):
class Plan(UniquePerAccountStripeObject):
amount = models.DecimalField(decimal_places=2, max_digits=9)
currency = models.CharField(max_length=15, blank=False)
interval = models.CharField(max_length=15)
Expand Down
6 changes: 6 additions & 0 deletions pinax/stripe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def test_plan_str_jpy(self):
p = Plan(amount=decimal.Decimal("5"), name="My Plan", currency="jpy", interval="monthly", interval_count=1)
self.assertTrue(u"\u00a5" in _str(p))

def test_plan_per_account(self):
Plan.objects.create(stripe_id="plan", amount=decimal.Decimal("100"), interval="monthly", interval_count=1)
account = Account.objects.create(stripe_id="acct_A")
Plan.objects.create(stripe_id="plan", stripe_account=account, amount=decimal.Decimal("100"), interval="monthly", interval_count=1)
self.assertEquals(Plan.objects.count(), 2)

def test_event_processing_exception_str(self):
e = EventProcessingException(data="hello", message="hi there", traceback="fake")
self.assertTrue("Event=" in str(e))
Expand Down

0 comments on commit 2e3f173

Please sign in to comment.