feat: added tests for cart views
This commit is contained in:
@@ -2,6 +2,8 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import environ
|
import environ
|
||||||
|
from shutil import which
|
||||||
|
|
||||||
|
|
||||||
BASE_DIR = Path(".")
|
BASE_DIR = Path(".")
|
||||||
|
|
||||||
|
|||||||
+13
-10
@@ -1,7 +1,10 @@
|
|||||||
|
from django.shortcuts import render
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
from shop.filters import ProductFilter
|
from shop.filters import ProductFilter
|
||||||
from shop.models import Product
|
from shop.models import Product, Cart
|
||||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin, CartMixin
|
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):
|
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||||
@@ -20,16 +23,16 @@ class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class CartDropdown(TemplateView, CartMixin):
|
def cart_dropdown(request, *args, **kwargs):
|
||||||
template_name = "components/cart/cart_navbar.html"
|
cart, created = get_or_create_cart(request)
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
response = render(request, "components/cart/cart_navbar.html", context={
|
||||||
cart = self.get_cart()
|
|
||||||
|
|
||||||
return {
|
|
||||||
"cart": cart,
|
"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()
|
list_products = ListProducts.as_view()
|
||||||
cart_dropdown = CartDropdown.as_view()
|
|
||||||
|
|||||||
@@ -28,13 +28,3 @@ class PaginatedQuerysetMixin:
|
|||||||
page = self.request.GET.get("page", 1)
|
page = self.request.GET.get("page", 1)
|
||||||
qs = paginator.get_page(page)
|
qs = paginator.get_page(page)
|
||||||
return qs
|
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.test import TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,19 @@ def get_cart(request):
|
|||||||
else:
|
else:
|
||||||
cart = Cart.objects.filter(user=request.user).first()
|
cart = Cart.objects.filter(user=request.user).first()
|
||||||
return cart
|
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 django.views.decorators.http import require_http_methods
|
||||||
|
|
||||||
from shop.models import Product, Cart, CartItem
|
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
|
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||||
|
|
||||||
|
|
||||||
@@ -38,21 +38,20 @@ class ProductDetail(TemplateView):
|
|||||||
)
|
)
|
||||||
def add_cart_item(request, *args, **kwargs):
|
def add_cart_item(request, *args, **kwargs):
|
||||||
product = get_object_or_404(Product, pk=request.POST.get("product"))
|
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'})
|
response = HttpResponse(status=201, headers={'HX-Trigger': 'updated-cart'})
|
||||||
|
|
||||||
if not request.user.is_authenticated:
|
if created and request.user.is_anonymous:
|
||||||
cart = Cart.objects.create(
|
|
||||||
user=None,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cart, created = Cart.objects.get_or_create(user=request.user)
|
|
||||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
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()
|
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:
|
if existing_cart_item is not None:
|
||||||
existing_cart_item.quantity += int(request.POST.get("quantity"))
|
existing_cart_item.quantity += int(request.POST.get("quantity"))
|
||||||
existing_cart_item.save()
|
existing_cart_item.save()
|
||||||
|
# Si no, lo creamos
|
||||||
else:
|
else:
|
||||||
CartItem.objects.create(
|
CartItem.objects.create(
|
||||||
cart=cart,
|
cart=cart,
|
||||||
@@ -69,7 +68,7 @@ def add_cart_item(request, *args, **kwargs):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
def delete_cart_item(request, pk, *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 = get_object_or_404(CartItem, pk=pk, cart=cart)
|
||||||
cart_item.delete()
|
cart_item.delete()
|
||||||
return HttpResponse(status=204, headers={'HX-Trigger': 'updated-cart'})
|
return HttpResponse(status=204, headers={'HX-Trigger': 'updated-cart'})
|
||||||
|
|||||||
Reference in New Issue
Block a user