Merge pull request #2296 from SchrodingersGat/requires-restart

Requires restart
This commit is contained in:
Oliver 2021-11-12 16:02:31 +11:00 committed by GitHub
commit 1deb0f2c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 4 deletions

View File

@ -12,11 +12,31 @@ class SettingsAdmin(ImportExportModelAdmin):
list_display = ('key', 'value')
def get_readonly_fields(self, request, obj=None):
"""
Prevent the 'key' field being edited once the setting is created
"""
if obj:
return ['key']
else:
return []
class UserSettingsAdmin(ImportExportModelAdmin):
list_display = ('key', 'value', 'user', )
def get_readonly_fields(self, request, obj=None):
"""
Prevent the 'key' field being edited once the setting is created
"""
if obj:
return ['key']
else:
return []
class NotificationEntryAdmin(admin.ModelAdmin):

View File

@ -1,10 +1,30 @@
# -*- coding: utf-8 -*-
import logging
from django.apps import AppConfig
logger = logging.getLogger('inventree')
class CommonConfig(AppConfig):
name = 'common'
def ready(self):
pass
self.clear_restart_flag()
def clear_restart_flag(self):
"""
Clear the SERVER_RESTART_REQUIRED setting
"""
try:
import common.models
if common.models.InvenTreeSetting.get_setting('SERVER_RESTART_REQUIRED'):
logger.info("Clearing SERVER_RESTART_REQUIRED flag")
common.models.InvenTreeSetting.set_setting('SERVER_RESTART_REQUIRED', False, None)
except:
pass

View File

@ -63,13 +63,15 @@ class BaseInvenTreeSetting(models.Model):
Enforce validation and clean before saving
"""
self.key = str(self.key).upper()
self.clean()
self.validate_unique()
super().save()
@classmethod
def allValues(cls, user=None):
def allValues(cls, user=None, exclude_hidden=False):
"""
Return a dict of "all" defined global settings.
@ -94,9 +96,15 @@ class BaseInvenTreeSetting(models.Model):
for key in cls.GLOBAL_SETTINGS.keys():
if key.upper() not in settings:
settings[key.upper()] = cls.get_setting_default(key)
if exclude_hidden:
hidden = cls.GLOBAL_SETTINGS[key].get('hidden', False)
if hidden:
# Remove hidden items
del settings[key.upper()]
for key, value in settings.items():
validator = cls.get_setting_validator(key)
@ -545,6 +553,17 @@ class InvenTreeSetting(BaseInvenTreeSetting):
even if that key does not exist.
"""
def save(self, *args, **kwargs):
"""
When saving a global setting, check to see if it requires a server restart.
If so, set the "SERVER_RESTART_REQUIRED" setting to True
"""
super().save()
if self.requires_restart():
InvenTreeSetting.set_setting('SERVER_REQUIRES_RESTART', True, None)
"""
Dict of all global settings values:
@ -563,6 +582,14 @@ class InvenTreeSetting(BaseInvenTreeSetting):
GLOBAL_SETTINGS = {
'SERVER_RESTART_REQUIRED': {
'name': _('Restart required'),
'description': _('A setting has been changed which requires a server restart'),
'default': False,
'validator': bool,
'hidden': True,
},
'INVENTREE_INSTANCE': {
'name': _('InvenTree Instance Name'),
'default': 'InvenTree server',
@ -936,6 +963,18 @@ class InvenTreeSetting(BaseInvenTreeSetting):
return self.__class__.get_setting(self.key)
def requires_restart(self):
"""
Return True if this setting requires a server restart after changing
"""
options = InvenTreeSetting.GLOBAL_SETTINGS.get(self.key, None)
if options:
return options.get('requires_restart', False)
else:
return False
class InvenTreeUserSetting(BaseInvenTreeSetting):
"""

View File

@ -251,6 +251,15 @@ def global_settings(*args, **kwargs):
return InvenTreeSetting.allValues()
@register.simple_tag()
def visible_global_settings(*args, **kwargs):
"""
Return any global settings which are not marked as 'hidden'
"""
return InvenTreeSetting.allValues(exclude_hidden=True)
@register.simple_tag()
def progress_bar(val, max, *args, **kwargs):
"""

View File

@ -5,6 +5,7 @@
{% settings_value 'BARCODE_ENABLE' as barcodes %}
{% settings_value 'REPORT_ENABLE_TEST_REPORT' as test_report_enabled %}
{% settings_value "REPORT_ENABLE" as report_enabled %}
{% settings_value "SERVER_RESTART_REQUIRED" as server_restart_required %}
<!DOCTYPE html>
<html lang="en">
@ -86,6 +87,21 @@
</div>
<main class='col ps-md-2 pt-2 pe-2'>
{% if server_restart_required %}
<div class='notification-area' id='restart-required'>
<div id='alert-restart-server' class='alert alert-danger' role='alert'>
<span class='fas fa-server'></span>
<b>{% trans "Server Restart Required" %}</b>
<small>
<br>
{% trans "A configuration option has been changed which requires a server restart" %}.
<br>
{% trans "Contact your system administrator for further information" %}
</small>
</div>
</div>
{% endif %}
{% block alerts %}
<div class='notification-area' id='alerts'>
<!-- Div for displayed alerts -->

View File

@ -13,7 +13,7 @@ const user_settings = {
{% endfor %}
};
{% global_settings as GLOBAL_SETTINGS %}
{% visible_global_settings as GLOBAL_SETTINGS %}
const global_settings = {
{% for key, value in GLOBAL_SETTINGS.items %}
{{ key }}: {% primitive_to_javascript value %},