2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-15 11:35:41 +00:00

Improve "hook" for loading custom unit database

This commit is contained in:
Oliver Walters
2023-05-21 22:49:24 +10:00
parent ac81041f09
commit 5268288416
2 changed files with 20 additions and 3 deletions

View File

@ -11,6 +11,7 @@ from django.core.exceptions import AppRegistryNotReady
from django.db import transaction
from django.db.utils import IntegrityError
import InvenTree.conversion
import InvenTree.tasks
from InvenTree.config import get_setting
from InvenTree.ready import canAppAccessDatabase, isInTestMode
@ -46,6 +47,9 @@ class InvenTreeConfig(AppConfig):
self.collect_notification_methods()
# Ensure the unit registry is loaded
InvenTree.conversion.reload_unit_registry()
if canAppAccessDatabase() or settings.TESTING_ENV:
self.add_user_on_startup()

View File

@ -15,13 +15,26 @@ def get_unit_registry():
# Cache the unit registry for speedier access
if _unit_registry is None:
_unit_registry = pint.UnitRegistry()
# TODO: Allow for custom units to be defined in the database
reload_unit_registry()
return _unit_registry
def reload_unit_registry():
"""Reload the unit registry from the database.
This function is called at startup, and whenever the database is updated.
"""
global _unit_registry
_unit_registry = pint.UnitRegistry()
# TODO: Define some "standard" additional unitss
# TODO: Allow for custom units to be defined in the database
def convert_physical_value(value: str, unit: str = None):
"""Validate that the provided value is a valid physical quantity.