|
@@ -1,4 +1,5 @@
|
|
|
import json
|
|
import json
|
|
|
|
|
+import re
|
|
|
|
|
|
|
|
from django import forms
|
|
from django import forms
|
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
@@ -12,6 +13,7 @@ from django.utils.translation import gettext_lazy as _
|
|
|
from netaddr import EUI, AddrFormatError
|
|
from netaddr import EUI, AddrFormatError
|
|
|
|
|
|
|
|
from utilities.forms import widgets
|
|
from utilities.forms import widgets
|
|
|
|
|
+from utilities.forms.constants import JSONB_UNSUPPORTED_CHARS_PATTERN
|
|
|
from utilities.validators import EnhancedURLValidator
|
|
from utilities.validators import EnhancedURLValidator
|
|
|
|
|
|
|
|
__all__ = (
|
|
__all__ = (
|
|
@@ -141,6 +143,9 @@ class JSONField(_JSONField):
|
|
|
"""
|
|
"""
|
|
|
Custom wrapper around Django's built-in JSONField to avoid presenting "null" as the default text.
|
|
Custom wrapper around Django's built-in JSONField to avoid presenting "null" as the default text.
|
|
|
"""
|
|
"""
|
|
|
|
|
+ default_error_messages = {
|
|
|
|
|
+ 'invalid_unicode': _('JSON data must not contain NUL characters or unpaired Unicode surrogates.'),
|
|
|
|
|
+ }
|
|
|
empty_values = [None, '', ()]
|
|
empty_values = [None, '', ()]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
def __init__(self, *args, **kwargs):
|
|
@@ -151,6 +156,20 @@ class JSONField(_JSONField):
|
|
|
if not self.help_text:
|
|
if not self.help_text:
|
|
|
self.help_text = _('Enter context data in <a href="https://json.org/">JSON</a> format.')
|
|
self.help_text = _('Enter context data in <a href="https://json.org/">JSON</a> format.')
|
|
|
|
|
|
|
|
|
|
+ def validate(self, value):
|
|
|
|
|
+ super().validate(value)
|
|
|
|
|
+ pending = [value]
|
|
|
|
|
+ while pending:
|
|
|
|
|
+ item = pending.pop()
|
|
|
|
|
+ if isinstance(item, str):
|
|
|
|
|
+ if re.search(JSONB_UNSUPPORTED_CHARS_PATTERN, item):
|
|
|
|
|
+ raise forms.ValidationError(self.error_messages['invalid_unicode'], code='invalid_unicode')
|
|
|
|
|
+ elif isinstance(item, dict):
|
|
|
|
|
+ pending.extend(item.keys())
|
|
|
|
|
+ pending.extend(item.values())
|
|
|
|
|
+ elif isinstance(item, (list, tuple)):
|
|
|
|
|
+ pending.extend(item)
|
|
|
|
|
+
|
|
|
def prepare_value(self, value):
|
|
def prepare_value(self, value):
|
|
|
if isinstance(value, InvalidJSONInput):
|
|
if isinstance(value, InvalidJSONInput):
|
|
|
return value
|
|
return value
|
|
@@ -160,8 +179,11 @@ class JSONField(_JSONField):
|
|
|
try:
|
|
try:
|
|
|
value = json.loads(value, cls=self.decoder)
|
|
value = json.loads(value, cls=self.decoder)
|
|
|
except json.decoder.JSONDecodeError:
|
|
except json.decoder.JSONDecodeError:
|
|
|
- return f'"{value}"'
|
|
|
|
|
- return json.dumps(value, sort_keys=True, indent=4, ensure_ascii=False, cls=self.encoder)
|
|
|
|
|
|
|
+ # Serialized below as a JSON string
|
|
|
|
|
+ pass
|
|
|
|
|
+ value = json.dumps(value, sort_keys=True, indent=4, ensure_ascii=False, cls=self.encoder)
|
|
|
|
|
+ # Escape surrogates, which cannot be encoded as UTF-8
|
|
|
|
|
+ return value.encode(errors='backslashreplace').decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
class MACAddressField(forms.Field):
|
|
class MACAddressField(forms.Field):
|