Coverage for src/backend/InvenTree/company/migrations/0060_auto_20230519_0344.py: 42%

23 statements  

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

1# Generated by Django 3.2.19 on 2023-05-19 03:44 

2 

3from django.db import migrations 

4 

5from InvenTree.helpers import normalize 

6 

7 

8def update_supplier_part_units(apps, schema_editor): 

9 """Migrate existing supplier part units to new field""" 

10 

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

12 

13 supplier_parts = SupplierPart.objects.all() 

14 

15 for sp in supplier_parts: 15 ↛ 16line 15 didn't jump to line 16 because the loop on line 15 never started

16 pack_size = normalize(sp.pack_size) 

17 sp.pack_quantity = str(pack_size) 

18 sp.pack_quantity_native = pack_size 

19 sp.save() 

20 

21 if supplier_parts.count() > 0: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true

22 print(f"Updated {supplier_parts.count()} supplier part units") 

23 

24 

25def reverse_pack_quantity(apps, schema_editor): 

26 """Reverse the migrations""" 

27 

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

29 

30 supplier_parts = SupplierPart.objects.all() 

31 

32 for sp in supplier_parts: 

33 sp.pack_size = sp.pack_quantity_native 

34 sp.save() 

35 

36 if supplier_parts.count() > 0: 

37 print(f"Updated {supplier_parts.count()} supplier part units") 

38 

39class Migration(migrations.Migration): 

40 

41 dependencies = [ 

42 ('company', '0059_supplierpart_pack_units'), 

43 ('part', '0111_auto_20230521_1350'), 

44 ] 

45 

46 operations = [ 

47 migrations.RunPython( 

48 code=update_supplier_part_units, 

49 reverse_code=reverse_pack_quantity, 

50 ) 

51 ]