Skip to content

Commit

Permalink
Merge branch 'dev' into feature/api_products_catalogue
Browse files Browse the repository at this point in the history
  • Loading branch information
knop-k authored Dec 20, 2023
2 parents d838339 + 9b4f7b3 commit 84b0232
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ def fetch_ceneo_data():
logging.error(f"Failed to fetch data from Ceneo API: {e}")
raise

def parse_categories(self, category_elem):
def parse_categories(self, category_elem, parent_category_id=None):
categories = []
for elem in category_elem:
category_id = int(elem.findtext("Id"))
category = {
'id': int(elem.findtext('Id')),
'name': elem.findtext('Name'),
"id": category_id,
"name": elem.findtext("Name"),
"parent_id": parent_category_id,
}
subcategories_elem = elem.find('Subcategories')
if subcategories_elem is not None:
categories.extend(self.parse_categories(subcategories_elem))
categories.extend(
self.parse_categories(
subcategories_elem, parent_category_id=category_id
)
)
categories.append(category)
return categories

Expand All @@ -56,7 +62,7 @@ def import_ceneo_categories(categories):
if category['id'] in existing_categories:
logging.info(f"Category {category['id']} ({category['name']}) already exists.")
else:
new_categories.append(CeneoCategory(id=category['id'], name=category['name']))
new_categories.append(CeneoCategory(**category))

if new_categories:
CeneoCategory.objects.bulk_create(new_categories, ignore_conflicts=True, update_conflicts=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-12-16 15:14

from django.db import migrations


def delete_all_ceneocategories(apps, schema_editor):
CC = apps.get_model("products_catalogue", "CeneoCategory")
CC.objects.all().delete()


class Migration(migrations.Migration):
dependencies = [
("products_catalogue", "0020_productattribute"),
]

operations = [
migrations.RunPython(delete_all_ceneocategories, delete_all_ceneocategories),
]

0 comments on commit 84b0232

Please sign in to comment.