diff --git a/InvenTree/common/apps.py b/InvenTree/common/apps.py index d055aeba74..4b16fc5236 100644 --- a/InvenTree/common/apps.py +++ b/InvenTree/common/apps.py @@ -2,7 +2,7 @@ from django.apps import AppConfig from django.db.utils import OperationalError, ProgrammingError import os - +import uuid import yaml @@ -10,14 +10,19 @@ class CommonConfig(AppConfig): name = 'common' def ready(self): + """ Will be called when the Common app is first loaded """ self.populate_default_settings() + self.add_instance_name() def populate_default_settings(self): """ Populate the default values for InvenTree key:value pairs. If a setting does not exist, it will be created. """ + # Import this here, rather than at the global-level, + # otherwise it is called all the time, and we don't want that, + # as the InvenTreeSetting model may have not been instantiated yet. from .models import InvenTreeSetting here = os.path.dirname(os.path.abspath(__file__)) @@ -46,3 +51,26 @@ class CommonConfig(AppConfig): except (OperationalError, ProgrammingError): # Migrations have not yet been applied - table does not exist break + + def add_instance_name(self): + """ + Check if an InstanceName has been defined for this database. + If not, create a random one! + """ + + # See note above + from .models import InvenTreeSetting + + if not InvenTreeSetting.objects.filter(key='InstanceName').exists(): + + val = uuid.uuid4().hex + + print("No 'InstanceName' found - generating random name '{n}'".format(n=val)) + + name = InvenTreeSetting( + key="InstanceName", + value=val, + description="Instance name for this InvenTree database installation." + ) + + name.save() diff --git a/InvenTree/common/kvp.yaml b/InvenTree/common/kvp.yaml index 9200b22c31..d0f7249dff 100644 --- a/InvenTree/common/kvp.yaml +++ b/InvenTree/common/kvp.yaml @@ -4,6 +4,9 @@ # Note: The description strings provided here will be translatable, # so ensure that any translations are provided as appropriate. +# TODO: Update the formatting here to include logical separators e.g. double-underscore +# TODO: This is so when there are enough options, we will be able to display them as a tree + - key: 'part_ipn_regex' default: '' description: 'Format string for internal part number'