From 1e317f82f5a2c56474441f06a746add5bd4395e2 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Wed, 27 Oct 2021 15:52:14 -0400 Subject: [PATCH] Move config parameter value population to ConfigRevisionForm init --- netbox/extras/forms/config.py | 31 ++++++++++++++++++------------ netbox/netbox/config/__init__.py | 7 +++---- netbox/netbox/config/parameters.py | 2 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/netbox/extras/forms/config.py b/netbox/extras/forms/config.py index 3d44280ec..fab6fdbd1 100644 --- a/netbox/extras/forms/config.py +++ b/netbox/extras/forms/config.py @@ -14,25 +14,14 @@ EMPTY_VALUES = ('', None, [], ()) class FormMetaclass(forms.models.ModelFormMetaclass): def __new__(mcs, name, bases, attrs): - config = get_config() # Emulate a declared field for each supported configuration parameter param_fields = {} for param in PARAMS: - is_static = hasattr(settings, param.name) - help_text = f'{param.description}
' if param.description else '' - value = getattr(config, param.name) - if value: - help_text += f'Current value: {value}' - if is_static: - help_text += ' (defined statically)' - elif value == param.default: - help_text += ' (default)' field_kwargs = { 'required': False, - 'disabled': is_static, 'label': param.label, - 'help_text': help_text, + 'help_text': param.description, } field_kwargs.update(**param.field_kwargs) param_fields[param.name] = param.field(**field_kwargs) @@ -50,6 +39,24 @@ class ConfigRevisionForm(forms.BaseModelForm, metaclass=FormMetaclass): 'comment': forms.Textarea(), } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Append current parameter values to form field help texts and check for static configurations + config = get_config() + for param in PARAMS: + value = getattr(config, param.name) + is_static = hasattr(settings, param.name) + if value: + help_text = f'
Current value: {value}' + if is_static: + help_text += ' (defined statically)' + elif value == param.default: + help_text += ' (default)' + self.fields[param.name].help_text += help_text + if is_static: + self.fields[param.name].disabled = True + def save(self, commit=True): instance = super().save(commit=False) diff --git a/netbox/netbox/config/__init__.py b/netbox/netbox/config/__init__.py index 9e015a891..a9a93636c 100644 --- a/netbox/netbox/config/__init__.py +++ b/netbox/netbox/config/__init__.py @@ -78,16 +78,15 @@ class Config: try: revision = ConfigRevision.objects.last() + if revision is None: + logger.debug("No previous configuration found in database; proceeding with default values") + return logger.debug("Loaded configuration data from database") except DatabaseError: # The database may not be available yet (e.g. when running a management command) logger.warning(f"Skipping config initialization (database unavailable)") return - if revision is None: - logger.debug("No previous configuration found in database; proceeding with default values") - return - revision.activate() logger.debug("Filled cache with data from latest ConfigRevision") self._populate_from_cache() diff --git a/netbox/netbox/config/parameters.py b/netbox/netbox/config/parameters.py index f889c2d20..8bf1d6dc5 100644 --- a/netbox/netbox/config/parameters.py +++ b/netbox/netbox/config/parameters.py @@ -4,7 +4,7 @@ from django.contrib.postgres.forms import SimpleArrayField class ConfigParam: - def __init__(self, name, label, default, description=None, field=None, field_kwargs=None): + def __init__(self, name, label, default, description='', field=None, field_kwargs=None): self.name = name self.label = label self.default = default