From fce28f5be8ba8831eed5ccf482fa2abf5432ee89 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 20 Oct 2024 16:42:21 +0100 Subject: Cart shows items in it; disables buttons if item in basket - Rough cart icon in navbar - Shows items in cart - Styled dependent on existence - Add to cart buttons disabled if resource in cart, on resource list page and detail page - Throws 404 error if trying add item to cart which has no price - eventually all items will have a price --- alphabetlearning/resources/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'alphabetlearning/resources') diff --git a/alphabetlearning/resources/views.py b/alphabetlearning/resources/views.py index 3bf3d22..c09ff78 100644 --- a/alphabetlearning/resources/views.py +++ b/alphabetlearning/resources/views.py @@ -128,6 +128,15 @@ 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]: + r.in_cart = True + except: + pass + # Create a separate queryset for Featured resources featured_resources = [r for r in resource_list if r.feature_slot] featured_resources = sorted(featured_resources, key=lambda resource: resource.feature_slot) @@ -313,6 +322,10 @@ def resource_detail(request, resource_id): "created": resource_metadata.created, "updated": resource_metadata.updated, } + # TODO test for this existing - it will fail if no cart + cart_items = request.user.shoppingcart.items.all() + if resource["name"] in [r.resource.name for r in cart_items]: + resource.update(in_cart=True) return render(request, "resources/resource_detail.html", {"resource": resource}) -- cgit v1.2.3