diff --git a/apps/product/management/commands/add_product_tag.py b/apps/product/management/commands/add_product_tag.py index ee4829f5..fb4542cc 100644 --- a/apps/product/management/commands/add_product_tag.py +++ b/apps/product/management/commands/add_product_tag.py @@ -26,15 +26,17 @@ class Command(BaseCommand): def add_category_tag(self): objects = [] for c in tqdm(self.category_sql(), desc='Add category tags'): - categories = TagCategory.objects.filter(index_name=c.category - ) + categories = TagCategory.objects.filter(index_name=c.category) if not categories.exists(): objects.append( TagCategory(label={"en-GB": c.category}, value_type=c.value_type, - index_name=c.category + index_name=c.category, + public=True ) ) + else: + categories.update(public=True) TagCategory.objects.bulk_create(objects) self.stdout.write(self.style.WARNING(f'Add or get tag category objects.')) @@ -64,22 +66,24 @@ class Command(BaseCommand): if not tags.exists(): objects.append(Tag(label={"en-GB": t.tag_value}, category=category, - value=t.tag_value, - old_id_meta_product=t.old_id - )) - else: - qs = tags.filter(old_id_meta_product__isnull=True)\ - .update(old_id_meta_product=t.old_id) + value=t.tag_value) + ) + Tag.objects.bulk_create(objects) self.stdout.write(self.style.WARNING(f'Add or get tag objects.')) + def remove_tags_product(self): + print('Begin clear tags product') + products = Product.objects.all() + products.tags.clear() + print('End clear tags product') + def product_sql(self): with connections['legacy'].cursor() as cursor: cursor.execute(''' select - DISTINCT - m.id as old_id_tag, + DISTINCT m.product_id, lower(trim(CONVERT(m.value USING utf8))) as tag_value, trim(CONVERT(v.key_name USING utf8)) as tag_category @@ -90,7 +94,12 @@ class Command(BaseCommand): def add_product_tag(self): for t in tqdm(self.product_sql(), desc='Add product tag'): - tags = Tag.objects.filter(old_id_meta_product=t.old_id_tag) + category = TagCategory.objects.get(index_name=t.tag_category) + + tags = Tag.objects.filter( + category=category, + value=t.tag_value + ) product = Product.objects.get(old_id=t.product_id) for tag in tags: if product not in tag.products.all(): @@ -111,6 +120,7 @@ class Command(BaseCommand): tag.save() def handle(self, *args, **kwargs): + self.remove_tags_product() self.add_category_tag() self.add_tag() self.check_tag()