Coverage for src/backend/InvenTree/part/migrations/0141_auto_20250722_0303.py: 39%
28 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 4.2.23 on 2025-07-22 03:03
3from django.db import migrations
5def cache_bom_valid(apps, schema_editor):
6 """Calculate and cache the BOM validity for all parts.
8 Procedure:
10 - Find all parts which have linked BOM item(s)
11 - Limit to parts which have a stored BOM checksum
12 - For each such part, calculate and update the BOM "validity"
13 """
15 from InvenTree.tasks import offload_task
16 from part.tasks import check_bom_valid
18 Part = apps.get_model('part', 'Part')
19 BomItem = apps.get_model('part', 'BomItem')
21 # Fetch all BomItem objects
22 bom_items = BomItem.objects.exclude(part=None).prefetch_related('part').distinct()
24 parts_to_update = set()
26 for item in bom_items: 26 ↛ 29line 26 didn't jump to line 29 because the loop on line 26 never started
28 # Parts associated with this BomItem
29 parts = []
31 if item.inherited:
32 # Find all inherited assemblies for this BomItem
33 parts = list(
34 Part.objects.filter(
35 tree_id=item.part.tree_id,
36 lft__gte=item.part.lft,
37 rght__lte=item.part.rght
38 )
39 )
40 else:
41 parts = [item.part]
43 for part in parts:
44 # Part has already been observed - skip
45 if part in parts_to_update:
46 continue
48 # Part has no BOM checksum - skip
49 if not part.bom_checksum:
50 continue
52 # Part has not already been validated
53 if not part.bom_checked_date:
54 continue
56 parts_to_update.add(part)
58 if len(parts_to_update) > 0: 58 ↛ 59line 58 didn't jump to line 59 because the condition on line 58 was never true
59 print(f"\nScheduling {len(parts_to_update)} parts to update BOM validity.")
61 for part in parts_to_update: 61 ↛ 64line 61 didn't jump to line 64 because the loop on line 61 never started
62 # Offload task to recalculate the BOM checksum for this part
63 # The background worker will process these when the server restarts
64 offload_task(
65 check_bom_valid,
66 part.pk,
67 force_async=True,
68 group='part'
69 )
72class Migration(migrations.Migration):
74 dependencies = [
75 ("part", "0140_part_bom_validated"),
76 ]
78 operations = [
79 migrations.RunPython(cache_bom_valid, migrations.RunPython.noop),
80 ]