Coverage for src/backend/InvenTree/company/migrations/0036_supplierpart_update_2.py: 16%

72 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1import InvenTree.fields 

2from django.db import migrations, models, transaction 

3import django.db.models.deletion 

4from django.db.utils import IntegrityError 

5 

6def supplierpart_make_manufacturer_parts(apps, schema_editor): 

7 Part = apps.get_model('part', 'Part') 

8 ManufacturerPart = apps.get_model('company', 'ManufacturerPart') 

9 SupplierPart = apps.get_model('company', 'SupplierPart') 

10 

11 supplier_parts = SupplierPart.objects.all() 

12 

13 if supplier_parts: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true

14 print(f'\nCreating ManufacturerPart Objects\n{"-"*10}') 

15 for supplier_part in supplier_parts: 

16 print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='') 

17 

18 if supplier_part.manufacturer_part: # pragma: no cover 

19 print(f'[ERROR: MANUFACTURER PART ALREADY EXISTS]') 

20 continue 

21 

22 part = supplier_part.part 

23 if not part: # pragma: no cover 

24 print(f'[ERROR: SUPPLIER PART IS NOT CONNECTED TO PART]') 

25 continue 

26 

27 manufacturer = supplier_part.manufacturer 

28 MPN = supplier_part.MPN 

29 link = supplier_part.link 

30 description = supplier_part.description 

31 

32 if manufacturer or MPN: 

33 print(f' | {part.name[:15].ljust(15)}', end='') 

34 

35 try: 

36 print(f' | {manufacturer.name[:15].ljust(15)}', end='') 

37 except AttributeError: 

38 print(f' | {"EMPTY MANUF".ljust(15)}', end='') 

39 

40 try: 

41 print(f' | {MPN[:15].ljust(15)}', end='') 

42 except TypeError: 

43 print(f' | {"EMPTY MPN".ljust(15)}', end='') 

44 

45 print('\t', end='') 

46 

47 # Create ManufacturerPart 

48 manufacturer_part = ManufacturerPart(part=part, manufacturer=manufacturer, MPN=MPN, description=description, link=link) 

49 created = False 

50 try: 

51 with transaction.atomic(): 

52 manufacturer_part.save() 

53 created = True 

54 except IntegrityError: 

55 manufacturer_part = ManufacturerPart.objects.get(part=part, manufacturer=manufacturer, MPN=MPN) 

56 

57 # Link it to SupplierPart 

58 supplier_part.manufacturer_part = manufacturer_part 

59 supplier_part.save() 

60 

61 if created: 

62 print(f'[SUCCESS: MANUFACTURER PART CREATED]') 

63 else: 

64 print(f'[IGNORED: MANUFACTURER PART ALREADY EXISTS]') 

65 else: 

66 print(f'[IGNORED: MISSING MANUFACTURER DATA]') 

67 

68 print(f'{"-"*10}\nDone\n') 

69 

70def supplierpart_populate_manufacturer_info(apps, schema_editor): # pragma: no cover 

71 Part = apps.get_model('part', 'Part') 

72 ManufacturerPart = apps.get_model('company', 'ManufacturerPart') 

73 SupplierPart = apps.get_model('company', 'SupplierPart') 

74 

75 supplier_parts = SupplierPart.objects.all() 

76 

77 if supplier_parts: 

78 print(f'\nSupplierPart: Populating Manufacturer Information\n{"-"*10}') 

79 for supplier_part in supplier_parts: 

80 print(f'{supplier_part.supplier.name[:15].ljust(15)} | {supplier_part.SKU[:15].ljust(15)}\t', end='') 

81 

82 manufacturer_part = supplier_part.manufacturer_part 

83 

84 if manufacturer_part: 

85 if manufacturer_part.manufacturer: 

86 supplier_part.manufacturer = manufacturer_part.manufacturer 

87 

88 if manufacturer_part.MPN: 

89 supplier_part.MPN = manufacturer_part.MPN 

90 

91 supplier_part.save() 

92 

93 print(f'[SUCCESS: UPDATED MANUFACTURER INFO]') 

94 else: 

95 print(f'[IGNORED: NO MANUFACTURER PART]') 

96 

97 print(f'{"-"*10}\nDone\n') 

98 

99 

100class Migration(migrations.Migration): 

101 

102 dependencies = [ 

103 ('company', '0035_supplierpart_update_1'), 

104 ] 

105 

106 operations = [ 

107 # Make new ManufacturerPart with SupplierPart "manufacturer" and "MPN" 

108 # fields, then link it to the new SupplierPart "manufacturer_part" field 

109 migrations.RunPython(supplierpart_make_manufacturer_parts, reverse_code=supplierpart_populate_manufacturer_info), 

110 ]