32 lines
988 B
Python
32 lines
988 B
Python
import json
|
|
|
|
from django.utils.safestring import mark_safe
|
|
from django import forms
|
|
|
|
|
|
class JSONEditor(forms.Textarea):
|
|
def __init__(self, *args, **kwargs):
|
|
self.schema = kwargs.pop('schema', None)
|
|
super(JSONEditor, self).__init__(*args, **kwargs)
|
|
|
|
def render(self, name, value, attrs=None):
|
|
if self.schema:
|
|
attrs['style'] = 'display: none'
|
|
s = super(JSONEditor, self).render(name, value, attrs=attrs)
|
|
s += mark_safe('<div style="display: inline-block; width: 80%%" id="%s_editor_holder"></div>"' % attrs['id'])
|
|
s += mark_safe('''<script>
|
|
(function () {
|
|
var schema = %s;
|
|
var jsoneditor = new JSONEditor(document.getElementById("%s_editor_holder"),
|
|
{
|
|
schema: schema,
|
|
disable_properties: true,
|
|
show_errors: "always",
|
|
});
|
|
})();
|
|
</script>''' % (json.dumps(self.schema), attrs['id']))
|
|
return s
|
|
|
|
class Media:
|
|
js = ('js/jsoneditor.min.js',)
|