Refactor and add messages

This commit is contained in:
PCoder 2017-10-21 20:37:50 +02:00
parent be8181ec42
commit 75e90dbacd

View file

@ -572,11 +572,13 @@ class SettingsView(LoginRequiredMixin, FormView):
if request.user.has_perm(self.permission_required[0], card): if request.user.has_perm(self.permission_required[0], card):
if card.card_id is not None: if card.card_id is not None:
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
stripe_utils.delete_customer_card( stripe_utils.dissociate_customer_card(
request.user.stripecustomer.stripe_id, request.user.stripecustomer.stripe_id,
card.card_id card.card_id
) )
card.delete() card.delete()
msg = _("Card deassociation successful")
messages.add_message(request, messages.SUCCESS, msg)
else: else:
msg = _("You are not permitted to do this operation") msg = _("You are not permitted to do this operation")
messages.add_message(request, messages.ERROR, msg) messages.add_message(request, messages.ERROR, msg)
@ -615,17 +617,9 @@ class SettingsView(LoginRequiredMixin, FormView):
exp_month=card_details_response['exp_month'], exp_month=card_details_response['exp_month'],
exp_year=card_details_response['exp_year'] exp_year=card_details_response['exp_year']
) )
form.add_error( msg = _('You seem to have already added this card')
"__all__", messages.add_message(request, messages.ERROR, msg)
_('You seem to have already added this card')
)
except UserCardDetail.DoesNotExist: except UserCardDetail.DoesNotExist:
add_result = stripe_utils.add_card_to_stripe_customer(
stripe_customer.stripe_id, token
)
if add_result.get('error') is not None:
form.add_error("__all__", card_details.get('error'))
return self.render_to_response(self.get_context_data())
UserCardDetail.create( UserCardDetail.create(
stripe_customer=stripe_customer, stripe_customer=stripe_customer,
last4=card_details_response['last4'], last4=card_details_response['last4'],