Merge pull request #1887 from matmair/settings-safety

settings fixes
This commit is contained in:
Oliver 2021-08-02 08:38:34 +10:00 committed by GitHub
commit d154ca08ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 10 deletions

View File

@ -0,0 +1,38 @@
"""
Custom management command to cleanup old settings that are not defined anymore
"""
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""
Cleanup old (undefined) settings in the database
"""
def handle(self, *args, **kwargs):
print("Collecting settings")
from common.models import InvenTreeSetting, InvenTreeUserSetting
# general settings
db_settings = InvenTreeSetting.objects.all()
model_settings = InvenTreeSetting.GLOBAL_SETTINGS
# check if key exist and delete if not
for setting in db_settings:
if setting.key not in model_settings:
setting.delete()
print(f"deleted setting '{setting.key}'")
# user settings
db_settings = InvenTreeUserSetting.objects.all()
model_settings = InvenTreeUserSetting.GLOBAL_SETTINGS
# check if key exist and delete if not
for setting in db_settings:
if setting.key not in model_settings:
setting.delete()
print(f"deleted user setting '{setting.key}'")
print("checked all settings")

View File

@ -20,6 +20,7 @@ from djmoney.contrib.exchange.models import convert_money
from djmoney.contrib.exchange.exceptions import MissingRate
from django.utils.translation import ugettext_lazy as _
from django.utils.html import format_html
from django.core.validators import MinValueValidator, URLValidator
from django.core.exceptions import ValidationError
@ -58,12 +59,13 @@ class BaseInvenTreeSetting(models.Model):
# Query the database
for setting in results:
settings.append({
"key": setting.key.upper(),
"value": setting.value
})
if setting.key:
settings.append({
"key": setting.key.upper(),
"value": setting.value
})
keys.add(setting.key.upper())
keys.add(setting.key.upper())
# Specify any "default" values which are not in the database
for key in cls.GLOBAL_SETTINGS.keys():
@ -93,7 +95,7 @@ class BaseInvenTreeSetting(models.Model):
# Wrap strings with quotes
else:
value = f"'{value}'"
value = format_html("'{}'", value)
setting["value"] = value

View File

@ -6,12 +6,12 @@
var user_settings = {
{% for setting in USER_SETTINGS %}
{{ setting.key }}: {{ setting.value|safe }},
{{ setting.key }}: {{ setting.value }},
{% endfor %}
};
var global_settings = {
{% for setting in GLOBAL_SETTINGS %}
{{ setting.key }}: {{ setting.value|safe }},
{{ setting.key }}: {{ setting.value }},
{% endfor %}
};

View File

@ -137,6 +137,14 @@ def rebuild(c):
manage(c, "rebuild_models")
@task
def clean_settings(c):
"""
Clean the setting tables of old settings
"""
manage(c, "clean_settings")
@task
def migrate(c):
"""
@ -167,7 +175,7 @@ def static(c):
manage(c, "collectstatic --no-input")
@task(pre=[install, migrate, static])
@task(pre=[install, migrate, static, clean_settings])
def update(c):
"""
Update InvenTree installation.