zoo_data: correction à l'éditeur JSON (fixes #21278)

This commit is contained in:
Benjamin Dauvergne 2018-01-19 15:16:10 +01:00
parent e15a8382a7
commit 954cee2467
2 changed files with 20 additions and 16 deletions

View File

@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib import admin
from django.forms import ModelForm
from rangefilter.filter import DateTimeRangeFilter
@ -22,22 +23,29 @@ from .models import Transaction, Entity, Relation, Log, Job
from .widgets import JSONEditor
class JSONEditorForm(ModelForm):
def __init__(self, *args, **kwargs):
super(JSONEditorForm, self).__init__(*args, **kwargs)
if self.instance and hasattr(self.instance, 'schema'):
for key in self.fields:
if key == 'content':
self.fields[key].widget = JSONEditor(schema=self.instance.schema.schema)
class JSONEditorMixin(object):
json_fields = ()
form = JSONEditorForm
def get_form(self, request, obj=None, **kwargs):
widgets = kwargs.setdefault('widgets', {})
for field in self.json_fields:
widgets[field] = JSONEditor()
if obj:
schema = obj.schema.schema
else:
schema = {}
widgets[field] = JSONEditor(schema=schema)
return super(JSONEditorMixin, self).get_form(request, obj=obj, **kwargs)
def get_formset(self, request, obj=None, **kwargs):
widgets = kwargs.setdefault('widgets', {})
for field in self.json_fields:
widgets[field] = JSONEditor()
return super(JSONEditorMixin, self).get_formset(request, obj=obj, **kwargs)
class LeftRelationInlineAdmin(JSONEditorMixin, admin.TabularInline):
fk_name = 'left'

View File

@ -28,11 +28,8 @@ class JSONEditor(forms.Textarea):
def render(self, name, value, attrs=None):
default_schema = {
'type': 'object',
'additionalProperties': {
'type': 'string'
}
'additionalProperties': True,
}
default_schema = {}
attrs['style'] = 'display: none'
s = super(JSONEditor, self).render(name, value, attrs=attrs)
s += mark_safe(
@ -46,11 +43,10 @@ class JSONEditor(forms.Textarea):
{
theme: "foundation",
schema: schema,
disable_properties: true,
show_errors: "always",
});
input = document.getElementById("%(id)s");
content = document.getElementById("%(id)s").value;
var input = document.getElementById("%(id)s");
var content = document.getElementById("%(id)s").value;
jsoneditor.on('change', function () {
input.value = JSON.stringify(jsoneditor.getValue());
})
@ -59,7 +55,7 @@ class JSONEditor(forms.Textarea):
}
})();
</script>''' % {
'schema': json.dumps(self.schema or default_schema),
'schema': json.dumps(self.schema or default_schema),
'id': attrs['id'],
})
return s