140 lines
4.0 KiB
Python
140 lines
4.0 KiB
Python
from decimal import Decimal
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.views.generic import TemplateView
|
|
|
|
from shop.filters import ProductFilter
|
|
from shop.models import (
|
|
CartItem,
|
|
Product,
|
|
ProductPrice,
|
|
WishlistedProduct,
|
|
CustomerAddress,
|
|
ShippingMethod,
|
|
)
|
|
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):
|
|
template_name = "web/list_products.html"
|
|
queryset = Product.objects.filter(hidden=False).prefetch_related(
|
|
"images",
|
|
)
|
|
filter_class = ProductFilter
|
|
|
|
def get_context_data(self, **kwargs):
|
|
qs = self.get_queryset()
|
|
page = self.get_paginated_queryset(qs)
|
|
|
|
return {
|
|
"page": page,
|
|
"has_next_page": page.has_next(),
|
|
"has_previous_page": page.has_previous(),
|
|
}
|
|
|
|
|
|
class ListWishlistedProducts(
|
|
TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin
|
|
):
|
|
template_name = "web/list_products.html"
|
|
queryset = Product.objects.filter(hidden=False).prefetch_related(
|
|
"images",
|
|
)
|
|
filter_class = ProductFilter
|
|
|
|
def get_queryset(self):
|
|
product_ids = WishlistedProduct.objects.filter(
|
|
user=self.request.user
|
|
).values_list("product", flat=True)
|
|
return super().get_queryset().filter(pk__in=product_ids).distinct()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
qs = self.get_queryset()
|
|
page = self.get_paginated_queryset(qs)
|
|
|
|
return {
|
|
"page": page,
|
|
"has_next_page": page.has_next(),
|
|
"has_previous_page": page.has_previous(),
|
|
}
|
|
|
|
|
|
def cart_dropdown(request, *args, **kwargs):
|
|
cart, created = get_or_create_cart(request)
|
|
|
|
response = render(
|
|
request,
|
|
"components/cart/cart_navbar.html",
|
|
context={
|
|
"cart": cart,
|
|
},
|
|
)
|
|
|
|
if not request.user.is_authenticated:
|
|
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
|
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
|
|
|
|
billing_addresses = CustomerAddress.objects.filter(
|
|
user=request.user, address_type=CustomerAddress.Types.BILLING
|
|
)
|
|
shipping_addresses = CustomerAddress.objects.filter(
|
|
user=request.user, address_type=CustomerAddress.Types.SHIPPING
|
|
)
|
|
shipping_methods = ShippingMethod.objects.all()
|
|
|
|
response = render(
|
|
request,
|
|
"components/cart/cart.html",
|
|
context={
|
|
"cart": cart,
|
|
"items": items,
|
|
"total": total,
|
|
"base_total": base_total,
|
|
"tax_total": tax_total,
|
|
"billing_addresses": billing_addresses,
|
|
"shipping_addresses": shipping_addresses,
|
|
"shipping_methods": shipping_methods,
|
|
},
|
|
)
|
|
|
|
if not request.user.is_authenticated:
|
|
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
|
return response
|
|
|
|
|
|
class WishlistButton(TemplateView):
|
|
template_name = "components/products/wishlist_button.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
product = get_object_or_404(Product, pk=self.kwargs.get("pk"))
|
|
is_wishlisted = WishlistedProduct.objects.filter(
|
|
user=self.request.user, product=product
|
|
).exists()
|
|
|
|
return {
|
|
"is_wishlisted": is_wishlisted,
|
|
"product": product,
|
|
}
|
|
|
|
|
|
list_products = ListProducts.as_view()
|
|
list_wishlisted_products = ListWishlistedProducts.as_view()
|
|
wishlist_button = WishlistButton.as_view()
|