Skip to content

Commit

Permalink
#562 Create migrations for duplicated products (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemijRodionov authored Sep 7, 2018
1 parent 305ca28 commit 4b4f566
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,26 +253,25 @@ 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)

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

Expand Down
34 changes: 34 additions & 0 deletions shopelectro/migrations/0026_remove_product_duplications.py
Original file line number Diff line number Diff line change
@@ -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),
]

0 comments on commit 4b4f566

Please sign in to comment.