From 58ddc4706511033611d09bb62902fbe35b354f9a Mon Sep 17 00:00:00 2001 From: eeintech Date: Mon, 5 Apr 2021 11:21:34 -0400 Subject: [PATCH] Updated migration files to handle duplicate manufacturer data --- .../migrations/0032_manufacturerpart.py | 69 -------------- .../migrations/0033_supplierpart_update.py | 91 +++++++++++++++++++ 2 files changed, 91 insertions(+), 69 deletions(-) create mode 100644 InvenTree/company/migrations/0033_supplierpart_update.py diff --git a/InvenTree/company/migrations/0032_manufacturerpart.py b/InvenTree/company/migrations/0032_manufacturerpart.py index 646a46edd6..0f7450b32a 100644 --- a/InvenTree/company/migrations/0032_manufacturerpart.py +++ b/InvenTree/company/migrations/0032_manufacturerpart.py @@ -2,63 +2,10 @@ import InvenTree.fields from django.db import migrations, models import django.db.models.deletion -def supplierpart_make_manufacturer_parts(apps, schema_editor): - Part = apps.get_model('part', 'Part') - ManufacturerPart = apps.get_model('company', 'ManufacturerPart') - SupplierPart = apps.get_model('company', 'SupplierPart') - - print(f'\nCreating Manufacturer parts\n{"-"*10}') - for supplier_part in SupplierPart.objects.all(): - print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='') - - if supplier_part.manufacturer_part: - print(f'[ERROR: MANUFACTURER PART ALREADY EXISTS]') - continue - - part = supplier_part.part - if not part: - print(f'[ERROR: SUPPLIER PART IS NOT CONNECTED TO PART]') - continue - - manufacturer = supplier_part.manufacturer - MPN = supplier_part.MPN - link = supplier_part.link - description = supplier_part.description - - if manufacturer or MPN: - print(f' | {part.name[:15].ljust(15)}', end='') - - try: - print(f' | {manufacturer.name[:15].ljust(15)}', end='') - except TypeError: - print(f' | {"EMPTY MANUF".ljust(15)}', end='') - - try: - print(f' | {MPN[:15].ljust(15)}', end='') - except TypeError: - print(f' | {"EMPTY MPN".ljust(15)}', end='') - - print('\t', end='') - - # Create ManufacturerPart - manufacturer_part = ManufacturerPart(part=part, manufacturer=manufacturer, MPN=MPN, link=link, description=description) - manufacturer_part.save() - - # Link it to SupplierPart - supplier_part.manufacturer_part = manufacturer_part - supplier_part.save() - - print(f'[SUCCESS: MANUFACTURER PART CREATED]') - else: - print(f'[IGNORED: MISSING MANUFACTURER DATA]') - - print(f'{"-"*10}\nDone') - class Migration(migrations.Migration): dependencies = [ - ('part', '0063_bomitem_inherited'), ('company', '0031_auto_20210103_2215'), ] @@ -77,20 +24,4 @@ class Migration(migrations.Migration): 'unique_together': {('part', 'manufacturer', 'MPN')}, }, ), - migrations.AddField( - model_name='supplierpart', - name='manufacturer_part', - field=models.ForeignKey(blank=True, help_text='Select manufacturer part', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='supplier_parts', to='company.ManufacturerPart', verbose_name='Manufacturer Part'), - ), - # Make new ManufacturerPart with SupplierPart "manufacturer" and "MPN" - # fields, then link it to the new SupplierPart "manufacturer_part" field - migrations.RunPython(supplierpart_make_manufacturer_parts), - migrations.RemoveField( - model_name='supplierpart', - name='MPN', - ), - migrations.RemoveField( - model_name='supplierpart', - name='manufacturer', - ), ] diff --git a/InvenTree/company/migrations/0033_supplierpart_update.py b/InvenTree/company/migrations/0033_supplierpart_update.py new file mode 100644 index 0000000000..16d224269a --- /dev/null +++ b/InvenTree/company/migrations/0033_supplierpart_update.py @@ -0,0 +1,91 @@ +import InvenTree.fields +from django.db import migrations, models, transaction +import django.db.models.deletion +from django.db.utils import IntegrityError + +def supplierpart_make_manufacturer_parts(apps, schema_editor): + Part = apps.get_model('part', 'Part') + ManufacturerPart = apps.get_model('company', 'ManufacturerPart') + SupplierPart = apps.get_model('company', 'SupplierPart') + + print(f'\nCreating Manufacturer parts\n{"-"*10}') + for supplier_part in SupplierPart.objects.all(): + print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='') + + if supplier_part.manufacturer_part: + print(f'[ERROR: MANUFACTURER PART ALREADY EXISTS]') + continue + + part = supplier_part.part + if not part: + print(f'[ERROR: SUPPLIER PART IS NOT CONNECTED TO PART]') + continue + + manufacturer = supplier_part.manufacturer + MPN = supplier_part.MPN + link = supplier_part.link + description = supplier_part.description + + if manufacturer or MPN: + print(f' | {part.name[:15].ljust(15)}', end='') + + try: + print(f' | {manufacturer.name[:15].ljust(15)}', end='') + except TypeError: + print(f' | {"EMPTY MANUF".ljust(15)}', end='') + + try: + print(f' | {MPN[:15].ljust(15)}', end='') + except TypeError: + print(f' | {"EMPTY MPN".ljust(15)}', end='') + + print('\t', end='') + + # Create ManufacturerPart + manufacturer_part = ManufacturerPart(part=part, manufacturer=manufacturer, MPN=MPN, description=description, link=link) + created = False + try: + with transaction.atomic(): + manufacturer_part.save() + created = True + except IntegrityError: + manufacturer_part = ManufacturerPart.objects.get(part=part, manufacturer=manufacturer, MPN=MPN) + + # Link it to SupplierPart + supplier_part.manufacturer_part = manufacturer_part + supplier_part.save() + + if created: + print(f'[SUCCESS: MANUFACTURER PART CREATED]') + else: + print(f'[IGNORED: MANUFACTURER PART ALREADY EXISTS]') + else: + print(f'[IGNORED: MISSING MANUFACTURER DATA]') + + print(f'{"-"*10}\nDone') + + +class Migration(migrations.Migration): + + dependencies = [ + ('company', '0032_manufacturerpart'), + ] + + operations = [ + migrations.AddField( + model_name='supplierpart', + name='manufacturer_part', + field=models.ForeignKey(blank=True, help_text='Select manufacturer part', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='supplier_parts', to='company.ManufacturerPart', verbose_name='Manufacturer Part'), + ), + # Make new ManufacturerPart with SupplierPart "manufacturer" and "MPN" + # fields, then link it to the new SupplierPart "manufacturer_part" field + migrations.RunPython(supplierpart_make_manufacturer_parts), + migrations.RemoveField( + model_name='supplierpart', + name='MPN', + ), + migrations.RemoveField( + model_name='supplierpart', + name='manufacturer', + ), + ]