Add functionality to delete user card

This commit is contained in:
PCoder 2017-10-21 00:02:35 +02:00
parent 255c8a1d80
commit c664b44f2c

View file

@ -537,6 +537,7 @@ class SettingsView(LoginRequiredMixin, FormView):
template_name = "hosting/settings.html" template_name = "hosting/settings.html"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
form_class = BillingAddressForm form_class = BillingAddressForm
permission_required = ['view_usercarddetail']
def get_form(self, form_class): def get_form(self, form_class):
""" """
@ -566,6 +567,16 @@ class SettingsView(LoginRequiredMixin, FormView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if 'delete_card' in request.POST: if 'delete_card' in request.POST:
try:
card = UserCardDetail.objects.get(pk=self.kwargs.get('pk'))
if request.user.has_perm(self.permission_required[0], card):
card.delete()
else:
msg = _("You are not permitted to do this operation")
messages.add_message(request, messages.ERROR, msg)
except UserCardDetail.DoesNotExist:
msg = _("The selected card does not exist")
messages.add_message(request, messages.ERROR, msg)
return HttpResponseRedirect(reverse_lazy('hosting:settings')) return HttpResponseRedirect(reverse_lazy('hosting:settings'))
form = self.get_form() form = self.get_form()
if form.is_valid(): if form.is_valid():
@ -603,7 +614,7 @@ class SettingsView(LoginRequiredMixin, FormView):
_('You seem to have already added this card') _('You seem to have already added this card')
) )
except UserCardDetail.DoesNotExist: except UserCardDetail.DoesNotExist:
UserCardDetail.objects.create( UserCardDetail.create(
stripe_customer=stripe_customer, stripe_customer=stripe_customer,
last4=card_details_response['last4'], last4=card_details_response['last4'],
brand=card_details_response['brand'], brand=card_details_response['brand'],