mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-15 20:18:17 -06:00
Closes #14690: Pretty-format JSON fields in the config form
This commit is contained in:
parent
d9a7b4ee0e
commit
7d4d347ff0
@ -207,6 +207,9 @@ class ConfigRevisionForm(BootstrapMixin, forms.ModelForm, metaclass=ConfigFormMe
|
|||||||
help_text += _(' (default)')
|
help_text += _(' (default)')
|
||||||
self.fields[param.name].help_text = help_text
|
self.fields[param.name].help_text = help_text
|
||||||
|
|
||||||
|
# Use the parameter-specific encoder
|
||||||
|
self.fields[param.name].encoder = param.encoder
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
instance = super().save(commit=False)
|
instance = super().save(commit=False)
|
||||||
|
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.postgres.forms import SimpleArrayField
|
from django.contrib.postgres.forms import SimpleArrayField
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
class PrettyJSONEncoder(json.JSONEncoder):
|
||||||
|
def __init__(self, indent, sort_keys, *args, **kwargs):
|
||||||
|
super().__init__(indent=4, sort_keys=True, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class ConfigParam:
|
class ConfigParam:
|
||||||
|
|
||||||
def __init__(self, name, label, default, description='', field=None, field_kwargs=None):
|
def __init__(self, name, label, default, description='', field=None, field_kwargs=None):
|
||||||
@ -12,6 +19,7 @@ class ConfigParam:
|
|||||||
self.field = field or forms.CharField
|
self.field = field or forms.CharField
|
||||||
self.description = description
|
self.description = description
|
||||||
self.field_kwargs = field_kwargs or {}
|
self.field_kwargs = field_kwargs or {}
|
||||||
|
self.encoder = PrettyJSONEncoder if self.field == forms.JSONField else None
|
||||||
|
|
||||||
|
|
||||||
PARAMS = (
|
PARAMS = (
|
||||||
|
Loading…
Reference in New Issue
Block a user