mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Split supplier part update migration and added reverse method for manufacturer data
This commit is contained in:
		
							
								
								
									
										18
									
								
								InvenTree/company/migrations/0035_supplierpart_update_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								InvenTree/company/migrations/0035_supplierpart_update_1.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					import InvenTree.fields
 | 
				
			||||||
 | 
					from django.db import migrations, models
 | 
				
			||||||
 | 
					import django.db.models.deletion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('company', '0034_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'),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
@@ -11,7 +11,7 @@ def supplierpart_make_manufacturer_parts(apps, schema_editor):
 | 
				
			|||||||
    supplier_parts = SupplierPart.objects.all()
 | 
					    supplier_parts = SupplierPart.objects.all()
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if supplier_parts:
 | 
					    if supplier_parts:
 | 
				
			||||||
        print(f'\nCreating Manufacturer parts\n{"-"*10}')
 | 
					        print(f'\nCreating ManufacturerPart Objects\n{"-"*10}')
 | 
				
			||||||
        for supplier_part in supplier_parts:
 | 
					        for supplier_part in supplier_parts:
 | 
				
			||||||
            print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='')
 | 
					            print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -67,29 +67,44 @@ def supplierpart_make_manufacturer_parts(apps, schema_editor):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        print(f'{"-"*10}\nDone\n')
 | 
					        print(f'{"-"*10}\nDone\n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def supplierpart_populate_manufacturer_info(apps, schema_editor):
 | 
				
			||||||
 | 
					    Part = apps.get_model('part', 'Part')
 | 
				
			||||||
 | 
					    ManufacturerPart = apps.get_model('company', 'ManufacturerPart')
 | 
				
			||||||
 | 
					    SupplierPart = apps.get_model('company', 'SupplierPart')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    supplier_parts = SupplierPart.objects.all()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if supplier_parts:
 | 
				
			||||||
 | 
					        print(f'\nSupplierPart: Populating Manufacturer Information\n{"-"*10}')
 | 
				
			||||||
 | 
					        for supplier_part in supplier_parts:
 | 
				
			||||||
 | 
					            print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='')
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            manufacturer_part = supplier_part.manufacturer_part
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if manufacturer_part:
 | 
				
			||||||
 | 
					                if manufacturer_part.manufacturer:
 | 
				
			||||||
 | 
					                    supplier_part.manufacturer = manufacturer_part.manufacturer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if manufacturer_part.MPN:
 | 
				
			||||||
 | 
					                    supplier_part.MPN = manufacturer_part.MPN
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                supplier_part.save()
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                print(f'[SUCCESS: UPDATED MANUFACTURER INFO]')
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                print(f'[IGNORED: NO MANUFACTURER PART]')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        print(f'{"-"*10}\nDone\n')
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Migration(migrations.Migration):
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    dependencies = [
 | 
					    dependencies = [
 | 
				
			||||||
        ('company', '0033_auto_20210410_1528'),
 | 
					        ('company', '0035_supplierpart_update_1'),
 | 
				
			||||||
        ('company', '0034_manufacturerpart'),
 | 
					 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    operations = [
 | 
					    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"
 | 
					        # Make new ManufacturerPart with SupplierPart "manufacturer" and "MPN"
 | 
				
			||||||
        # fields, then link it to the new SupplierPart "manufacturer_part" field
 | 
					        # fields, then link it to the new SupplierPart "manufacturer_part" field
 | 
				
			||||||
        migrations.RunPython(supplierpart_make_manufacturer_parts),
 | 
					        migrations.RunPython(supplierpart_make_manufacturer_parts, reverse_code=supplierpart_populate_manufacturer_info),
 | 
				
			||||||
        migrations.RemoveField(
 | 
					 | 
				
			||||||
            model_name='supplierpart',
 | 
					 | 
				
			||||||
            name='MPN',
 | 
					 | 
				
			||||||
        ),
 | 
					 | 
				
			||||||
        migrations.RemoveField(
 | 
					 | 
				
			||||||
            model_name='supplierpart',
 | 
					 | 
				
			||||||
            name='manufacturer',
 | 
					 | 
				
			||||||
        ),
 | 
					 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
							
								
								
									
										21
									
								
								InvenTree/company/migrations/0037_supplierpart_update_3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								InvenTree/company/migrations/0037_supplierpart_update_3.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					import InvenTree.fields
 | 
				
			||||||
 | 
					from django.db import migrations, models
 | 
				
			||||||
 | 
					import django.db.models.deletion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('company', '0036_supplierpart_update_2'),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    operations = [
 | 
				
			||||||
 | 
					        migrations.RemoveField(
 | 
				
			||||||
 | 
					            model_name='supplierpart',
 | 
				
			||||||
 | 
					            name='MPN',
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        migrations.RemoveField(
 | 
				
			||||||
 | 
					            model_name='supplierpart',
 | 
				
			||||||
 | 
					            name='manufacturer',
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
@@ -109,11 +109,11 @@ class TestManufacturerField(MigratorTestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class TestManufacturerPart(MigratorTestCase):
 | 
					class TestManufacturerPart(MigratorTestCase):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Tests for migration 0034 and 0035 which added and transitioned to the ManufacturerPart model
 | 
					    Tests for migration 0034-0037 which added and transitioned to the ManufacturerPart model
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    migrate_from = ('company', '0033_auto_20210410_1528')
 | 
					    migrate_from = ('company', '0033_auto_20210410_1528')
 | 
				
			||||||
    migrate_to = ('company', '0035_supplierpart_update')
 | 
					    migrate_to = ('company', '0037_supplierpart_update_3')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def prepare(self):
 | 
					    def prepare(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user