2015-05-31 05:13:58 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
from django import forms
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-05-31 05:13:58 +08:00
|
|
|
|
|
|
|
__all__ = ['JSONField']
|
|
|
|
|
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
class InvalidJSONInput(str):
|
2016-03-27 03:11:57 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
class JSONString(str):
|
2016-11-12 09:07:31 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-31 05:13:58 +08:00
|
|
|
class JSONField(forms.CharField):
|
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _("'%(value)s' value must be valid JSON."),
|
|
|
|
}
|
2016-06-13 20:09:54 +08:00
|
|
|
widget = forms.Textarea
|
2015-05-31 05:13:58 +08:00
|
|
|
|
|
|
|
def to_python(self, value):
|
2016-07-26 20:18:08 +08:00
|
|
|
if self.disabled:
|
|
|
|
return value
|
2015-05-31 05:13:58 +08:00
|
|
|
if value in self.empty_values:
|
|
|
|
return None
|
2016-11-12 09:07:31 +08:00
|
|
|
elif isinstance(value, (list, dict, int, float, JSONString)):
|
|
|
|
return value
|
2015-05-31 05:13:58 +08:00
|
|
|
try:
|
2016-11-12 09:07:31 +08:00
|
|
|
converted = json.loads(value)
|
2015-05-31 05:13:58 +08:00
|
|
|
except ValueError:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['invalid'],
|
|
|
|
code='invalid',
|
|
|
|
params={'value': value},
|
|
|
|
)
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(converted, str):
|
2016-11-12 09:07:31 +08:00
|
|
|
return JSONString(converted)
|
|
|
|
else:
|
|
|
|
return converted
|
2015-05-31 05:13:58 +08:00
|
|
|
|
2016-03-27 03:11:57 +08:00
|
|
|
def bound_data(self, data, initial):
|
|
|
|
if self.disabled:
|
|
|
|
return initial
|
|
|
|
try:
|
|
|
|
return json.loads(data)
|
|
|
|
except ValueError:
|
|
|
|
return InvalidJSONInput(data)
|
|
|
|
|
2015-05-31 05:13:58 +08:00
|
|
|
def prepare_value(self, value):
|
2016-03-27 03:11:57 +08:00
|
|
|
if isinstance(value, InvalidJSONInput):
|
|
|
|
return value
|
2015-05-31 05:13:58 +08:00
|
|
|
return json.dumps(value)
|