Merge branch 'fix/tag_product' into 'develop'

Fix category, poduct-tags

See merge request gm/gm-backend!140
This commit is contained in:
d.kuzmenko 2019-11-19 12:34:40 +00:00
commit a00cb91b8b

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
@ -26,18 +26,43 @@ 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.'))
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('''
@ -64,22 +89,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 +117,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,7 +143,9 @@ class Command(BaseCommand):
tag.save()
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()