mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-07 16:18:16 -06:00
17219 use custom json encoder
This commit is contained in:
parent
6d84241db9
commit
3c2af97823
@ -32,6 +32,7 @@ from netbox.views.generic.base import BaseObjectView
|
|||||||
from netbox.views.generic.mixins import TableMixin
|
from netbox.views.generic.mixins import TableMixin
|
||||||
from utilities.forms import ConfirmationForm
|
from utilities.forms import ConfirmationForm
|
||||||
from utilities.htmx import htmx_partial
|
from utilities.htmx import htmx_partial
|
||||||
|
from utilities.json import ConfigJSONEncoder
|
||||||
from utilities.query import count_related
|
from utilities.query import count_related
|
||||||
from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, register_model_view
|
from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, register_model_view
|
||||||
from . import filtersets, forms, tables
|
from . import filtersets, forms, tables
|
||||||
@ -523,23 +524,6 @@ class SystemView(UserPassesTestMixin, View):
|
|||||||
def test_func(self):
|
def test_func(self):
|
||||||
return self.request.user.is_staff
|
return self.request.user.is_staff
|
||||||
|
|
||||||
def map_validators(self, validator):
|
|
||||||
if isinstance(validator, dict):
|
|
||||||
for k, v in validator.items():
|
|
||||||
if isinstance(v, tuple):
|
|
||||||
validator[k] = type(v[0]).__name__
|
|
||||||
else:
|
|
||||||
validator[k] = self.map_validators(v)
|
|
||||||
|
|
||||||
elif isinstance(validator, list):
|
|
||||||
for index, v in enumerate(validator):
|
|
||||||
validator[index] = self.map_validators(v)
|
|
||||||
|
|
||||||
elif issubclass(type(validator), CustomValidator):
|
|
||||||
return type(validator).__name__
|
|
||||||
|
|
||||||
return validator
|
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
|
|
||||||
# System stats
|
# System stats
|
||||||
@ -578,10 +562,6 @@ class SystemView(UserPassesTestMixin, View):
|
|||||||
# Fall back to using the active config data if no record is found
|
# Fall back to using the active config data if no record is found
|
||||||
config = get_config()
|
config = get_config()
|
||||||
|
|
||||||
# If Custom Validators is function (in configuration.py) get function name
|
|
||||||
if hasattr(config, 'CUSTOM_VALIDATORS') and config.CUSTOM_VALIDATORS:
|
|
||||||
config.CUSTOM_VALIDATORS = self.map_validators(config.CUSTOM_VALIDATORS)
|
|
||||||
|
|
||||||
# Raw data export
|
# Raw data export
|
||||||
if 'export' in request.GET:
|
if 'export' in request.GET:
|
||||||
params = [param.name for param in PARAMS]
|
params = [param.name for param in PARAMS]
|
||||||
@ -601,6 +581,9 @@ class SystemView(UserPassesTestMixin, View):
|
|||||||
plugins_table = tables.PluginTable(plugins, orderable=False)
|
plugins_table = tables.PluginTable(plugins, orderable=False)
|
||||||
plugins_table.configure(request)
|
plugins_table.configure(request)
|
||||||
|
|
||||||
|
if hasattr(config, 'CUSTOM_VALIDATORS') and config.CUSTOM_VALIDATORS:
|
||||||
|
config.CUSTOM_VALIDATORS = json.dumps(config.CUSTOM_VALIDATORS, cls=ConfigJSONEncoder, indent=4)
|
||||||
|
|
||||||
return render(request, 'core/system.html', {
|
return render(request, 'core/system.html', {
|
||||||
'stats': stats,
|
'stats': stats,
|
||||||
'plugins_table': plugins_table,
|
'plugins_table': plugins_table,
|
||||||
|
@ -95,7 +95,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th scope="row" class="ps-3">{% trans "Custom validators" %}</th>
|
<th scope="row" class="ps-3">{% trans "Custom validators" %}</th>
|
||||||
{% if config.CUSTOM_VALIDATORS %}
|
{% if config.CUSTOM_VALIDATORS %}
|
||||||
<td><pre>{{ config.CUSTOM_VALIDATORS|json }}</pre></td>
|
<td><pre>{{ config.CUSTOM_VALIDATORS }}</pre></td>
|
||||||
{% else %}
|
{% else %}
|
||||||
<td>{{ ''|placeholder }}</td>
|
<td>{{ ''|placeholder }}</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -3,6 +3,7 @@ import decimal
|
|||||||
from django.core.serializers.json import DjangoJSONEncoder
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
'ConfigJSONEncoder',
|
||||||
'CustomFieldJSONEncoder',
|
'CustomFieldJSONEncoder',
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,3 +16,15 @@ class CustomFieldJSONEncoder(DjangoJSONEncoder):
|
|||||||
if isinstance(o, decimal.Decimal):
|
if isinstance(o, decimal.Decimal):
|
||||||
return float(o)
|
return float(o)
|
||||||
return super().default(o)
|
return super().default(o)
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigJSONEncoder(DjangoJSONEncoder):
|
||||||
|
"""
|
||||||
|
Override Django's built-in JSON encoder to save python functions as function names.
|
||||||
|
"""
|
||||||
|
def default(self, o):
|
||||||
|
from extras.validators import CustomValidator
|
||||||
|
|
||||||
|
if issubclass(type(o), CustomValidator):
|
||||||
|
return type(o).__name__
|
||||||
|
return super().default(o)
|
||||||
|
Loading…
Reference in New Issue
Block a user