From 73e926193beacd8d040da995ec39a6deca3cfd6e Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Mon, 12 Dec 2016 07:03:49 -0500
Subject: [PATCH] Added category export.

---
 wooify | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/wooify b/wooify
index 4ce8da7..07be835 100644
--- a/wooify
+++ b/wooify
@@ -140,30 +140,42 @@ def ecwid_parser(r, r_id=None):
 def expand_category(name, cat_id, slugify=None, delimiter=None, products=None):
     """Build a rich category entry from minimal input."""
     slugify = slugify or UniqueSlugify()
+    clean_name = (name[name.rfind(delimiter) + len(delimiter):]
+                  if delimiter and delimiter in name else name)
     return {
         'Id': cat_id,
         'Term Taxonomy Id': cat_id,
-        'Name': (name[name.rfind(delimiter):]
-                 if delimiter and delimiter in name else name),
-        'Slug': slugify(name),
-        'Parent Raw Name': (name[:name.rfind(delimiter)]
-                            if delimiter and delimiter in name
-                            else ''),
+        'Name': clean_name,
+        'Slug': slugify(clean_name),
+        'Parent Name': (name[:name.rfind(delimiter)]
+                        if delimiter and delimiter in name
+                        else ''),
         'Count': (sum([1 for p in products if name in p['Categories']])
                   if products else 0),
     }
 
 
-def add_missing_parents(categories):
+def add_missing_parents(categories, products, delimiter, slugify=None):
     """Find categories whose parents do not exist, and recursively add
        those parents to the list."""
+    def split_category_name(name):
+        """Return the name of each of this categories ancestors, and itself."""
+        return [(name[:name.rfind(delimiter)]
+                 if delimiter and delimiter in name
+                 else '') for i in range(name.count(delimiter))] + [name]
+    slugify = slugify or UniqueSlugify()
+    categories_with_parents = categories.copy()
     for i, c in categories.items():
-        if not c['Parent Raw Name']:
-            continue
-        if c['Parent Raw Name'] in categories:
+        try:
+            for ancestor in split_category_name(c['Parent Name']):
+                if ancestor not in categories_with_parents:
+                    cat_id = len(categories_with_parents) + 1
+                    categories_with_parents[ancestor] = expand_category(
+                        ancestor, cat_id, slugify, delimiter,
+                        products)
+        except KeyError:
             continue
-        # TODO: this
-    return categories
+    return categories_with_parents
 
 
 def make_woocommerce_csv(input_file, products_file, categories_file=None):
@@ -185,26 +197,27 @@ def make_woocommerce_csv(input_file, products_file, categories_file=None):
             raw_category_names.extend(product['Categories'])
         raw_category_names = list(set(raw_category_names))  # Uniquify
         categories = {}
-        slugify = UniqueSlugify()
+        slugify = UniqueSlugify(to_lower=True)
         for idx, name in enumerate(raw_category_names):
             categories[name] = expand_category(name, idx, slugify,
                                                ECWID_CATEGORY_DELIMITER,
                                                products)
-        categories = add_missing_parents(categories)
+        categories = add_missing_parents(categories, products,
+                                         ECWID_CATEGORY_DELIMITER, slugify)
         for i, c in categories.items():
-            if c['Parent Raw Name']:
-                parent = categories[c['Parent Raw Name']]
+            if c['Parent Name']:
+                parent = categories[c['Parent Name']]
                 c['Parent Id'] = parent['Id']
                 c['Parent Slug'] = parent['Slug']
             else:
                 c['Parent Id'] = ''
                 c['Parent Slug'] = ''
-            del c['Parent Raw Name']
+            del c['Parent Name']
 
         for product in products:
             products_writer.writerow(product)
 
-        if csv_gategories:
+        if csv_categories:
             categories_writer = csv.DictWriter(
                 csv_categories,
                 fieldnames=[f for f, d in WOO_IMP_EXP_CATEGORY_FIELDS])
-- 
GitLab