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
« 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
3import logging
5from django.db import migrations
8logger = logging.getLogger('inventree')
11def assign_bom_items(apps, schema_editor):
12 """
13 Run through existing BuildItem objects,
14 and assign a matching BomItem
15 """
17 BuildItem = apps.get_model('build', 'builditem')
18 BomItem = apps.get_model('part', 'bomitem')
19 Part = apps.get_model('part', 'part')
21 count_valid = 0
22 count_total = 0
24 for build_item in BuildItem.objects.all(): # pragma: no cover
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
30 if count_total == 0:
31 # First time around
32 logger.info("Assigning BomItems to existing BuildItem objects")
34 count_total += 1
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 )
42 build_item.bom_item = bom_item
43 build_item.save()
45 count_valid += 1
47 except BomItem.DoesNotExist:
48 pass
50 if count_total > 0: # pragma: no cover
51 logger.info(f"Assigned BomItem for {count_valid}/{count_total} entries")
54class Migration(migrations.Migration):
56 dependencies = [
57 ('build', '0028_builditem_bom_item'),
58 ]
60 operations = []