Skip to content

Commit

Permalink
list products by a catalog endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Jan 15, 2024
1 parent 9ef7f16 commit f75de5b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
CatalogViewSet.as_view({"get": "retrieve", "delete": "destroy"}),
name="catalog-detail-delete",
),
path(
"<uuid:app_uuid>/catalogs/<uuid:catalog_uuid>/products/",
CatalogViewSet.as_view({"get": "list_products"}),
name="catalog-list-products",
),
path(
"<uuid:app_uuid>/catalogs/<uuid:catalog_uuid>/enable/",
CatalogViewSet.as_view({"post": "enable_catalog"}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from marketplace.wpp_products.serializers import (
CatalogSerializer,
ProductSerializer,
ToggleVisibilitySerializer,
TresholdSerializer,
CatalogListSerializer,
Expand Down Expand Up @@ -90,6 +91,11 @@ def get_object(self):
queryset = self.get_queryset()
catalog_uuid = self.kwargs.get("catalog_uuid")
return get_object_or_404(queryset, uuid=catalog_uuid)

def _get_catalog(self, catalog_uuid, app_uuid):
return get_object_or_404(
Catalog, uuid=catalog_uuid, app__uuid=app_uuid, app__code="wpp-cloud"
)

def create(self, request, app_uuid, *args, **kwargs):
app = get_object_or_404(App, uuid=app_uuid, code="wpp-cloud")
Expand Down Expand Up @@ -144,6 +150,13 @@ def list(self, request, *args, **kwargs):

return self.get_paginated_response(serialized_data)

@action(detail=True, methods=["GET"], url_path="list-products")
def list_products(self, request, app_uuid, catalog_uuid, *args, **kwargs):
catalog = self._get_catalog(catalog_uuid, app_uuid)
products = catalog.products.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)

def destroy(self, request, *args, **kwargs):
success = self.fb_service.catalog_deletion(self.get_object())
if not success:
Expand Down
21 changes: 20 additions & 1 deletion marketplace/wpp_products/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from marketplace.wpp_products.models import Catalog
from marketplace.wpp_products.models import Catalog, Product


class CatalogSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -45,3 +45,22 @@ def to_representation(self, obj):
serialized_data.insert(0, connected_data)

return serialized_data

class ProductSerializer(serializers.ModelSerializer):
title = serializers.CharField(required=True)
available = serializers.SerializerMethodField()
price = serializers.CharField()
sale_price = serializers.CharField()
facebook_product_id = serializers.CharField()
image_link = serializers.CharField()


class Meta:
model = Product
fields = ("title", "available", "price", "sale_price", "facebook_product_id", "image_link")

def get_available(self, obj):
if obj.availability == 'in stock':
return True
else:
return False

0 comments on commit f75de5b

Please sign in to comment.