Kaynağa Gözat

add product mpn, link_rewrite and category link_rewrite fields

sxoinas12 2 yıl önce
ebeveyn
işleme
3fb656db00

+ 9 - 9
lint_types/report.xml

@@ -1,14 +1,14 @@
 <?xml version="1.0" encoding="utf-8"?>
-<testsuite errors="0" failures="1" name="mypy" skips="0" tests="1" time="1.375">
-  <testcase classname="mypy" file="mypy" line="1" name="mypy-py3_8-darwin" time="1.375">
+<testsuite errors="0" failures="1" name="mypy" skips="0" tests="1" time="1.389">
+  <testcase classname="mypy" file="mypy" line="1" name="mypy-py3_8-darwin" time="1.389">
     <failure message="mypy produced messages">telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:1: error: Relative import climbs too many namespaces
 telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:2: error: Relative import climbs too many namespaces
 telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:6: error: Function is missing a type annotation
-telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:41: error: Function is missing a type annotation
-telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:42: error: Call to untyped function "product" in typed context
-telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:45: error: Function is missing a type annotation
-telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:53: error: Function is missing a type annotation
-telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:54: error: Call to untyped function "category" in typed context
+telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:43: error: Function is missing a type annotation
+telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:44: error: Call to untyped function "product" in typed context
+telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:47: error: Function is missing a type annotation
+telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:56: error: Function is missing a type annotation
+telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py:57: error: Call to untyped function "category" in typed context
 telecaster/telecaster/parsers/parse_xml_to_json.py:1: error: Function is missing a type annotation
 telecaster/telecaster/parsers/parse_xml_to_json.py:5: error: Call to untyped function "parse_xml_to_json" in typed context
 telecaster/telecaster/parsers/parse_xml_to_json.py:12: error: Function is missing a type annotation
@@ -31,7 +31,7 @@ telecaster/telecaster/serializers/XmlGeneratorSerializer.py:10: error: Class can
 telecaster/telecaster/serializers/XmlGeneratorSerializer.py:12: error: Function is missing a type annotation
 telecaster/telecaster/serializers/XmlGeneratorSerializer.py:16: error: Function is missing a type annotation
 telecaster/telecaster/serializers/XmlGeneratorSerializer.py:29: error: Function is missing a type annotation
-telecaster/telecaster/serializers/XmlGeneratorSerializer.py:71: error: Call to untyped function "validate_product_for_skroutz" of "XmlGeneratorSerializer" in typed context
+telecaster/telecaster/serializers/XmlGeneratorSerializer.py:81: error: Call to untyped function "validate_product_for_skroutz" of "XmlGeneratorSerializer" in typed context
 telecaster/telecaster/Clients/PrestaShopClient.py:4: error: Relative import climbs too many namespaces
 telecaster/telecaster/Clients/PrestaShopClient.py:7: error: Class cannot subclass "BaseClient" (has type "Any")
 telecaster/telecaster/Clients/PrestaShopClient.py:8: error: Function is missing a type annotation
@@ -44,7 +44,7 @@ telecaster/telecaster/views/XmlGeneratorView.py:11: error: Class cannot subclass
 telecaster/telecaster/views/XmlGeneratorView.py:17: error: Function is missing a type annotation for one or more arguments
 telecaster/telecaster/views/XmlGeneratorView.py:21: error: Function is missing a type annotation for one or more arguments
 telecaster/telecaster/views/XmlGeneratorView.py:39: error: Call to untyped function "for_xml" of "XmlGeneratorSerializer" in typed context
-telecaster/telecaster/views/XmlGeneratorView.py:42: error: Returning Any from function declared to return "JsonResponse"
+telecaster/telecaster/views/XmlGeneratorView.py:43: error: Returning Any from function declared to return "JsonResponse"
 telecaster/telecaster/tests/conftest.py:8: error: Function is missing a return type annotation
 telecaster/telecaster/tests/conftest.py:13: error: Function is missing a type annotation
 telecaster/telecaster/tests/conftest.py:14: error: Function is missing a type annotation

+ 3 - 3
telecaster/telecaster/Clients/PrestaShopClient.py

@@ -11,11 +11,11 @@ class PrestaShopClient(BaseClient):
 
     def build_url(self, url, params={}):
         updated_params = {**params, 'ws_key': self.token}
-        return f'{self.base_url}{url}?{urllib.parse.urlencode(updated_params)}'
+        return f'{self.base_url}/api{url}?{urllib.parse.urlencode(updated_params)}'
 
     def get_products(self, params={}):
         constructed_params = {
-            'display': '[name, id, id_default_image, id_category_default,price,wholesale_price,manufacturer_name,ean13,delivery_in_stock,additional_shipping_cost,description,quantity]',
+            'display': '[name, id, mpn, id_default_image, id_category_default,price,wholesale_price,manufacturer_name,ean13,delivery_in_stock,additional_shipping_cost,description,quantity, link_rewrite]',
             **params,
         }
         full_url = self.build_url('/products', constructed_params)
@@ -30,7 +30,7 @@ class PrestaShopClient(BaseClient):
         return result
 
     def get_categories(self, params={}):
-        constructed_params = {'display': '[id,name]', **params}
+        constructed_params = {'display': '[id,name, link_rewrite]', **params}
         full_url = self.build_url('/categories', constructed_params)
         xml_categories_content = self.get(full_url)
         xml_categories = ElementTree.fromstring(xml_categories_content)

BIN
telecaster/telecaster/__pycache__/settings.cpython-38.pyc


+ 2 - 1
telecaster/telecaster/models/CategoryModel.py

@@ -1,4 +1,5 @@
 class CategoryModel:
-    def __init__(self, category_id: int, name: str):
+    def __init__(self, category_id: int, name: str, slug: str):
         self.category_id = category_id
         self.name = name
