feat: added tests for cart views
This commit is contained in:
+13
-10
@@ -1,7 +1,10 @@
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import TemplateView
|
||||
from shop.filters import ProductFilter
|
||||
from shop.models import Product
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin, CartMixin
|
||||
from shop.models import Product, Cart
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
|
||||
|
||||
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
@@ -20,16 +23,16 @@ class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
}
|
||||
|
||||
|
||||
class CartDropdown(TemplateView, CartMixin):
|
||||
template_name = "components/cart/cart_navbar.html"
|
||||
def cart_dropdown(request, *args, **kwargs):
|
||||
cart, created = get_or_create_cart(request)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
cart = self.get_cart()
|
||||
response = render(request, "components/cart/cart_navbar.html", context={
|
||||
"cart": cart,
|
||||
})
|
||||
|
||||
return {
|
||||
"cart": cart,
|
||||
}
|
||||
if not request.user.is_authenticated:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
||||
return response
|
||||
|
||||
|
||||
list_products = ListProducts.as_view()
|
||||
cart_dropdown = CartDropdown.as_view()
|
||||
|
||||
@@ -28,13 +28,3 @@ class PaginatedQuerysetMixin:
|
||||
page = self.request.GET.get("page", 1)
|
||||
qs = paginator.get_page(page)
|
||||
return qs
|
||||
|
||||
|
||||
class CartMixin:
|
||||
def get_cart(self):
|
||||
if not self.request.user.is_authenticated:
|
||||
uid = self.request.COOKIES.get(ANONYMOUS_CART_ID_COOKIE_NAME)
|
||||
cart = Cart.objects.get(uuid=uid)
|
||||
else:
|
||||
cart = Cart.objects.filter(user=self.request.user).first()
|
||||
return cart
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
@@ -9,3 +9,19 @@ def get_cart(request):
|
||||
else:
|
||||
cart = Cart.objects.filter(user=request.user).first()
|
||||
return cart
|
||||
|
||||
|
||||
def get_or_create_cart(request):
|
||||
created = False
|
||||
if not request.user.is_authenticated:
|
||||
uid = request.COOKIES.get(ANONYMOUS_CART_ID_COOKIE_NAME)
|
||||
|
||||
if uid:
|
||||
cart = Cart.objects.get(uuid=uid)
|
||||
else:
|
||||
cart = Cart.objects.create(user=None)
|
||||
created = True
|
||||
else:
|
||||
cart, created = Cart.objects.get_or_create(user=request.user)
|
||||
|
||||
return cart, created
|
||||
|
||||
+7
-8
@@ -4,7 +4,7 @@ from django.http.response import HttpResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
|
||||
from shop.models import Product, Cart, CartItem
|
||||
from web.utils import get_cart
|
||||
from web.utils import get_cart, get_or_create_cart
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
|
||||
|
||||
@@ -38,21 +38,20 @@ class ProductDetail(TemplateView):
|
||||
)
|
||||
def add_cart_item(request, *args, **kwargs):
|
||||
product = get_object_or_404(Product, pk=request.POST.get("product"))
|
||||
cart, created = get_or_create_cart(request)
|
||||
response = HttpResponse(status=201, headers={'HX-Trigger': 'updated-cart'})
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
cart = Cart.objects.create(
|
||||
user=None,
|
||||
)
|
||||
else:
|
||||
cart, created = Cart.objects.get_or_create(user=request.user)
|
||||
if created and request.user.is_anonymous:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
||||
|
||||
# Comprobamos si existe una línea de carrito para ese carrito de ese producto
|
||||
existing_cart_item = CartItem.objects.filter(cart=cart, product=product).first()
|
||||
|
||||
# Si existe, simplemente le sumamos la cantidad a la línea ya existente
|
||||
if existing_cart_item is not None:
|
||||
existing_cart_item.quantity += int(request.POST.get("quantity"))
|
||||
existing_cart_item.save()
|
||||
# Si no, lo creamos
|
||||
else:
|
||||
CartItem.objects.create(
|
||||
cart=cart,
|
||||
@@ -69,7 +68,7 @@ def add_cart_item(request, *args, **kwargs):
|
||||
]
|
||||
)
|
||||
def delete_cart_item(request, pk, *args, **kwargs):
|
||||
cart = get_cart(request)
|
||||
cart = get_or_create_cart(request)
|
||||
cart_item = get_object_or_404(CartItem, pk=pk, cart=cart)
|
||||
cart_item.delete()
|
||||
return HttpResponse(status=204, headers={'HX-Trigger': 'updated-cart'})
|
||||
|
||||
Reference in New Issue
Block a user