Fix tag add product
This commit is contained in:
parent
23a1af1870
commit
903db06f0e
|
|
@ -43,7 +43,8 @@ class Command(BaseCommand):
|
|||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select
|
||||
DISTINCT
|
||||
DISTINCT
|
||||
m.id as old_id,
|
||||
trim(CONVERT(m.value USING utf8)) as tag_value,
|
||||
trim(CONVERT(v.key_name USING utf8)) as tag_category
|
||||
FROM product_metadata m
|
||||
|
|
@ -56,16 +57,22 @@ class Command(BaseCommand):
|
|||
for t in tqdm(self.tag_sql(), desc='Add tags'):
|
||||
category = TagCategory.objects.get(index_name=t.tag_category)
|
||||
|
||||
tag = Tag.objects.filter(
|
||||
tags = Tag.objects.filter(
|
||||
category=category,
|
||||
value=t.tag_value
|
||||
)
|
||||
|
||||
if not tag.exists():
|
||||
if not tags.exists():
|
||||
objects.append(Tag(label={"en-GB": t.tag_value},
|
||||
category=category,
|
||||
value=t.tag_value
|
||||
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)
|
||||
|
||||
|
||||
Tag.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Add or get tag objects.'))
|
||||
|
||||
|
|
@ -74,6 +81,7 @@ class Command(BaseCommand):
|
|||
cursor.execute('''
|
||||
select
|
||||
DISTINCT
|
||||
m.id as old_id_tag,
|
||||
m.product_id,
|
||||
lower(trim(CONVERT(m.value USING utf8))) as tag_value,
|
||||
trim(CONVERT(v.key_name USING utf8)) as tag_category
|
||||
|
|
@ -83,16 +91,12 @@ class Command(BaseCommand):
|
|||
return namedtuplefetchall(cursor)
|
||||
|
||||
def add_product_tag(self):
|
||||
objects = []
|
||||
for t in tqdm(self.product_sql(), desc='Add product tag'):
|
||||
category = TagCategory.objects.get(index_name=t.tag_category)
|
||||
tag = Tag.objects.annotate(lower_value=Lower('value'))
|
||||
tag.filter(lower_value=t.tag_value, category=category)
|
||||
products = Product.objects.filter(old_id=t.product_id)
|
||||
if products.exists():
|
||||
for p in products:
|
||||
p.tags.add(*list(tag))
|
||||
p.save()
|
||||
tags = Tag.objects.filter(old_id_meta_product=t.old_id_tag)
|
||||
product = Product.objects.get(old_id=t.product_id)
|
||||
for tag in tags:
|
||||
if product not in tag.products.all():
|
||||
product.tags.add(tag)
|
||||
|
||||
self.stdout.write(self.style.WARNING(f'Add or get tag objects.'))
|
||||
|
||||
|
|
@ -110,7 +114,7 @@ class Command(BaseCommand):
|
|||
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
self.add_category_tag()
|
||||
self.add_tag()
|
||||
self.check_tag()
|
||||
# self.add_category_tag()
|
||||
# self.add_tag()
|
||||
# self.check_tag()
|
||||
self.add_product_tag()
|
||||
|
|
|
|||
18
apps/tag/migrations/0014_tag_old_id_meta_product.py
Normal file
18
apps/tag/migrations/0014_tag_old_id_meta_product.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.7 on 2019-11-13 11:22
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('tag', '0013_auto_20191113_0930'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='tag',
|
||||
name='old_id_meta_product',
|
||||
field=models.PositiveIntegerField(blank=True, default=None, null=True, verbose_name='old id metadata product'),
|
||||
),
|
||||
]
|
||||
|
|
@ -39,6 +39,9 @@ class Tag(TranslatedFieldsMixin, models.Model):
|
|||
|
||||
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
|
||||
|
||||
old_id_meta_product = models.PositiveIntegerField(_('old id metadata product'),
|
||||
blank=True, null=True, default=None)
|
||||
|
||||
objects = TagQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user