Refactor getting all cards to UserCardDetail.get_all_cards_list()

This commit is contained in:
PCoder 2017-10-21 21:27:21 +02:00
parent 8df72620d6
commit 14c7d6ac0e
2 changed files with 14 additions and 7 deletions

View file

@ -209,3 +209,15 @@ class UserCardDetail(AssignPermissionsMixin, models.Model):
)
instance.assign_permissions(stripe_customer.user)
return instance
@classmethod
def get_all_cards_list(cls, stripe_customer):
user_card_details = UserCardDetail.objects.filter(
stripe_customer_id=stripe_customer.id
)
cards_list = []
for card in user_card_details:
cards_list.append({
'last4': card.last4, 'brand': card.brand, 'id': card.id
})
return cards_list

View file

@ -551,14 +551,9 @@ class SettingsView(LoginRequiredMixin, FormView):
def get_context_data(self, **kwargs):
context = super(SettingsView, self).get_context_data(**kwargs)
user = self.request.user
user_card_details = UserCardDetail.objects.filter(
stripe_customer_id=user.stripecustomer.id
cards_list = UserCardDetail.get_all_cards_list(
stripe_customer= user.stripecustomer
)
cards_list = []
for card in user_card_details:
cards_list.append({
'last4': card.last4, 'brand': card.brand, 'id': card.id
})
context.update({
'cards_list': cards_list,
'stripe_key': settings.STRIPE_API_PUBLIC_KEY