Added a shopping cart feature.

Not the most elegant solution but all issues will be resolved after a meeting
feature/product_models_refactor
KarolG 2023-05-17 03:10:36 +02:00
rodzic 31e1e0a2ee
commit 8c550aa0dd
6 zmienionych plików z 141 dodań i 1 usunięć

Wyświetl plik

@ -0,0 +1,15 @@
<!-- basic interface to add produts, created for testing -->
<!DOCTYPE html>
<html>
<head>
<title>My Store</title>
</head>
<body>
<h1>Welcome to My Store</h1>
<form action="{% url 'add_to_cart' %}" method="POST">
<input type="text" id="product_id" name="product_id">
{% csrf_token %}
<button type="submit">Add Item to Cart</button>
</form>
</body>
</html>

Wyświetl plik

@ -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'),
]

Wyświetl plik

@ -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)

Wyświetl plik

@ -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})

Wyświetl plik

@ -0,0 +1,50 @@
<!-- cart.html -->
<!-- content will be changed after consultation -->
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
{% if cart %}
<table>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Remove</th>
</tr>
{% for product_id, quantity in cart.items %}
<tr>
<td>{{ product_id }}</td>
<td>{{ quantity }}</td>
<td>{{ products_dict }}</td>
<td></td>
<td><form action="{% url 'remove_from_cart' %}" method="POST">
<input type="hidden" value= {{ product_id }} name= "product_id" >
{% csrf_token %}
<button type="submit">Remove</button></a></form></td>
</tr>
{% endfor %}
</table>
<p class="cart-total">Cart Total: {{ cart_total }}</p>
{% else %}
<p class="empty-cart-msg">Your cart is empty.</p>
{% endif %}
</body>
</html>