Skip to content

Commit f5dd7d8

Browse files
13809 fix ConfigRevision edit if custom validators (#13825)
* 13809 fix ConfigRevision edit, check if custom validator JSON serializable * 13809 check json rendering for all fields * Refactor field initialization logic to more cleanly handle statically configured values --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent a1e42da commit f5dd7d8

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

netbox/extras/forms/model_forms.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -518,22 +518,34 @@ def __init__(self, *args, **kwargs):
518518
config = get_config()
519519
for param in PARAMS:
520520
value = getattr(config, param.name)
521-
is_static = hasattr(settings, param.name)
522-
if value:
523-
help_text = self.fields[param.name].help_text
524-
if help_text:
525-
help_text += '<br />' # Line break
526-
help_text += _('Current value: <strong>{value}</strong>').format(value=value)
527-
if is_static:
528-
help_text += _(' (defined statically)')
529-
elif value == param.default:
530-
help_text += _(' (default)')
531-
self.fields[param.name].help_text = help_text
521+
522+
# Set the field's initial value, if it can be serialized. (This may not be the case e.g. for
523+
# CUSTOM_VALIDATORS, which may reference Python objects.)
524+
try:
525+
json.dumps(value)
532526
if type(value) in (tuple, list):
533-
value = ', '.join(value)
534-
self.fields[param.name].initial = value
535-
if is_static:
527+
self.fields[param.name].initial = ', '.join(value)
528+
else:
529+
self.fields[param.name].initial = value
530+
except TypeError:
531+
pass
532+
533+
# Check whether this parameter is statically configured (e.g. in configuration.py)
534+
if hasattr(settings, param.name):
536535
self.fields[param.name].disabled = True
536+
self.fields[param.name].help_text = _(
537+
'This parameter has been defined statically and cannot be modified.'
538+
)
539+
continue
540+
541+
# Set the field's help text
542+
help_text = self.fields[param.name].help_text
543+
if help_text:
544+
help_text += '<br />' # Line break
545+
help_text += _('Current value: <strong>{value}</strong>').format(value=value or '&mdash;')
546+
if value == param.default:
547+
help_text += _(' (default)')
548+
self.fields[param.name].help_text = help_text
537549

538550
def save(self, commit=True):
539551
instance = super().save(commit=False)

0 commit comments

Comments
 (0)