mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
commit
d154ca08ea
38
InvenTree/InvenTree/management/commands/clean_settings.py
Normal file
38
InvenTree/InvenTree/management/commands/clean_settings.py
Normal 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")
|
@ -20,6 +20,7 @@ from djmoney.contrib.exchange.models import convert_money
|
|||||||
from djmoney.contrib.exchange.exceptions import MissingRate
|
from djmoney.contrib.exchange.exceptions import MissingRate
|
||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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.validators import MinValueValidator, URLValidator
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
@ -58,12 +59,13 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
|
|
||||||
# Query the database
|
# Query the database
|
||||||
for setting in results:
|
for setting in results:
|
||||||
settings.append({
|
if setting.key:
|
||||||
"key": setting.key.upper(),
|
settings.append({
|
||||||
"value": setting.value
|
"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
|
# Specify any "default" values which are not in the database
|
||||||
for key in cls.GLOBAL_SETTINGS.keys():
|
for key in cls.GLOBAL_SETTINGS.keys():
|
||||||
@ -90,10 +92,10 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
# Numerical values remain the same
|
# Numerical values remain the same
|
||||||
elif cls.validator_is_int(validator):
|
elif cls.validator_is_int(validator):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Wrap strings with quotes
|
# Wrap strings with quotes
|
||||||
else:
|
else:
|
||||||
value = f"'{value}'"
|
value = format_html("'{}'", value)
|
||||||
|
|
||||||
setting["value"] = value
|
setting["value"] = value
|
||||||
|
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
|
|
||||||
var user_settings = {
|
var user_settings = {
|
||||||
{% for setting in USER_SETTINGS %}
|
{% for setting in USER_SETTINGS %}
|
||||||
{{ setting.key }}: {{ setting.value|safe }},
|
{{ setting.key }}: {{ setting.value }},
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
};
|
};
|
||||||
|
|
||||||
var global_settings = {
|
var global_settings = {
|
||||||
{% for setting in GLOBAL_SETTINGS %}
|
{% for setting in GLOBAL_SETTINGS %}
|
||||||
{{ setting.key }}: {{ setting.value|safe }},
|
{{ setting.key }}: {{ setting.value }},
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
};
|
};
|
10
tasks.py
10
tasks.py
@ -137,6 +137,14 @@ def rebuild(c):
|
|||||||
|
|
||||||
manage(c, "rebuild_models")
|
manage(c, "rebuild_models")
|
||||||
|
|
||||||
|
@task
|
||||||
|
def clean_settings(c):
|
||||||
|
"""
|
||||||
|
Clean the setting tables of old settings
|
||||||
|
"""
|
||||||
|
|
||||||
|
manage(c, "clean_settings")
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def migrate(c):
|
def migrate(c):
|
||||||
"""
|
"""
|
||||||
@ -167,7 +175,7 @@ def static(c):
|
|||||||
manage(c, "collectstatic --no-input")
|
manage(c, "collectstatic --no-input")
|
||||||
|
|
||||||
|
|
||||||
@task(pre=[install, migrate, static])
|
@task(pre=[install, migrate, static, clean_settings])
|
||||||
def update(c):
|
def update(c):
|
||||||
"""
|
"""
|
||||||
Update InvenTree installation.
|
Update InvenTree installation.
|
||||||
|
Loading…
Reference in New Issue
Block a user