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 | |
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?
-rw-r--r-- | alphabetlearning/payments/urls.py | 1 | ||||
-rw-r--r-- | alphabetlearning/payments/views.py | 22 | ||||
-rw-r--r-- | alphabetlearning/resources/views.py | 1 | ||||
-rw-r--r-- | alphabetlearning/templates/base.html | 3 | ||||
-rw-r--r-- | alphabetlearning/templates/payments/cart_lineitem.html | 2 | ||||
-rw-r--r-- | alphabetlearning/templates/payments/cartitem_confirm_delete.html | 7 |
6 files changed, 30 insertions, 6 deletions
diff --git a/alphabetlearning/payments/urls.py b/alphabetlearning/payments/urls.py index d5b0dd5..9bedaf7 100644 --- a/alphabetlearning/payments/urls.py +++ b/alphabetlearning/payments/urls.py @@ -18,5 +18,6 @@ urlpatterns = [ ), path("add-to-basket/<int:resource_id>", views.add_to_cart, name="add_to_basket"), path("landing/", views.ProductLandingPageView.as_view(), name="landing"), + path("delete-cart-item/<int:pk>", views. DeleteCartItem.as_view(), name="delete_cart_item"), path("webhooks/stripe/", views.stripe_webhook, name="stripe-webhook"), ] 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") + + + diff --git a/alphabetlearning/resources/views.py b/alphabetlearning/resources/views.py index 0aae929..c3d1bcd 100644 --- a/alphabetlearning/resources/views.py +++ b/alphabetlearning/resources/views.py @@ -129,7 +129,6 @@ def index(request): resource_list = [_extract_metadata_from_resource(r) for r in resource_objs] for r in resource_list: - # TODO test for this existing - it will fail if no cart try: cart_items = request.user.shoppingcart.items.all() if r.name in [r.resource.name for r in cart_items]: diff --git a/alphabetlearning/templates/base.html b/alphabetlearning/templates/base.html index 257a439..8bc9416 100644 --- a/alphabetlearning/templates/base.html +++ b/alphabetlearning/templates/base.html @@ -114,6 +114,7 @@ {% if request.user.shoppingcart %} <!- TODO - fix this, the logic should be different button if no cart -> + <! this is a problem because it seems you can have an empty cart -> <div class="mx-2 gray-icon"> <a href="{% url "payments:cart_detail" %}" class="full-basket-button"> <span class="fw-bold px-1"> @@ -130,7 +131,7 @@ {% else %} <div class="mx-2 gray-icon"> - <button class="empty-basket-button"><span class="fw-bold px-1">Empty Basket</span> + <button class="empty-basket-button" disabled><span class="fw-bold px-1">Empty Basket</span> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-bag-x" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M6.146 8.146a.5.5 0 0 1 .708 0L8 9.293l1.146-1.147a.5.5 0 1 1 .708.708L8.707 10l1.147 1.146a.5.5 0 0 1-.708.708L8 10.707l-1.146 1.147a.5.5 0 0 1-.708-.708L7.293 10 6.146 8.854a.5.5 0 0 1 0-.708"/> <path d="M8 1a2.5 2.5 0 0 1 2.5 2.5V4h-5v-.5A2.5 2.5 0 0 1 8 1m3.5 3v-.5a3.5 3.5 0 1 0-7 0V4H1v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V4zM2 5h12v9a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1z"/> diff --git a/alphabetlearning/templates/payments/cart_lineitem.html b/alphabetlearning/templates/payments/cart_lineitem.html index 79899df..9524d96 100644 --- a/alphabetlearning/templates/payments/cart_lineitem.html +++ b/alphabetlearning/templates/payments/cart_lineitem.html @@ -12,7 +12,7 @@ </div> <div class="p-2"> <p>{{ item.resource.card_description }}</p> - <p><a class="btn btn-danger btn-sm fs-6 text" href="#">Remove from basket</a></p> + <p><a class="btn btn-danger btn-sm fs-6 text" href="{% url "payments:delete_cart_item" item.pk %}">Remove from basket</a></p> </div> </div> </div> diff --git a/alphabetlearning/templates/payments/cartitem_confirm_delete.html b/alphabetlearning/templates/payments/cartitem_confirm_delete.html new file mode 100644 index 0000000..da3b81e --- /dev/null +++ b/alphabetlearning/templates/payments/cartitem_confirm_delete.html @@ -0,0 +1,7 @@ +<!- TODO improve the handling of this and make it htmx -> +<form method="post"> + {% csrf_token %} + <p>Are you sure you want to delete "{{ object }}"?</p> + {{ form }} + <input type="submit" value="Confirm"> +</form> |