|
@@ -1,9 +1,11 @@
|
|
import hashlib
|
|
import hashlib
|
|
|
|
+import json
|
|
import struct
|
|
import struct
|
|
from collections import defaultdict
|
|
from collections import defaultdict
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
from functools import lru_cache
|
|
from functools import lru_cache
|
|
|
|
|
|
|
|
+import xmltodict
|
|
from dicttoxml import dicttoxml
|
|
from dicttoxml import dicttoxml
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
|
|
|
|
@@ -111,9 +113,8 @@ class CosmohomeClient(PrestaShopClient):
|
|
combination,
|
|
combination,
|
|
float(product.get('price')) + float(combination.get('price'))
|
|
float(product.get('price')) + float(combination.get('price'))
|
|
),
|
|
),
|
|
- "Quantity": combination.get('quantity') or serialized_product.get(
|
|
|
|
- 'Quantity'),
|
|
|
|
-
|
|
|
|
|
|
+ "Quantity": combination.get('quantity'),
|
|
|
|
+ "stock": "Y" if int(combination.get('quantity')) > 0 else "N",
|
|
"Color": self.get_color(product) or serialized_product.get('Color'),
|
|
"Color": self.get_color(product) or serialized_product.get('Color'),
|
|
"Size": self.get_size(product) or serialized_product.get('Size'),
|
|
"Size": self.get_size(product) or serialized_product.get('Size'),
|
|
})
|
|
})
|
|
@@ -269,7 +270,48 @@ def generate_xml():
|
|
return final_xml
|
|
return final_xml
|
|
|
|
|
|
|
|
|
|
-def generate_xml_task():
|
|
|
|
|
|
+def generate_xml_task(filepath=None):
|
|
|
|
+ if not filepath:
|
|
|
|
+ raise RuntimeError("No filepath provided")
|
|
xml = generate_xml()
|
|
xml = generate_xml()
|
|
- with open(settings.BASE_DIR / 'artifacts/cosmohome_products.xml', 'w') as file:
|
|
|
|
- file.write(xml)
|
|
|
|
|
|
+ with open(filepath, 'w') as file:
|
|
|
|
+ file.write(xml)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def generate_stats(filepath=None):
|
|
|
|
+ if not filepath:
|
|
|
|
+ raise RuntimeError("No filepath provided")
|
|
|
|
+ with open(filepath, 'r') as file:
|
|
|
|
+ xml_string = file.read()
|
|
|
|
+
|
|
|
|
+ data_dict = xmltodict.parse(xml_string)
|
|
|
|
+ date = data_dict.get('store').get('date')
|
|
|
|
+ products = data_dict.get('store').get('products').get('product')
|
|
|
|
+
|
|
|
|
+ total_products = len(products)
|
|
|
|
+ total_products_in_stock = len([p for p in products if p.get('stock') == 'Y'])
|
|
|
|
+ total_products_out_of_stock = len([p for p in products if p.get('stock') == 'N'])
|
|
|
|
+ duplicate_product_ids_count = total_products - len(set([p.get('productId') for p in products]))
|
|
|
|
+ duplicate_product_mpn_count = total_products - len(set([p.get('MPN') for p in products]))
|
|
|
|
+
|
|
|
|
+ def get_duplicates_by(key, output_key=None):
|
|
|
|
+ duplicates_by_key = defaultdict(list)
|
|
|
|
+ for p in products:
|
|
|
|
+ duplicates_by_key[p.get(key)].append(p.get(output_key) if output_key else p)
|
|
|
|
+ duplicates_by_key = {k: v for k, v in duplicates_by_key.items() if len(v) > 1}
|
|
|
|
+ return duplicates_by_key
|
|
|
|
+
|
|
|
|
+ duplicates_by_mpn = get_duplicates_by('MPN', "productURL")
|
|
|
|
+ duplicates_by_product_id = get_duplicates_by('productId', "productURL")
|
|
|
|
+
|
|
|
|
+ stats = {
|
|
|
|
+ "date": date,
|
|
|
|
+ "total_products": total_products,
|
|
|
|
+ "total_products_in_stock": total_products_in_stock,
|
|
|
|
+ "total_products_out_of_stock": total_products_out_of_stock,
|
|
|
|
+ "duplicate_product_ids_count": duplicate_product_ids_count,
|
|
|
|
+ "duplicate_product_mpn_count": duplicate_product_mpn_count,
|
|
|
|
+ "duplicates_by_mpn": duplicates_by_mpn,
|
|
|
|
+ "duplicates_by_product_id": duplicates_by_product_id,
|
|
|
|
+ }
|
|
|
|
+ return stats
|