Skip to content

Commit

Permalink
Change Customer.__str__() to be a human-readable string
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed Aug 6, 2017
1 parent 2dd3656 commit 4f63cb2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
18 changes: 7 additions & 11 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def human_readable(self):


@class_doc_inherit
@python_2_unicode_compatible
class Customer(StripeCustomer):
doc = """
Expand Down Expand Up @@ -174,18 +175,13 @@ class Customer(StripeCustomer):
class Meta:
unique_together = ("subscriber", "livemode")

def str_parts(self):
parts = []

if self.subscriber:
parts.append(smart_text(self.subscriber))
parts.append("email={email}".format(email=self.subscriber.email))
def __str__(self):
if not self.subscriber:
return "{stripe_id} (deleted)".format(stripe_id=self.stripe_id)
elif self.subscriber.email:
return self.subscriber.email
else:
parts.append("(deleted)")

parts.extend(super(Customer, self).str_parts())

return parts
return self.stripe_id

@classmethod
def get_or_create(cls, subscriber, livemode=djstripe_settings.STRIPE_LIVE_MODE):
Expand Down
4 changes: 2 additions & 2 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def run_test_suite(args):
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "djstripe",
"USER": "",
"USER": "postgres",
"PASSWORD": "",
"HOST": "",
"HOST": "localhost",
"PORT": "",
},
},
Expand Down
7 changes: 3 additions & 4 deletions tests/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def setUp(self):
self.account = Account.objects.create()

def test_str(self):
self.assertEqual("<{subscriber}, email={email}, stripe_id={stripe_id}>".format(
subscriber=str(self.user), email=self.user.email, stripe_id=FAKE_CUSTOMER["id"]
), str(self.customer))
self.assertEqual(str(self.customer), self.user.email)
self.customer.subscriber = None
self.assertEqual(str(self.customer), "{stripe_id} (deleted)".format(stripe_id=self.customer.stripe_id))

def test_customer_dashboard_url(self):
expected_url = "https://dashboard.stripe.com/test/customers/{}".format(self.customer.stripe_id)
Expand Down Expand Up @@ -715,7 +715,6 @@ def test_delete_subscriber_purges_customer(self, customer_retrieve_mock):
self.user.delete()
customer = Customer.objects.get(stripe_id=FAKE_CUSTOMER["id"])
self.assertIsNotNone(customer.date_purged)
self.assertEqual(["(deleted)", "stripe_id=cus_6lsBvm5rJ0zyHc"], customer.str_parts())

@patch("stripe.Customer.retrieve")
def test_delete_subscriber_without_customer_is_noop(self, customer_retrieve_mock):
Expand Down

0 comments on commit 4f63cb2

Please sign in to comment.