mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 12:05:53 +00:00
Improve "hook" for loading custom unit database
This commit is contained in:
@ -11,6 +11,7 @@ from django.core.exceptions import AppRegistryNotReady
|
|||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
|
|
||||||
|
import InvenTree.conversion
|
||||||
import InvenTree.tasks
|
import InvenTree.tasks
|
||||||
from InvenTree.config import get_setting
|
from InvenTree.config import get_setting
|
||||||
from InvenTree.ready import canAppAccessDatabase, isInTestMode
|
from InvenTree.ready import canAppAccessDatabase, isInTestMode
|
||||||
@ -46,6 +47,9 @@ class InvenTreeConfig(AppConfig):
|
|||||||
|
|
||||||
self.collect_notification_methods()
|
self.collect_notification_methods()
|
||||||
|
|
||||||
|
# Ensure the unit registry is loaded
|
||||||
|
InvenTree.conversion.reload_unit_registry()
|
||||||
|
|
||||||
if canAppAccessDatabase() or settings.TESTING_ENV:
|
if canAppAccessDatabase() or settings.TESTING_ENV:
|
||||||
self.add_user_on_startup()
|
self.add_user_on_startup()
|
||||||
|
|
||||||
|
@ -15,13 +15,26 @@ def get_unit_registry():
|
|||||||
|
|
||||||
# Cache the unit registry for speedier access
|
# Cache the unit registry for speedier access
|
||||||
if _unit_registry is None:
|
if _unit_registry is None:
|
||||||
_unit_registry = pint.UnitRegistry()
|
reload_unit_registry()
|
||||||
|
|
||||||
# TODO: Allow for custom units to be defined in the database
|
|
||||||
|
|
||||||
return _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):
|
def convert_physical_value(value: str, unit: str = None):
|
||||||
"""Validate that the provided value is a valid physical quantity.
|
"""Validate that the provided value is a valid physical quantity.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user