mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 12:05:53 +00:00
Adds data migration for converting units to valid values
This commit is contained in:
@ -22,7 +22,7 @@ def update_template_units(apps, schema_editor):
|
|||||||
ureg = InvenTree.conversion.get_unit_registry()
|
ureg = InvenTree.conversion.get_unit_registry()
|
||||||
|
|
||||||
n_converted = 0
|
n_converted = 0
|
||||||
invalid_units = []
|
invalid_units = set()
|
||||||
|
|
||||||
for template in PartParameterTemplate.objects.all():
|
for template in PartParameterTemplate.objects.all():
|
||||||
|
|
||||||
@ -69,8 +69,8 @@ def update_template_units(apps, schema_editor):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
print(f"warningCould not find unit match for {template.units}")
|
print(f"warning: Could not find unit match for {template.units}")
|
||||||
invalid_units.append(template.units)
|
invalid_units.add(template.units)
|
||||||
|
|
||||||
print(f"Updated units for {n_templates} parameter templates")
|
print(f"Updated units for {n_templates} parameter templates")
|
||||||
|
|
||||||
|
89
InvenTree/part/migrations/0111_auto_20230521_1350.py
Normal file
89
InvenTree/part/migrations/0111_auto_20230521_1350.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
# Generated by Django 3.2.19 on 2023-05-21 13:50
|
||||||
|
|
||||||
|
import pint
|
||||||
|
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
import InvenTree.conversion
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_part_units(apps, schema_editor):
|
||||||
|
"""Update the units field for each Part object:
|
||||||
|
|
||||||
|
- Check if the units are valid
|
||||||
|
- Attempt to convert to valid units (if possible)
|
||||||
|
"""
|
||||||
|
|
||||||
|
Part = apps.get_model('part', 'Part')
|
||||||
|
|
||||||
|
parts = Part.objects.exclude(units=None).exclude(units='')
|
||||||
|
n_parts = parts.count()
|
||||||
|
|
||||||
|
ureg = InvenTree.conversion.get_unit_registry()
|
||||||
|
|
||||||
|
invalid_units = set()
|
||||||
|
n_converted = 0
|
||||||
|
|
||||||
|
for part in parts:
|
||||||
|
|
||||||
|
# Override '%' units (which are invalid)
|
||||||
|
if part.units == '%':
|
||||||
|
part.units = 'percent'
|
||||||
|
part.save()
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Test if unit is 'valid'
|
||||||
|
try:
|
||||||
|
ureg.Unit(part.units)
|
||||||
|
continue
|
||||||
|
except pint.errors.UndefinedUnitError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Check a lower-case version
|
||||||
|
try:
|
||||||
|
ureg.Unit(part.units.lower())
|
||||||
|
print(f"Found unit match: {part.units} -> {part.units.lower()}")
|
||||||
|
part.units = part.units.lower()
|
||||||
|
part.save()
|
||||||
|
n_converted += 1
|
||||||
|
continue
|
||||||
|
except pint.errors.UndefinedUnitError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
found = False
|
||||||
|
|
||||||
|
# Attempt to convert to a valid unit
|
||||||
|
for unit in ureg:
|
||||||
|
if unit.lower() == part.units.lower():
|
||||||
|
print("Found unit match: {part.units} -> {unit}")
|
||||||
|
part.units = str(unit)
|
||||||
|
part.save()
|
||||||
|
n_converted += 1
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
print(f"Warning: Invalid units for part '{part}': {part.units}")
|
||||||
|
invalid_units.add(part.units)
|
||||||
|
|
||||||
|
print(f"Updated units for {n_parts} parts")
|
||||||
|
|
||||||
|
if n_converted > 0:
|
||||||
|
print(f"Converted units for {n_converted} parts")
|
||||||
|
|
||||||
|
if len(invalid_units) > 0:
|
||||||
|
print(f"Found {len(invalid_units)} invalid units:")
|
||||||
|
for unit in invalid_units:
|
||||||
|
print(f" - {unit}")
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('part', '0110_alter_part_units'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(code=migrate_part_units, reverse_code=migrations.RunPython.noop)
|
||||||
|
]
|
Reference in New Issue
Block a user