Coverage for src/backend/InvenTree/build/migrations/0029_auto_20210601_1525.py: 23%

25 statements  

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

1# Generated by Django 3.2 on 2021-06-01 05:25 

2 

3import logging 

4 

5from django.db import migrations 

6 

7 

8logger = logging.getLogger('inventree') 

9 

10 

11def assign_bom_items(apps, schema_editor): 

12 """ 

13 Run through existing BuildItem objects, 

14 and assign a matching BomItem 

15 """ 

16 

17 BuildItem = apps.get_model('build', 'builditem') 

18 BomItem = apps.get_model('part', 'bomitem') 

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

20 

21 count_valid = 0 

22 count_total = 0 

23 

24 for build_item in BuildItem.objects.all(): # pragma: no cover 

25 

26 # Try to find a BomItem which matches the BuildItem 

27 # Note: Before this migration, variant stock assignment was not allowed, 

28 # so BomItem lookup should be pretty easy 

29 

30 if count_total == 0: 

31 # First time around 

32 logger.info("Assigning BomItems to existing BuildItem objects") 

33 

34 count_total += 1 

35 

36 try: 

37 bom_item = BomItem.objects.get( 

38 part__id=build_item.build.part.pk, 

39 sub_part__id=build_item.stock_item.part.pk, 

40 ) 

41 

42 build_item.bom_item = bom_item 

43 build_item.save() 

44 

45 count_valid += 1 

46 

47 except BomItem.DoesNotExist: 

48 pass 

49 

50 if count_total > 0: # pragma: no cover 

51 logger.info(f"Assigned BomItem for {count_valid}/{count_total} entries") 

52 

53 

54class Migration(migrations.Migration): 

55 

56 dependencies = [ 

57 ('build', '0028_builditem_bom_item'), 

58 ] 

59 

60 operations = []