Coverage for src/backend/InvenTree/part/migrations/0083_auto_20220731_2357.py: 40%

19 statements  

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

1# Generated by Django 3.2.14 on 2022-07-31 23:57 

2 

3from django.db import migrations 

4 

5from InvenTree.helpers import constructPathString 

6 

7 

8def update_pathstring(apps, schema_editor): 

9 """Construct pathstring for all existing PartCategory objects""" 

10 

11 PartCategory = apps.get_model('part', 'partcategory') 

12 

13 n = PartCategory.objects.count() 

14 

15 if n > 0: 15 ↛ 17line 15 didn't jump to line 17 because the condition on line 15 was never true

16 

17 for cat in PartCategory.objects.all(): 

18 

19 # Construct complete path for category 

20 path = [cat.name] 

21 

22 parent = cat.parent 

23 

24 # Iterate up the tree 

25 while parent is not None: 

26 path = [parent.name] + path 

27 parent = parent.parent 

28 

29 pathstring = constructPathString(path) 

30 

31 cat.pathstring = pathstring 

32 cat.save() 

33 

34 print(f"\n--- Updated 'pathstring' for {n} PartCategory objects ---\n") 

35 

36 

37class Migration(migrations.Migration): 

38 

39 dependencies = [ 

40 ('part', '0082_partcategory_pathstring'), 

41 ] 

42 

43 operations = [ 

44 migrations.RunPython( 

45 update_pathstring, 

46 reverse_code=migrations.RunPython.noop 

47 ) 

48 ]