mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Load default settings on InvenTree launch
This commit is contained in:
parent
4b8e44bc4a
commit
356b6cf15b
@ -1,5 +1,43 @@
|
||||
from django.apps import AppConfig
|
||||
from django.db.utils import OperationalError
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class CommonConfig(AppConfig):
|
||||
name = 'common'
|
||||
|
||||
def ready(self):
|
||||
""" Will be called when the Common app is first loaded """
|
||||
self.populate_default_settings()
|
||||
|
||||
def populate_default_settings(self):
|
||||
""" Populate the default values for InvenTree key:value pairs.
|
||||
If a setting does not exist, it will be created.
|
||||
"""
|
||||
|
||||
from .models import InvenTreeSetting
|
||||
|
||||
with open('./common/kvp.yaml') as kvp:
|
||||
values = yaml.safe_load(kvp)
|
||||
|
||||
for value in values:
|
||||
key = value['key']
|
||||
default = value['default']
|
||||
description = value['description']
|
||||
|
||||
try:
|
||||
# If a particular setting does not exist in the database, create it now
|
||||
if not InvenTreeSetting.objects.filter(key=key).exists():
|
||||
setting = InvenTreeSetting(
|
||||
key=key,
|
||||
value=default,
|
||||
description=description
|
||||
)
|
||||
|
||||
setting.save()
|
||||
|
||||
print("Creating new key: '{k}' = '{v}'".format(k=key, v=default))
|
||||
except OperationalError:
|
||||
# Migrations have not yet been applied - table does not exist
|
||||
break
|
||||
|
13
InvenTree/common/kvp.yaml
Normal file
13
InvenTree/common/kvp.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
# This file contains the default values for the key:value settings available in InvenTree
|
||||
# This file should not be edited locally.
|
||||
|
||||
# Note: The description strings provided here will be translatable,
|
||||
# so ensure that any translations are provided as appropriate.
|
||||
|
||||
- key: 'part_ipn_regex'
|
||||
default: ''
|
||||
description: 'Format string for internal part number'
|
||||
|
||||
- key: part_deep_copy
|
||||
default: True
|
||||
description: 'Parts are deep-copied by default'
|
17
InvenTree/common/migrations/0006_auto_20200203_0951.py
Normal file
17
InvenTree/common/migrations/0006_auto_20200203_0951.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Generated by Django 2.2.9 on 2020-02-03 09:51
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('common', '0005_auto_20190915_1256'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='inventreesetting',
|
||||
options={'verbose_name': 'InvenTree Setting', 'verbose_name_plural': 'InvenTree Settings'},
|
||||
),
|
||||
]
|
@ -21,6 +21,10 @@ class InvenTreeSetting(models.Model):
|
||||
even if that key does not exist.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
verbose_name = "InvenTree Setting"
|
||||
verbose_name_plural = "InvenTree Settings"
|
||||
|
||||
@classmethod
|
||||
def get_setting(cls, key, backup_value=None):
|
||||
"""
|
||||
|
@ -6,6 +6,8 @@ from django import template
|
||||
from InvenTree import version
|
||||
from InvenTree.helpers import decimal2string
|
||||
|
||||
from common.models import InvenTreeSetting
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@ -69,3 +71,8 @@ def inventree_github_url(*args, **kwargs):
|
||||
def inventree_docs_url(*args, **kwargs):
|
||||
""" Return URL for InvenTree documenation site """
|
||||
return "https://inventree.github.io"
|
||||
|
||||
|
||||
@register.simple_tag()
|
||||
def inventree_setting(key, *args, **kwargs):
|
||||
return InvenTreeSetting.get_setting(key)
|
||||
|
Loading…
Reference in New Issue
Block a user