feat: wishlist
This commit is contained in:
+16
-2
@@ -1,10 +1,10 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.filters import ProductFilter
|
||||
from shop.models import CartItem, Product, ProductPrice
|
||||
from shop.models import CartItem, Product, ProductPrice, WishlistedProduct
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
@@ -75,4 +75,18 @@ def cart(request, *args, **kwargs):
|
||||
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()
|
||||
wishlist_button = WishlistButton.as_view()
|
||||
|
||||
+32
-1
@@ -4,7 +4,7 @@ from django.utils.text import gettext_lazy as _
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.models import CartItem, Product, ProductCategory
|
||||
from shop.models import CartItem, Product, ProductCategory, WishlistedProduct
|
||||
from web.models import WebSettings
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
@@ -114,6 +114,37 @@ def delete_cart_item(request, pk, *args, **kwargs):
|
||||
return HttpResponse(status=204, headers={"HX-Trigger": "updated-cart"})
|
||||
|
||||
|
||||
@require_http_methods(
|
||||
[
|
||||
"POST",
|
||||
]
|
||||
)
|
||||
def add_to_wishlist(request, *args, **kwargs):
|
||||
product = get_object_or_404(Product, pk=request.POST.get("product"))
|
||||
response = HttpResponse(status=201, headers={"HX-Trigger": "updated-wishlist"})
|
||||
|
||||
if request.user.is_anonymous:
|
||||
return HttpResponse(status=400)
|
||||
|
||||
WishlistedProduct.objects.get_or_create(
|
||||
user=request.user,
|
||||
product=product,
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@require_http_methods(
|
||||
[
|
||||
"POST",
|
||||
]
|
||||
)
|
||||
def delete_from_wishlist(request, pk, *args, **kwargs):
|
||||
wishlisted_item = get_object_or_404(WishlistedProduct, product_id=pk, user=request.user)
|
||||
wishlisted_item.delete()
|
||||
return HttpResponse(status=204, headers={"HX-Trigger": "updated-wishlist"})
|
||||
|
||||
|
||||
def manifest(request, *args, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user