Skip to content
Snippets Groups Projects
Commit 73e92619 authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Added category export.

parent 783dd4b6
No related branches found
No related tags found
No related merge requests found
Pipeline #32 passed with stage
in 39 seconds
...@@ -140,30 +140,42 @@ def ecwid_parser(r, r_id=None): ...@@ -140,30 +140,42 @@ def ecwid_parser(r, r_id=None):
def expand_category(name, cat_id, slugify=None, delimiter=None, products=None): def expand_category(name, cat_id, slugify=None, delimiter=None, products=None):
"""Build a rich category entry from minimal input.""" """Build a rich category entry from minimal input."""
slugify = slugify or UniqueSlugify() slugify = slugify or UniqueSlugify()
clean_name = (name[name.rfind(delimiter) + len(delimiter):]
if delimiter and delimiter in name else name)
return { return {
'Id': cat_id, 'Id': cat_id,
'Term Taxonomy Id': cat_id, 'Term Taxonomy Id': cat_id,
'Name': (name[name.rfind(delimiter):] 'Name': clean_name,
if delimiter and delimiter in name else name), 'Slug': slugify(clean_name),
'Slug': slugify(name), 'Parent Name': (name[:name.rfind(delimiter)]
'Parent Raw Name': (name[:name.rfind(delimiter)] if delimiter and delimiter in name
if delimiter and delimiter in name else ''),
else ''),
'Count': (sum([1 for p in products if name in p['Categories']]) 'Count': (sum([1 for p in products if name in p['Categories']])
if products else 0), 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 """Find categories whose parents do not exist, and recursively add
those parents to the list.""" 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(): for i, c in categories.items():
if not c['Parent Raw Name']: try:
continue for ancestor in split_category_name(c['Parent Name']):
if c['Parent Raw Name'] in categories: 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 continue
# TODO: this return categories_with_parents
return categories
def make_woocommerce_csv(input_file, products_file, categories_file=None): 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): ...@@ -185,26 +197,27 @@ def make_woocommerce_csv(input_file, products_file, categories_file=None):
raw_category_names.extend(product['Categories']) raw_category_names.extend(product['Categories'])
raw_category_names = list(set(raw_category_names)) # Uniquify raw_category_names = list(set(raw_category_names)) # Uniquify
categories = {} categories = {}
slugify = UniqueSlugify() slugify = UniqueSlugify(to_lower=True)
for idx, name in enumerate(raw_category_names): for idx, name in enumerate(raw_category_names):
categories[name] = expand_category(name, idx, slugify, categories[name] = expand_category(name, idx, slugify,
ECWID_CATEGORY_DELIMITER, ECWID_CATEGORY_DELIMITER,
products) products)
categories = add_missing_parents(categories) categories = add_missing_parents(categories, products,
ECWID_CATEGORY_DELIMITER, slugify)
for i, c in categories.items(): for i, c in categories.items():
if c['Parent Raw Name']: if c['Parent Name']:
parent = categories[c['Parent Raw Name']] parent = categories[c['Parent Name']]
c['Parent Id'] = parent['Id'] c['Parent Id'] = parent['Id']
c['Parent Slug'] = parent['Slug'] c['Parent Slug'] = parent['Slug']
else: else:
c['Parent Id'] = '' c['Parent Id'] = ''
c['Parent Slug'] = '' c['Parent Slug'] = ''
del c['Parent Raw Name'] del c['Parent Name']
for product in products: for product in products:
products_writer.writerow(product) products_writer.writerow(product)
if csv_gategories: if csv_categories:
categories_writer = csv.DictWriter( categories_writer = csv.DictWriter(
csv_categories, csv_categories,
fieldnames=[f for f, d in WOO_IMP_EXP_CATEGORY_FIELDS]) fieldnames=[f for f, d in WOO_IMP_EXP_CATEGORY_FIELDS])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment