From 39a406cbc178559369b114bcd7ac561574cdb4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 19 Nov 2019 15:04:12 +0300 Subject: [PATCH] Product type tags --- .../management/commands/add_product_tag.py | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/product/management/commands/add_product_tag.py b/apps/product/management/commands/add_product_tag.py index fb4542cc..74427110 100644 --- a/apps/product/management/commands/add_product_tag.py +++ b/apps/product/management/commands/add_product_tag.py @@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand from django.db import connections from establishment.management.commands.add_position import namedtuplefetchall from tag.models import Tag, TagCategory -from product.models import Product +from product.models import Product, ProductType from tqdm import tqdm @@ -40,6 +40,29 @@ class Command(BaseCommand): TagCategory.objects.bulk_create(objects) self.stdout.write(self.style.WARNING(f'Add or get tag category objects.')) + + def product_type_category_sql(self): + with connections['legacy'].cursor() as cursor: + cursor.execute(''' + select + DISTINCT + trim(CONVERT(v.key_name USING utf8)) as tag_category + FROM product_metadata m + join product_key_value_metadata v on v.id = m.product_key_value_metadatum_id + join products p on p.id = m.product_id + where UPPER(trim(p.type)) = 'WINE' + ''') + return namedtuplefetchall(cursor) + + def add_type_product_category(self): + for c in tqdm(self.product_type_category_sql(), desc='Add type product category'): + types = ProductType.objects.filter(index_name='wine') + category = TagCategory.objects.get(index_name=c.tag_category) + if types.exists() and category not in types.tag_categories.all(): + types.tag_categories.add(category) + + self.stdout.write(self.style.WARNING(f'Add type product category objects.')) + def tag_sql(self): with connections['legacy'].cursor() as cursor: cursor.execute(''' @@ -122,6 +145,7 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): self.remove_tags_product() self.add_category_tag() + self.add_type_product_category() self.add_tag() self.check_tag() self.add_product_tag()