فهرست منبع

add BaseSerializer for prestashop products

sxoinas12 2 سال پیش
والد
کامیت
9fb2d1f878

+ 9 - 0
telecaster/telecaster/serializers/PrestaShopProductSerializer.py

@@ -0,0 +1,9 @@
+from rest_framework import serializers
+
+
+class PrestaShopProductSerializer(serializers.BaseSerializer):
+    def to_internal_value(self, data):
+        return data
+
+    def to_representation(self, instance):
+        return {'id': instance['id']}

+ 2 - 1
telecaster/telecaster/serializers/__init__.py

@@ -1,3 +1,4 @@
 from .XmlGeneratorSerializer import XmlGeneratorSerializer
+from .PrestaShopProductSerializer import PrestaShopProductSerializer
 
-__all__ = ['XmlGeneratorSerializer']
+__all__ = ['XmlGeneratorSerializer', 'PrestaShopProductSerializer']

+ 16 - 4
telecaster/telecaster/views/XmlGeneratorView.py

@@ -4,6 +4,7 @@ from rest_framework.request import Request
 from django.http import JsonResponse
 from ..Clients.PrestaShopClient import PrestaShopClient
 from ..parsers.PrestaShopParser import PrestaShopParser
+from ..serializers import PrestaShopProductSerializer
 
 
 class XmlGeneratorView(views.APIView):
@@ -18,9 +19,20 @@ class XmlGeneratorView(views.APIView):
 
         prestashop_client = PrestaShopClient(base_url=url, token=token)
 
-        products_response = prestashop_client.get_products({'limit': 4})
-        categories_response = prestashop_client.get_categories({'limit': 4})
+        products_response = prestashop_client.get_products({'limit': 10})
+        categories_response = prestashop_client.get_categories()
+
+        products = PrestaShopParser.parse_products(products_response)
         categories = PrestaShopParser.parse_categories(categories_response)
 
-        response_json = {categories, products_response}
-        return JsonResponse(response_json, safe=False)
+        for product in products:
+            # worst line in the world
+            # maybe we should have the name directly in the parser
+            product['category'] = (
+                categories.get(product['id_category_default'], {}).get('name', {}).get('language', '')
+            )
+
+        serializer = PrestaShopProductSerializer(data=products, many=True)
+        serializer.is_valid()
+
+        return JsonResponse(serializer.data, safe=False)