From ef9dc446db7fdabb4b99f624f2a16df9002308cf Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Thu, 26 Oct 2017 15:00:54 +0200 Subject: [PATCH] Use hasattr to check if a user already has a stripecustomer object --- hosting/views.py | 6 +++++- membership/models.py | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index f082ae1b..d4af6d25 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -664,8 +664,12 @@ class PaymentVMView(LoginRequiredMixin, FormView): def get_context_data(self, **kwargs): context = super(PaymentVMView, self).get_context_data(**kwargs) user = self.request.user + if hasattr(user, 'stripecustomer'): + stripe_customer = user.stripecustomer + else: + stripe_customer = None cards_list = UserCardDetail.get_all_cards_list( - stripe_customer= user.stripecustomer + stripe_customer=stripe_customer ) context.update({ 'cards_list': cards_list, diff --git a/membership/models.py b/membership/models.py index dc76a8dc..7ebf00ca 100644 --- a/membership/models.py +++ b/membership/models.py @@ -221,19 +221,19 @@ class StripeCustomer(models.Model): stripe_data = stripe_utils.create_customer(token, email, user.name) if stripe_data.get('response_object'): stripe_cus_id = stripe_data.get('response_object').get('id') - if user.stripecustomer is None: - # The user never had an associated Stripe account - # So, create one - stripe_customer = StripeCustomer.objects.create( - user=user, stripe_id=stripe_cus_id - ) - else: + if hasattr(user, 'stripecustomer'): # User already had a Stripe account and we are here # because the account was deleted in dashboard # So, we simply update the stripe_id user.stripecustomer.stripe_id = stripe_cus_id user.stripecustomer.save() stripe_customer = user.stripecustomer + else: + # The user never had an associated Stripe account + # So, create one + stripe_customer = StripeCustomer.objects.create( + user=user, stripe_id=stripe_cus_id + ) return stripe_customer else: return None