aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r--alphabetlearning/payments/views.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py
index cf77e5b..92751c9 100644
--- a/alphabetlearning/payments/views.py
+++ b/alphabetlearning/payments/views.py
@@ -1,13 +1,14 @@
import stripe
from django.http import HttpResponse, HttpResponseBadRequest
from django.core.mail import send_mail
+from django.urls import reverse_lazy
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.shortcuts import render
from django.views import View
-from django.views.generic import TemplateView
+from django.views.generic import TemplateView, DeleteView
from django.views.decorators.csrf import csrf_exempt
from alphabetlearning.resources.models import Resource
@@ -155,7 +156,6 @@ def stripe_webhook(request):
# TODO clear their shopping cart - we need to obtain their user ID
user = User.objects.get(email=customer_email)
ShoppingCart.objects.get(user=user).delete()
- print(f"Deleted cart for {user}")
# TODO add the transaction to our local history? Or just use Stripe?
@@ -167,5 +167,21 @@ def stripe_webhook(request):
[customer_email],
fail_silently=False,
)
-
return HttpResponse(status=200)
+
+class DeleteCartItem(DeleteView):
+ model = CartItem
+ success_url = reverse_lazy("payments:cart_detail")
+
+ # delete the cart item if there is more than one
+ # delete the item and the cart itself if it is the last thing in the cart
+ def post(self, request, *args, **kwargs):
+ cart_items = request.user.shoppingcart.items.all()
+ if len(cart_items) > 1:
+ return self.delete(request, *args, **kwargs)
+ else:
+ request.user.shoppingcart.delete()
+ return redirect("resources:resource_list")
+
+
+