diff --git a/web/templates/components/cart/cart.html b/web/templates/components/cart/cart.html new file mode 100644 index 0000000..64fe5a6 --- /dev/null +++ b/web/templates/components/cart/cart.html @@ -0,0 +1,142 @@ +{% load i18n %} +
+
+

{% translate 'Carrito' %}

+ +
+
+
+ + {% for item in items %} +
+
+ + {{ item.product.name }} + + + + +
+
+ + {{ item.quantity }}x + +
+
+

{{ item.product.price.price_with_tax }} + €

+
+
+ +
+ {{ item.product.name }} + +
+ + +
+ {% csrf_token %} + +
+ +
+
+
+
+ {% endfor %} + + {% if items.count == 0 %} + + {% translate 'No hay nada en el carrito...' %} + + {% endif %} +
+
+ +
+
+

{% translate 'Resumen del pedido' %}

+ +
+
+
+
{% translate 'Base imponible' %}
+
{{ base_total }}
+
+ +
+
{% translate 'Descuentos' %}
+
-{{ savings }}
+
+ +
+
{% translate 'Gastos de envío' %}
+
{{ shipping_cost }} €
+
+ +
+
{% translate 'Impuestos' %}
+
{{ tax_total }}
+
+
+ +
+
{% translate 'Total' %}
+
{{ total }}
+
+
+ +
+ +
+ + +
+ + {# #} + {#
#} + {#
#} + {# #} + {# #} + {#
#} + {# #} + {#
#} + {#
#} +
+
+ +
\ No newline at end of file diff --git a/web/templates/components/cart/cart_navbar.html b/web/templates/components/cart/cart_navbar.html index 4c8ae35..2d37cd4 100644 --- a/web/templates/components/cart/cart_navbar.html +++ b/web/templates/components/cart/cart_navbar.html @@ -70,8 +70,8 @@ {% if cart.items.count > 0 %} {% translate 'Ver carrito' %} diff --git a/web/templates/components/icons/x.svg b/web/templates/components/icons/x.svg new file mode 100644 index 0000000..96a1b09 --- /dev/null +++ b/web/templates/components/icons/x.svg @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/web/templates/users/login.html b/web/templates/users/login.html index eb17c85..eaf0b1d 100644 --- a/web/templates/users/login.html +++ b/web/templates/users/login.html @@ -5,7 +5,7 @@
- + logo {{ title }} diff --git a/web/templates/web/cart_detail.html b/web/templates/web/cart_detail.html index 519db9c..3903330 100644 --- a/web/templates/web/cart_detail.html +++ b/web/templates/web/cart_detail.html @@ -3,26 +3,5 @@ {% block main %} {% include 'components/generic/navbar.html' %} -
-
- -
-
- -
-
-{% endblock %} \ No newline at end of file +
+{% endblock %} diff --git a/web/urls.py b/web/urls.py index 005a055..88930d6 100644 --- a/web/urls.py +++ b/web/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from web.views.components import cart_dropdown, list_products +from web.views.components import cart, cart_dropdown, list_products from web.views.users import login, logout, register, reset_password from web.views.web import ( add_cart_item, @@ -12,7 +12,6 @@ from web.views.web import ( app_name = "web" - urlpatterns = [ path("", index, name="index"), path("products///", product_detail, name="product_detail"), @@ -25,6 +24,7 @@ urlpatterns = [ # components path("web/components/products/", list_products, name="list_products"), path("web/components/cart-dropdown/", cart_dropdown, name="cart_dropdown"), + path("web/components/cart/", cart, name="cart"), # api path("web/add-cart-item/", add_cart_item, name="add_cart_item"), path("web/delete-cart-item//", delete_cart_item, name="delete_cart_item"), diff --git a/web/utils.py b/web/utils.py index bcc75dd..8a00b3f 100644 --- a/web/utils.py +++ b/web/utils.py @@ -1,4 +1,8 @@ -from shop.models import Cart +from decimal import Decimal + +from django.db.models import Sum + +from shop.models import Cart, CartItem from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME @@ -16,3 +20,9 @@ def get_or_create_cart(request) -> [Cart, bool]: cart, created = Cart.objects.get_or_create(user=request.user) return cart, created + + +def get_cart_total(cart: Cart): + return CartItem.objects.filter(cart=cart).annotate(cart_total=Sum("total")).get( + "cart_total" + ) or Decimal("0.00") diff --git a/web/views/components.py b/web/views/components.py index 9233fb2..948a6d7 100644 --- a/web/views/components.py +++ b/web/views/components.py @@ -1,11 +1,14 @@ +from decimal import Decimal + +from django.db.models import Sum from django.shortcuts import render from django.views.generic import TemplateView from shop.filters import ProductFilter -from shop.models import Product +from shop.models import CartItem, Product, ProductPrice from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME -from web.utils import get_or_create_cart +from web.utils import get_cart_total, get_or_create_cart class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin): @@ -42,4 +45,35 @@ def cart_dropdown(request, *args, **kwargs): return response +def cart(request, *args, **kwargs): + cart, created = get_or_create_cart(request) + items = CartItem.objects.filter(cart=cart) + + base_total = Decimal("0.00") + tax_total = Decimal("0.00") + + for item in items: + price = ProductPrice.objects.filter(product=item.product, current=True).first() + base_total += price.price * item.quantity + tax_total += round(price.price * Decimal(price.tax.value / 100), 2) + + total = base_total + tax_total + + response = render( + request, + "components/cart/cart.html", + context={ + "cart": cart, + "items": items, + "total": total, + "base_total": base_total, + "tax_total": tax_total, + }, + ) + + if not request.user.is_authenticated: + response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid) + return response + + list_products = ListProducts.as_view()