diff options
author | Matthew Lemon <y@yulqen.org> | 2024-10-20 20:28:09 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-10-20 20:28:09 +0100 |
commit | e063a30a06caef9e4cda7afb5ef175b1c04b5e96 (patch) | |
tree | 2f37118c780144df0b089f82d33f003a7df191be /alphabetlearning/payments/views.py | |
parent | a5942c7f240686146c243e22d849db97d0a904b9 (diff) |
Can now delete item from basket
- if is only one item left, it is deleted along with the basket
- if there are more than one, just that one is deleted
- TODO needs proper handling of the confirmation page - it's not
formatted. htmx?
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r-- | alphabetlearning/payments/views.py | 22 |
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") + + + |