Coverage for src/backend/InvenTree/build/migrations/0036_auto_20220707_1101.py: 66%
26 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.14 on 2022-07-07 11:01
3from django.db import migrations
6def update_build_reference(apps, schema_editor):
7 """Update the build order reference.
9 Ref: https://github.com/inventree/InvenTree/pull/3267
11 Performs the following steps:
12 - Extract existing 'prefix' value
13 - Generate a build order pattern based on the prefix value
14 - Update any existing build order references with the specified prefix
15 """
17 InvenTreeSetting = apps.get_model('common', 'inventreesetting')
19 try:
20 prefix = InvenTreeSetting.objects.get(key='BUILDORDER_REFERENCE_PREFIX').value
21 except Exception:
22 prefix = 'BO-'
24 # Construct a reference pattern
25 pattern = prefix + '{ref:04d}'
27 # Create or update the BuildOrder.reference pattern
28 try:
29 setting = InvenTreeSetting.objects.get(key='BUILDORDER_REFERENCE_PATTERN')
30 setting.value = pattern
31 setting.save()
32 except InvenTreeSetting.DoesNotExist:
33 setting = InvenTreeSetting.objects.create(
34 key='BUILDORDER_REFERENCE_PATTERN',
35 value=pattern,
36 )
38 # Update any existing build order references with the prefix
39 Build = apps.get_model('build', 'build')
41 n = 0
43 for build in Build.objects.all(): 43 ↛ 44line 43 didn't jump to line 44 because the loop on line 43 never started
44 if not build.reference.startswith(prefix):
45 build.reference = prefix + build.reference
46 build.save()
47 n += 1
49 if n > 0: 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true
50 print(f"Updated reference field for {n} BuildOrder objects")
53class Migration(migrations.Migration):
55 dependencies = [
56 ('build', '0035_alter_build_notes'),
57 ]
59 operations = [
60 migrations.RunPython(
61 update_build_reference,
62 reverse_code=migrations.RunPython.noop,
63 )
64 ]