From 63f01db7ba5073ba68abdda63613ad4aa1363855 Mon Sep 17 00:00:00 2001 From: Pablo Moreno Date: Thu, 2 Jan 2025 12:17:24 +0100 Subject: [PATCH] feat: wishlist view --- config/settings/base.py | 2 ++ .../components/products/wishlist_button.html | 2 +- .../components/users/user_dropdown.html | 2 +- web/templates/web/wishlist.html | 31 +++++++++++++++++++ web/tests/test_wishlist.py | 31 +++++++++++++++++-- web/urls.py | 17 ++++++++-- web/views/components.py | 27 ++++++++++++++++ web/views/web.py | 13 ++++++++ 8 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 web/templates/web/wishlist.html diff --git a/config/settings/base.py b/config/settings/base.py index a0e4417..b42bc0b 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -135,6 +135,8 @@ STATIC_ROOT = BASE_DIR / "static" MEDIA_URL = "/media/" MEDIA_ROOT = BASE_DIR / "media" +LOGIN_REDIRECT_URL = "/login/" + STORAGES = { "default": { "BACKEND": "django.core.files.storage.FileSystemStorage", diff --git a/web/templates/components/products/wishlist_button.html b/web/templates/components/products/wishlist_button.html index 3e24ef3..a309935 100644 --- a/web/templates/components/products/wishlist_button.html +++ b/web/templates/components/products/wishlist_button.html @@ -4,7 +4,7 @@ {% csrf_token %} diff --git a/web/templates/components/users/user_dropdown.html b/web/templates/components/users/user_dropdown.html index 7e8d53e..64d42ee 100644 --- a/web/templates/components/users/user_dropdown.html +++ b/web/templates/components/users/user_dropdown.html @@ -19,7 +19,7 @@
  • - {% translate 'Lista de deseados' %} diff --git a/web/templates/web/wishlist.html b/web/templates/web/wishlist.html new file mode 100644 index 0000000..a0aa7f7 --- /dev/null +++ b/web/templates/web/wishlist.html @@ -0,0 +1,31 @@ +{% extends 'theme/base.html' %} +{% load static i18n %} + +{% block main %} + {% include 'components/generic/navbar.html' %} +
    +
    +

    + {% translate 'Lista de deseados' %} +

    + +
    +
    + +
    + +
    + +
    +
    +
    +
    +{% endblock %} diff --git a/web/tests/test_wishlist.py b/web/tests/test_wishlist.py index cce278d..94df9e7 100644 --- a/web/tests/test_wishlist.py +++ b/web/tests/test_wishlist.py @@ -38,10 +38,12 @@ class TestWishlist(TestCase, CreateProductsMixin): reverse("web:add_to_wishlist"), { "product": self.product.pk, - } + }, ) assert response.status_code == 201 - assert WishlistedProduct.objects.filter(user=self.user, product=self.product).exists() + assert WishlistedProduct.objects.filter( + user=self.user, product=self.product + ).exists() def test_logged_user_delete_wishlisted_product(self): wishlisted = WishlistedProduct.objects.create( @@ -53,4 +55,27 @@ class TestWishlist(TestCase, CreateProductsMixin): reverse("web:delete_from_wishlist", kwargs={"pk": wishlisted.pk}) ) assert response.status_code == 204 - assert not WishlistedProduct.objects.filter(user=self.user, product=self.product).exists() + assert not WishlistedProduct.objects.filter( + user=self.user, product=self.product + ).exists() + + def test_logged_user_wishlist(self): + WishlistedProduct.objects.create( + user=self.user, + product=self.product, + ) + self.client.force_login(self.user) + response = self.client.get( + reverse("web:wishlist") + ) + assert response.status_code == 200 + + def test_not_logged_user_wishlist(self): + WishlistedProduct.objects.create( + user=self.user, + product=self.product, + ) + response = self.client.get( + reverse("web:wishlist") + ) + assert response.status_code == 302 diff --git a/web/urls.py b/web/urls.py index db367a8..ce8d5a1 100644 --- a/web/urls.py +++ b/web/urls.py @@ -1,6 +1,12 @@ from django.urls import path - -from web.views.components import cart, cart_dropdown, list_products, wishlist_button +from django.contrib.auth.decorators import login_required +from web.views.components import ( + cart, + cart_dropdown, + list_products, + wishlist_button, + list_wishlisted_products, +) from web.views.users import login, logout, my_account, register, reset_password from web.views.web import ( add_cart_item, @@ -12,12 +18,14 @@ from web.views.web import ( index, manifest, product_detail, + wishlist, ) app_name = "web" urlpatterns = [ path("", index, name="index"), + path("wishlist", login_required(wishlist, login_url="/login/"), name="wishlist"), path("categories//", category_view, name="category_view"), path("products///", product_detail, name="product_detail"), path("cart/", cart_detail, name="cart_detail"), @@ -29,6 +37,11 @@ urlpatterns = [ path("my-account/", my_account, name="my_account"), # components path("web/components/products/", list_products, name="list_products"), + path( + "web/components/wishlist/", + list_wishlisted_products, + name="list_wishlisted_products", + ), path("web/components/cart-dropdown/", cart_dropdown, name="cart_dropdown"), path("web/components/cart/", cart, name="cart"), path( diff --git a/web/views/components.py b/web/views/components.py index 2215614..3a471f0 100644 --- a/web/views/components.py +++ b/web/views/components.py @@ -28,6 +28,32 @@ class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin): } +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) @@ -91,4 +117,5 @@ class WishlistButton(TemplateView): list_products = ListProducts.as_view() +list_wishlisted_products = ListWishlistedProducts.as_view() wishlist_button = WishlistButton.as_view() diff --git a/web/views/web.py b/web/views/web.py index d99a5e9..cf20d35 100644 --- a/web/views/web.py +++ b/web/views/web.py @@ -71,6 +71,18 @@ class CartDetail(TemplateView): } +class WishlistView(TemplateView): + template_name = "web/wishlist.html" + + def get_context_data(self, **kwargs): + settings = WebSettings.load() + return { + "title": settings.web_title, + "web_title": settings.web_title, + "description": settings.web_description, + } + + @require_http_methods( [ "POST", @@ -188,3 +200,4 @@ index = IndexView.as_view() category_view = CategoryView.as_view() product_detail = ProductDetail.as_view() cart_detail = CartDetail.as_view() +wishlist = WishlistView.as_view()