Add 'InstanceName' setting

- Will be auto-generated if it does not already exist
This commit is contained in:
Oliver Walters 2020-04-06 19:28:08 +10:00
parent 7c2af32b38
commit 071c317bae
2 changed files with 32 additions and 1 deletions

View File

@ -2,7 +2,7 @@ from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError from django.db.utils import OperationalError, ProgrammingError
import os import os
import uuid
import yaml import yaml
@ -10,14 +10,19 @@ class CommonConfig(AppConfig):
name = 'common' name = 'common'
def ready(self): def ready(self):
""" Will be called when the Common app is first loaded """ """ Will be called when the Common app is first loaded """
self.populate_default_settings() self.populate_default_settings()
self.add_instance_name()
def populate_default_settings(self): def populate_default_settings(self):
""" Populate the default values for InvenTree key:value pairs. """ Populate the default values for InvenTree key:value pairs.
If a setting does not exist, it will be created. 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 from .models import InvenTreeSetting
here = os.path.dirname(os.path.abspath(__file__)) here = os.path.dirname(os.path.abspath(__file__))
@ -46,3 +51,26 @@ class CommonConfig(AppConfig):
except (OperationalError, ProgrammingError): except (OperationalError, ProgrammingError):
# Migrations have not yet been applied - table does not exist # Migrations have not yet been applied - table does not exist
break 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()

View File

@ -4,6 +4,9 @@
# Note: The description strings provided here will be translatable, # Note: The description strings provided here will be translatable,
# so ensure that any translations are provided as appropriate. # 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' - key: 'part_ipn_regex'
default: '' default: ''
description: 'Format string for internal part number' description: 'Format string for internal part number'