Fix system config export

This commit is contained in:
Jeremy Stretch 2024-08-27 13:56:38 -04:00
parent 3c2af97823
commit ffbc8b4b41
2 changed files with 4 additions and 2 deletions

View File

@ -574,13 +574,14 @@ class SystemView(UserPassesTestMixin, View):
k: getattr(config, k) for k in sorted(params)
},
}
response = HttpResponse(json.dumps(data, indent=4), content_type='text/json')
response = HttpResponse(json.dumps(data, cls=ConfigJSONEncoder, indent=4), content_type='text/json')
response['Content-Disposition'] = 'attachment; filename="netbox.json"'
return response
plugins_table = tables.PluginTable(plugins, orderable=False)
plugins_table.configure(request)
# Serialize any CustomValidator classes
if hasattr(config, 'CUSTOM_VALIDATORS') and config.CUSTOM_VALIDATORS:
config.CUSTOM_VALIDATORS = json.dumps(config.CUSTOM_VALIDATORS, cls=ConfigJSONEncoder, indent=4)

View File

@ -20,11 +20,12 @@ class CustomFieldJSONEncoder(DjangoJSONEncoder):
class ConfigJSONEncoder(DjangoJSONEncoder):
"""
Override Django's built-in JSON encoder to save python functions as function names.
Override Django's built-in JSON encoder to serialize CustomValidator classes as strings.
"""
def default(self, o):
from extras.validators import CustomValidator
if issubclass(type(o), CustomValidator):
return type(o).__name__
return super().default(o)