Skip to content

Commit

Permalink
Merge pull request dj-stripe#447 from jleclanche/issue/446
Browse files Browse the repository at this point in the history
Make Charge.customer nullable
  • Loading branch information
kavdev authored Apr 15, 2017
2 parents ef6071a + f7726e0 commit 26ba552
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
20 changes: 20 additions & 0 deletions djstripe/migrations/0027_auto_20170308_0620.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 1.10.6 on 2017-03-08 07:18
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('djstripe', '0026_idempotencykey'),
]

operations = [
migrations.AlterField(
model_name='charge',
name='customer',
field=models.ForeignKey(help_text='The customer associated with this charge.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='charges', to='djstripe.Customer'),
),
]
4 changes: 1 addition & 3 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Charge(StripeCharge):
)

customer = ForeignKey(
"Customer", on_delete=models.CASCADE,
"Customer", on_delete=models.CASCADE, null=True,
related_name="charges",
help_text="The customer associated with this charge."
)
Expand Down Expand Up @@ -123,8 +123,6 @@ def _attach_objects_hook(self, cls, data):
customer = cls._stripe_object_to_customer(target_cls=Customer, data=data)
if customer:
self.customer = customer
else:
raise ValidationError("A customer was not attached to this charge.")

transfer = cls._stripe_object_to_transfer(target_cls=Transfer, data=data)
if transfer:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from copy import deepcopy
from decimal import Decimal

from django.core.exceptions import ValidationError
from django.test.testcases import TestCase
from mock import patch

Expand Down Expand Up @@ -101,8 +100,10 @@ def test_sync_from_stripe_data_no_customer(self, default_account_mock):
fake_charge_copy = deepcopy(FAKE_CHARGE)
fake_charge_copy.pop("customer", None)

with self.assertRaisesMessage(ValidationError, "A customer was not attached to this charge."):
Charge.sync_from_stripe_data(fake_charge_copy)
Charge.sync_from_stripe_data(fake_charge_copy)
assert Charge.objects.count() == 1
charge = Charge.objects.get()
assert charge.customer is None

@patch("stripe.Charge.retrieve")
@patch("stripe.Transfer.retrieve")
Expand Down

0 comments on commit 26ba552

Please sign in to comment.