Product type tags

This commit is contained in:
Виктор Гладких 2019-11-19 15:04:12 +03:00
parent c9a0fb6656
commit 39a406cbc1

View File

@ -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()