diff --git a/shopelectro/management/commands/_update_catalog/update_products.py b/shopelectro/management/commands/_update_catalog/update_products.py index c2d37201..6a9e48ff 100644 --- a/shopelectro/management/commands/_update_catalog/update_products.py +++ b/shopelectro/management/commands/_update_catalog/update_products.py @@ -253,11 +253,15 @@ def save(product, field, value): for page_field, page_value in value.items(): if not getattr(product.page, page_field, ''): setattr(product.page, page_field, page_value) + elif field == 'tags': + product.tags = merge(list(product.tags.all()), value) else: setattr(product, field, value) def merge(left: List, right: List) -> List: """Merge two arrays with order preserving.""" + # Dirty patch for preserving tags, appended from admin. + # Still waiting 1C throwing out. return left + [e for e in right if e not in left] products = Product.objects.filter(uuid__in=data) @@ -265,14 +269,9 @@ def merge(left: List, right: List) -> List: for product in products: product_data = data[str(product.uuid)] for field, value in product_data.items(): - if field != 'tags': - save(product, field, value) - else: - # Dirty patch for preserving tags, appended from admin. - # Still waiting 1C throwing out. - product.tags = merge(list(product.tags.all()), value) - + save(product, field, value) product.save() + logger.info('{} products were updated.'.format(products.count())) return products diff --git a/shopelectro/migrations/0026_remove_product_duplications.py b/shopelectro/migrations/0026_remove_product_duplications.py new file mode 100644 index 00000000..d38c6955 --- /dev/null +++ b/shopelectro/migrations/0026_remove_product_duplications.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2018-08-24 08:44 +from __future__ import unicode_literals + +from django.db import migrations, models + + +def migrate_forward(apps, schema_editor): + Product = apps.get_model('shopelectro', 'Product') + + uuids_to_delete = [ + result['uuid'] for result in + Product.objects.values('uuid').annotate(models.Count('uuid')) + .order_by().filter(uuid__count__gt=1) + ] + + preserved = set() + + for product in Product.objects.filter(uuid__in=uuids_to_delete): + if product.uuid in preserved: + product.delete() + else: + preserved.add(product.uuid) + + +class Migration(migrations.Migration): + + dependencies = [ + ('shopelectro', '0025_tag_unique_constraint'), + ] + + operations = [ + migrations.RunPython(migrate_forward), + ]