+        self.slug = slug

+ 4 - 0
telecaster/telecaster/models/ProductModel.py

@@ -15,6 +15,8 @@ class ProductModel:
         additional_shipping_cost: float,
         description: str,
         quantity: int,
+        slug: str,
+        mpn: str,
         category: CategoryModel,
     ):
         self.product_id = product_id
@@ -29,3 +31,5 @@ class ProductModel:
         self.additional_shipping_cost = additional_shipping_cost
         self.description = description
         self.quantity = quantity
+        self.slug = slug
+        self.mpn = mpn

+ 5 - 2
telecaster/telecaster/sanitizers/prestashop_backoffice_api_sanitizer.py

@@ -26,6 +26,7 @@ class PrestashopBackOfficeApiSanitizer:
             'name': raw_product.get('name', {}).get('language', ''),
             'image': raw_product['id_default_image'],
             'price': float(raw_product['price']),
+            'slug': str(raw_product['link_rewrite'].get('language', '')) or '',
             'wholesale_price': float(raw_product['wholesale_price']),
             'manufacturer': str(raw_product['manufacturer_name']),
             'ena13': ean13,
@@ -33,7 +34,8 @@ class PrestashopBackOfficeApiSanitizer:
             'additional_shipping_cost': float(raw_product['additional_shipping_cost']),
             'description': str(raw_product.get('description', {}).get('language', '')),
             'quantity': int(raw_product['quantity']),
-            'category': CategoryModel(category_id=int(raw_product['id_category_default']), name=''),
+            'mpn': str(raw_product['mpn']),
+            'category': CategoryModel(category_id=int(raw_product['id_category_default']), name='', slug=''),
         }
 
         return ProductModel(**sanitized_product)
@@ -44,8 +46,9 @@ class PrestashopBackOfficeApiSanitizer:
 
     def category(self, raw_category):
         sanitized_category = {
-            'category_id': raw_category['id'],
+            'category_id': int(raw_category['id']),
             'name': raw_category.get('name', {}).get('language', ''),
+            'slug': raw_category.get('link_rewrite', {}).get('language', ''),
         }
 
         return CategoryModel(**sanitized_category)

+ 14 - 6
telecaster/telecaster/serializers/XmlGeneratorSerializer.py

@@ -26,7 +26,7 @@ class XmlGeneratorSerializer(serializers.Serializer):
         return True
 
     @classmethod
-    def for_xml(self, products=[], categories={}):
+    def for_xml(self, products=[], categories={}, site_url=''):
         """Generates an xml based on -> https://developer.skroutz.gr/el/feedspec/#xml"""
 
         root = gfg.Element("mywebstore")
@@ -46,13 +46,23 @@ class XmlGeneratorSerializer(serializers.Serializer):
         skroutz_products = []
 
         for product in products:
+            category_object = categories.get(product.get('category', {}).category_id, {})
+            # Probably all this logic with link must be moved into sanitizer
+            # Link must be refactored since it is heavily depended on Prestashop Settings.
+            category_name = category_object.get('name', '')
+            category_slug = category_object.get('slug', '')
+            product_slug = product.get('slug', '')
+            link = ''
+            if product_slug and category_slug and site_url:
+                link = f'{site_url}/{category_slug}/{product_slug}'
+
             skroutz_product = {
                 'id': str(product.get('product_id')),
                 'name': product.get('name'),
-                'link': product.get('link', 'hardcodedlin.com'),  # fix this from prestashop API
+                'link': link,  # fix this from prestashop API
                 'image': product.get('image'),
                 'additionalimage': product.get('additionalimage'),
-                'category': categories.get(product.get('category', {}).category_id, {}).get('name', ''),
+                'category': category_name,
                 'price_with_vat': product.get('wholesale_price'),
                 'vat': product.get('vat'),
                 'manufacturer': product.get('manufacturer'),
@@ -61,7 +71,7 @@ class XmlGeneratorSerializer(serializers.Serializer):
                 'instock': product.get('in_stock'),
                 'availability': str(
                     product.get('availability', 'Παραδοση 1 εως 3 ημερε')
-                ),  # fix this from prestashop API
+                ),  # fix this from Sanitizer
                 'size': product.get('size'),
                 'weight': product.get('weight'),
                 'color': product.get('color'),  # maybe make it array based on skroutz
@@ -80,8 +90,6 @@ class XmlGeneratorSerializer(serializers.Serializer):
 
                 child = gfg.Element(key)
                 if key in skroutz_cdata_fields:
-                    print(key, value)
-
                     child.text = cdata_parser(value)
                 else:
                     child.text = value

+ 0 - 1
telecaster/telecaster/settings.py

@@ -50,7 +50,6 @@ MIDDLEWARE = [
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]
 
-
 ROOT_URLCONF = 'telecaster.urls'
 
 TEMPLATES = [

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

@@ -26,7 +26,7 @@ class XmlGeneratorView(views.APIView):
 
         # retrieve categories first since they are needed for products
         categories = prestashop_client.get_categories()
-        products = prestashop_client.get_products({'limit': 4})
+        products = prestashop_client.get_products({'limit': 5})
         sanitizer = PrestashopBackOfficeApiSanitizer()
 
         sanitized_categories = sanitizer.categories(categories)
@@ -36,8 +36,8 @@ class XmlGeneratorView(views.APIView):
 
         # serialize response
         serializer = XmlGeneratorSerializer()
-        response_xml = serializer.for_xml(products=sanitized_products, categories=categories_dict)
-        breakpoint()
+        response_xml = serializer.for_xml(
+            products=sanitized_products, categories=categories_dict, site_url=url
+        )
 
         return Response(response_xml, content_type='text/xml')
-        # return JsonResponse()