Coverage for src/backend/InvenTree/company/migrations/0079_auto_20260212_1054.py: 44%

19 statements  

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

1# Generated by Django 5.2.11 on 2026-02-12 10:54 

2 

3from django.db import migrations 

4 

5 

6def link_primary_supplier_part(apps, schema_editor): 

7 """Mark 'primary' SupplierPart for each Part, if one exists.""" 

8 

9 Part = apps.get_model("part", "Part") 

10 SupplierPart = apps.get_model("company", "SupplierPart") 

11 

12 # Find any part which links to a "default_supplier" 

13 primary_supplier_ids = Part.objects.exclude( 

14 default_supplier=None, 

15 ).values_list("default_supplier_id", flat=True).distinct() 

16 

17 if len(primary_supplier_ids) > 0: 17 ↛ 19line 17 didn't jump to line 19 because the condition on line 17 was never true

18 # Mark the relevant SupplierPart objects as "primary" 

19 SupplierPart.objects.filter( 

20 pk__in=primary_supplier_ids 

21 ).update(primary=True) 

22 

23 print(f"Marked {len(primary_supplier_ids)} SupplierPart objects as primary") 

24 

25 

26def reverse_link_primary_supplier_part(apps, schema_editor): 

27 """Add 'primary' SupplierPart for each Part.""" 

28 

29 SupplierPart = apps.get_model("company", "SupplierPart") 

30 

31 # Find any SupplierPart object marked as "primary" 

32 primary_supplier_parts = SupplierPart.objects.filter(primary=True) 

33 

34 if len(primary_supplier_parts) > 0: 

35 # Unmark the relevant SupplierPart objects as "primary" 

36 

37 for supplier_part in primary_supplier_parts: 

38 supplier_part.part.default_supplier = supplier_part 

39 supplier_part.part.save() 

40 

41 print(f"Linked {len(primary_supplier_parts)} primary SupplierPart objects") 

42 

43 

44class Migration(migrations.Migration): 

45 

46 dependencies = [ 

47 ("company", "0078_supplierpart_primary"), 

48 ] 

49 

50 operations = [ 

51 migrations.RunPython( 

52 code=link_primary_supplier_part, 

53 reverse_code=reverse_link_primary_supplier_part, 

54 ), 

55 ]