Explorar el Código

created basic implementation of PrestaShop parser

sxoinas12 hace 2 años
padre
commit
78e7667383

+ 28 - 0
telecaster/telecaster/parsers/PrestaShopParser.py

@@ -0,0 +1,28 @@
+import xml.etree.ElementTree as ElementTree
+
+
+class PrestaShopParser:
+    def __init__(self):
+        pass
+
+    @classmethod
+    # WIP, parser is not finished yet
+    def categories_helper_parser(self, xmlCategories):
+        response = {}
+        for child in list(xmlCategories):
+            if len(list(child)) > 0:
+                if child.tag == 'category':
+                    self.categories_helper_parser(child)
+                else:
+                    response[child.tag] = self.categories_helper_parser(child)
+            else:
+                response[child.tag] = child.text or ''
+
+        return response
+
+    # parses xml categories response to a dictionary
+    @classmethod
+    def parse_categories(self, xmlCategoriesContent):
+        xmlCategories = ElementTree.fromstring(xmlCategoriesContent)
+        categories = self.categories_helper_parser(xmlCategories)
+        return categories

+ 1 - 1
telecaster/telecaster/parsers/parse_xml_to_json.py

@@ -19,4 +19,4 @@ def parse_prestashop_xml_products(xml):
         else:
             response[child.attrib.get('id')] = child.attrib
 
-    return response
+    return response

+ 11 - 8
telecaster/telecaster/views/XmlGeneratorView.py

@@ -2,9 +2,12 @@ import requests
 
 from rest_framework import views
 from rest_framework.request import Request
-import xml.etree.ElementTree as ElementTree
+
 from django.http import JsonResponse
 from ..parsers.parse_xml_to_json import parse_xml_to_json
+from ..Clients.PrestaShopClient import PrestaShopClient
+from ..parsers.PrestaShopParser import PrestaShopParser
+
 
 class XmlGeneratorView(views.APIView):
     @classmethod
@@ -14,13 +17,13 @@ class XmlGeneratorView(views.APIView):
     @classmethod
     def post(cls, request: Request, *args, **kwargs) -> JsonResponse:
         url = request.data.get('url')
+        token = request.data.get('token')
+
+        prestashop_client = PrestaShopClient(base_url=url, token=token)
 
-        if url:
-            response = requests.get(url)
-            if response and response.status_code == 200:
-                response_xml_as_string = response.content
-                response_xml = ElementTree.fromstring(response_xml_as_string)
-                response_json = parse_xml_to_json(response_xml)
+        products_response = prestashop_client.get_products({'limit': 4})
+        categories_response = prestashop_client.get_categories({'limit': 4})
+        categories = PrestaShopParser.parse_categories(categories_response)
 
-        #serialized_response = XmlGeneratorSerializer.for_api()
+        response_json = {}
         return JsonResponse(response_json, safe=False)