From 8c550aa0dd232d855dbfce99ab3b259024cd91df Mon Sep 17 00:00:00 2001 From: KarolG Date: Wed, 17 May 2023 03:10:36 +0200 Subject: [PATCH] Added a shopping cart feature. Not the most elegant solution but all issues will be resolved after a meeting --- artel/artel/templates/index.html | 15 ++++++ artel/artel/urls.py | 9 +++- artel/cart/tests.py | 33 ++++++++++++ artel/cart/views.py | 35 +++++++++++++ artel/store/templates/store/cart_items.html | 50 +++++++++++++++++++ .../store/templates/store/shopping_cart.html | 0 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 artel/artel/templates/index.html create mode 100644 artel/cart/tests.py create mode 100644 artel/cart/views.py create mode 100644 artel/store/templates/store/cart_items.html delete mode 100644 artel/store/templates/store/shopping_cart.html diff --git a/artel/artel/templates/index.html b/artel/artel/templates/index.html new file mode 100644 index 0000000..a4e8b7d --- /dev/null +++ b/artel/artel/templates/index.html @@ -0,0 +1,15 @@ + + + + + My Store + + +

Welcome to My Store

+
+ + {% csrf_token %} + +
+ + diff --git a/artel/artel/urls.py b/artel/artel/urls.py index b3d57c5..73ec295 100644 --- a/artel/artel/urls.py +++ b/artel/artel/urls.py @@ -1,18 +1,25 @@ from django.conf import settings from django.urls import include, path from django.contrib import admin +from django.views.generic import TemplateView + from wagtail.admin import urls as wagtailadmin_urls from wagtail import urls as wagtail_urls from wagtail.documents import urls as wagtaildocs_urls from search import views as search_views +from cart import views as cart_views urlpatterns = [ path("django-admin/", admin.site.urls), path("admin/", include(wagtailadmin_urls)), path("documents/", include(wagtaildocs_urls)), - path("search/", search_views.search, name="search") + path("search/", search_views.search, name="search"), + path('cart/items/', cart_views.view_cart, name='cart_items'), + path('add_to_cart/', cart_views.add_to_cart, name='add_to_cart'), + path('remove_from_cart/', cart_views.remove_from_cart, name='remove_from_cart'), + path('', TemplateView.as_view(template_name='index.html'), name='index'), ] diff --git a/artel/cart/tests.py b/artel/cart/tests.py new file mode 100644 index 0000000..f433f8c --- /dev/null +++ b/artel/cart/tests.py @@ -0,0 +1,33 @@ +from django.test import TestCase +from django.urls import reverse +from store.models import ProductAuthor, ProductCategory, ProductTemplate, Product + + +class StoreTestCase(TestCase): + def setUp(self): + self.productid = 1 + self.author = ProductAuthor.objects.create(name='Test Author') + self.category = ProductCategory.objects.create(name='Test Category') + self.template = ProductTemplate.objects.create(category=self.category, + author=self.author, + title='Test title', + code='Test code', + description='Test description' + ) + self.product = Product.objects.create(template=self.template, + price=10.99) + self.cart_url = reverse('view_cart') + + def test_add_to_cart(self): + response = self.client.post(reverse('add_to_cart', + args=[self.productid])) + self.assertEqual(response.status_code, 302) + + def test_remove_from_cart(self): + response = self.client.post(reverse('remove_from_cart', + args=[self.productid])) + self.assertEqual(response.status_code, 302) + + def test_view_cart(self): + response = self.client.get(self.cart_url) + self.assertEqual(response.status_code, 200) diff --git a/artel/cart/views.py b/artel/cart/views.py new file mode 100644 index 0000000..9af4471 --- /dev/null +++ b/artel/cart/views.py @@ -0,0 +1,35 @@ +from django.shortcuts import render, redirect + + +def add_to_cart(request): + if request.method == 'POST': + product_id = request.POST["product_id"] + cart = request.session['cart'] + if product_id in cart: + cart[product_id] += 1 + else: + cart[product_id] = 1 + + # Save the session + request.session.modified = True + return redirect('../cart/items') + + +def remove_from_cart(request): + if request.method == 'POST': + product_id = request.POST["product_id"] + cart = request.session.get('cart', {}) + if product_id in cart: + del cart[product_id] + request.session['cart'] = cart + request.session.modified = True + + # Redirect to the cart page or product listing page + return redirect('../cart/items') + + +def view_cart(request): + cart = request.session.get('cart', {}) + products = "good" + return render(request, 'store/cart_items.html', + {'cart': cart, 'products': products}) diff --git a/artel/store/templates/store/cart_items.html b/artel/store/templates/store/cart_items.html new file mode 100644 index 0000000..3edcf09 --- /dev/null +++ b/artel/store/templates/store/cart_items.html @@ -0,0 +1,50 @@ + + + + + + Shopping Cart + + + + {% if cart %} + + + + + + + + + {% for product_id, quantity in cart.items %} + + + + + + + + {% endfor %} +
ProductQuantityPriceTotalRemove
{{ product_id }}{{ quantity }}{{ products_dict }}
+ + {% csrf_token %} +
+

Cart Total: {{ cart_total }}

+{% else %} +

Your cart is empty.

+{% endif %} + + diff --git a/artel/store/templates/store/shopping_cart.html b/artel/store/templates/store/shopping_cart.html deleted file mode 100644 index e69de29..0000000