├── .gitignore
├── LICENSE
├── README.md
├── README.rst
├── build.sh
├── django_jsoneditor
├── __init__.py
├── settings.py
├── settings_testapp1.py
├── settings_testapp2.py
├── urls.py
└── wsgi.py
├── jsoneditor
├── __init__.py
├── fields
│ ├── __init__.py
│ ├── django3_jsonfield.py
│ ├── django_extensions_jsonfield.py
│ ├── django_json_field.py
│ ├── django_jsonfield.py
│ ├── jsonfield.py
│ └── postgres_jsonfield.py
├── forms.py
└── static
│ ├── django-jsoneditor
│ ├── ace_options.js
│ ├── django-jsoneditor.css
│ ├── django-jsoneditor.js
│ └── init.js
│ └── jsoneditor
│ ├── img
│ ├── jsoneditor-icons.png
│ └── jsoneditor-icons.svg
│ ├── jsoneditor.css
│ └── jsoneditor.js
├── manage.py
├── setup.cfg
├── setup.py
├── testapp
├── __init__.py
├── admin.py
├── migrations
│ ├── 0001_initial.py
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
└── testapp2
├── __init__.py
├── admin.py
├── migrations
├── 0001_initial.py
└── __init__.py
├── models.py
├── tests.py
└── views.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 |
5 | # C extensions
6 | *.so
7 |
8 | # Distribution / packaging
9 | .Python
10 | env/
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | *.egg-info/
23 | .installed.cfg
24 | *.egg
25 |
26 | # PyInstaller
27 | # Usually these files are written by a python script from a template
28 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
29 | *.manifest
30 | *.spec
31 |
32 | # Installer logs
33 | pip-log.txt
34 | pip-delete-this-directory.txt
35 |
36 | # Unit test / coverage reports
37 | htmlcov/
38 | .tox/
39 | .coverage
40 | .coverage.*
41 | .cache
42 | nosetests.xml
43 | coverage.xml
44 | *,cover
45 |
46 | # Translations
47 | *.mo
48 | *.pot
49 |
50 | # Django stuff:
51 | *.log
52 |
53 | # Sphinx documentation
54 | docs/_build/
55 |
56 | # PyBuilder
57 | target/
58 |
59 | # Database
60 | *.sqlite3
61 |
62 | # IntelliJ
63 | *.idea
64 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Django-JSONEditor
2 |
3 | Django-JSONEditor is an online structured JSON input widget for Django appropriate for various JSONField's provided for Django.
4 |
5 | Code of the javascript JSONEditor online editor has been got from the http://jsoneditoronline.org/.
6 |
7 | See the latest versions of the javascript online JSON Editor here: https://github.com/josdejong/jsoneditor
8 |
9 | Sample views:
10 |
11 |
12 |
13 | ## Installation
14 |
15 | ### Latest version from the GIT repository::
16 |
17 | pip install "git+git://github.com/nnseva/django-jsoneditor.git"
18 |
19 | ### Stable version from the PyPi repository::
20 |
21 | pip install django-jsoneditor
22 |
23 | Note that you should use one of original JSONField packages to provide the JSONField itself.
24 |
25 | ## Configuration
26 |
27 | You **should** append `jsoneditor` into the `INSTALLED_APPS` of your `settings.py` file:
28 | ```python
29 | INSTALLED_APPS = (
30 | # ...
31 | 'jsoneditor',
32 | # ...
33 | )
34 | ```
35 |
36 | You **can** use CDN repositories to get JSONEditor javascript code, or host it yourself, instead of the packaged one using the following two settings in your `settings.py` file:
37 | ```python
38 | JSON_EDITOR_JS = 'whatever-your-want.js'
39 | JSON_EDITOR_CSS = 'whatever-your-want.css'
40 | ```
41 |
42 | Just look to the http://cdnjs.com/libraries/jsoneditor and select the preferred one, like:
43 | ```python
44 | JSON_EDITOR_JS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/8.6.4/jsoneditor.js'
45 | JSON_EDITOR_CSS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/8.6.4/jsoneditor.css'
46 | ```
47 |
48 | ### Custom JSONEditor initialization
49 | You **can** change initial parameters for the `jsoneditor.JSONEditor`
50 | *javascript* constructor initial call for your own purposes using
51 | `JSON_EDITOR_INIT_JS` settings. Copy the `jsoneditor/static/django-jsoneditor/init.js`
52 | file to your own static storage, change initial values of the
53 | `django_jsoneditor_init` object and setup the `JSON_EDITOR_INIT_JS`
54 | variable of the `settings` file to point your own modified copy of the
55 | file.
56 |
57 | **Note** that the django original static file subsystem is used to
58 | refer to the init file.
59 |
60 | For example, let's say your project has a `myapp` application,
61 | and you would like to init all available modes of the JSONEditor
62 | instead of two allowed by default.
63 |
64 | * copy the `jsoneditor/static/django-jsoneditor/init.js` to `myapp/static/jsoneditor-init.js` file
65 | * change content of the `myapp/static/jsoneditor-init.js` to:
66 | ```javascript
67 | django_jsoneditor_init = {
68 | mode: 'tree',
69 | modes: ['code', 'form', 'text', 'tree', 'view'] // all modes
70 | }
71 | ```
72 | * insert into your `settings.py` file the following code:
73 | ```python
74 | JSON_EDITOR_INIT_JS = "jsoneditor-init.js"
75 | ```
76 | (**note** that the static file subsystem refers to static files without `static` prefix)
77 |
78 | You can extend the `JSON_EDITOR_INIT_JS` file as you wish; it will be used on every
79 | page where the `JSONEditor` widget is used just before the `django-jsonfield.js` file.
80 |
81 | ### Custom Ace initialization
82 | In the same fashion, you can also set options for the Ace editor that is initialized when either
83 | starting with or switching to 'code' mode. These options can be found here:
84 | https://github.com/ajaxorg/ace/wiki/Configuring-Ace. This can for example come in handy when
85 | wanting to customize for example the height or looks of the editor. The default of this file can be
86 | found in `jsoneditor/static/django-jsoneditor/ace_options.js`, which is empty. A custom one can be
87 | pointed to by adding the following line to your `settings.py`:
88 | ```python
89 | JSON_EDITOR_ACE_OPTIONS_JS = "[your_ace_options_file].js"
90 | ```
91 |
92 | ### Per-field customization
93 | You can also override JSONEditor and Ace initialization on a per-field basis. To do this, pass the
94 | desired `init_options` and/or `ace_option` to the widget's initializer. For example, let's
95 | say you want to make a certain field read-only:
96 |
97 | ```python
98 | from django.contrib import admin
99 | from django.db.models.fields.json import JSONField
100 | from jsoneditor.forms import JSONEditor
101 |
102 |
103 | class MyAdmin(admin.ModelAdmin):
104 | formfield_overrides = {
105 | JSONField: {
106 | "widget": JSONEditor(
107 | init_options={"mode": "view", "modes": ["view", "code", "tree"]},
108 | ace_options={"readOnly": True},
109 | )
110 | }
111 | }
112 | ```
113 |
114 | These values will override any project-level options in the custom javascript files described above.
115 |
116 |
117 | ## Use
118 |
119 | You can use the JSONEditor widget for fields in selected Admin classes like:
120 |
121 | admin.py:
122 | ```python
123 | from django.contrib import admin
124 | from django.db.models.fields.json import JSONField
125 | from jsoneditor.forms import JSONEditor
126 |
127 |
128 | class MyAdmin(admin.ModelAdmin):
129 | formfield_overrides = {
130 | JSONField: {'widget': JSONEditor},
131 | }
132 | ```
133 |
134 | Or use the original JSONField implementation fixed by the package.
135 |
136 | Right now there are the following fixed implementations:
137 |
138 | * `jsoneditor.fields.django_json_field.JSONField` replaces a `JSONField` from https://github.com/derek-schaefer/django-json-field (**NOTE** the package is not compatible with django v.1.9)
139 | * `jsoneditor.fields.django_jsonfield.JSONField` replaces a `JSONField` from https://launchpad.net/django-jsonfield package
140 | * `jsoneditor.fields.postgres_jsonfield.JSONField` replaces `django.contrib.postgres.fields.JSONField` (**NOTE** this field type appears only from django v.1.9)
141 | * `jsoneditor.fields.django_extensions_jsonfield.JSONField` replaces `django_extensions.db.fields.json.JSONField`
142 | * `jsoneditor.fields.jsonfield` has been added for people using https://github.com/rpkilby/jsonfield (the https://github.com/bradjasper/django-jsonfield now redirects there)
143 | * `jsoneditor.fields.django3_jsonfield` uses the standard JSONField and JSONFormField provided by Django 3+
144 |
145 | To use the fixed implementation instead of the original one, just replace your import with the desired one. For example, for Django 3.0 and above:
146 |
147 | models.py:
148 | ```python
149 | from django.db import models
150 |
151 | # from json_field import JSONField replaced by:
152 | from jsoneditor.fields.django3_jsonfield import JSONField
153 | # Create your models here.
154 |
155 | class TestModel(models.Model):
156 | my_field = JSONField()
157 | ```
158 |
159 | You can access the underlying ``JSONEditor`` JS objects in your JavaScript via dictionary named ``jsonEditors``. This dictionary's keys are the IDs of the fields generated by this component in the form: ``"id"+[your form field name]+"_json_jsoneditor"``, e.g. ``id_template_parameters_json_jsoneditor``. The values in the dictionary are the instances of the correspondent JSONEditor objects.
160 |
161 |
162 | ## Jsonschema
163 |
164 | You can pass a jsonschema as an arguement to `JSONEditor` widget so that
165 | jsoneditor can validate user inputs. Example:
166 |
167 | ``` python
168 | from django import forms
169 | from jsoneditor.forms import JSONEditor
170 |
171 | class MyAdmin(admin.ModelAdmin):
172 | def formfield_for_dbfield(self, db_field, request, **kwargs):
173 | field = super().formfield_for_dbfield(db_field, request, **kwargs)
174 | if isinstance(field, forms.fields.JSONField):
175 | field.widget = JSONEditor(jsonschema={
176 | "type": "array",
177 | "items": {
178 | "type": "string"
179 | }
180 | })
181 | return field
182 |
183 | ```
184 |
185 | ## Custom Style
186 |
187 | You can pass the style in attrs params for `JSONEditor` widget so that
188 | jsoneditor render with the style what you setup. Example:
189 |
190 | ``` python
191 | from json_field import JSONField
192 | from jsoneditor.forms import JSONEditor
193 |
194 | class MyAdmin(admin.ModelAdmin):
195 | formfield_overrides = {
196 | JSONField: {'widget': JSONEditor(attrs={'style': 'width: 620px;'})}
197 | }
198 | ```
199 | ## Custom Encoders
200 |
201 | There are situations where you may prefer to use a custom [JSONEncoder](https://docs.python.org/3/library/json.html#json.JSONEncoder) class. For example, you may want to use Django's [DjangoJSONEncoder](https://docs.djangoproject.com/en/4.1/topics/serialization/#djangojsonencoder) to handle timestamps in a Django-friendly way. You can do this by passing the `encoder` param as an initialization argument.
202 |
203 | ``` python
204 | from django.core.serializers.json import DjangoJSONEncoder
205 |
206 | class MyAdmin(admin.ModelAdmin):
207 | formfield_overrides = {
208 | JSONField: {'widget': JSONEditor(encoder=DjangoJSONEncoder)} # will now encode/decode python datetime and timestamp objects!
209 | }
210 | ```
211 |
212 | ## Collecting bounties
213 |
214 | You are free to give some bounties on https://www.bountysource.com/ to force solving existent or new issues
215 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | Django-JSONEditor
2 | ===================
3 |
4 | Django-JSONEditor is an online structured JSON input widget for Django appropriate for various JSONField's provided for Django.
5 |
6 | Code of the javascript JSONEditor online editor has been got from the http://jsoneditoronline.org/.
7 |
8 | See the latest versions of the javascript online JSON Editor here: https://github.com/josdejong/jsoneditor
9 |
10 | Sample views:
11 |
12 | .. image:: https://raw.github.com/josdejong/jsoneditor/master/misc/jsoneditor.png
13 |
14 | Installation
15 | ------------
16 | Latest version from the GIT repository::
17 |
18 | pip install "git+git://github.com/nnseva/django-jsoneditor.git"
19 |
20 | Stable version from the PyPi repository::
21 |
22 | pip install django-jsoneditor
23 |
24 | See all details on https://github.com/nnseva/django-jsoneditor
25 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | rm -r dist/*
4 | python setup.py sdist bdist_wheel
5 | twine upload dist/*
6 |
--------------------------------------------------------------------------------
/django_jsoneditor/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nnseva/django-jsoneditor/dfc1ad59ef7da77c754a1beb31f07417e2a3a334/django_jsoneditor/__init__.py
--------------------------------------------------------------------------------
/django_jsoneditor/settings.py:
--------------------------------------------------------------------------------
1 | settings_testapp1.py
--------------------------------------------------------------------------------
/django_jsoneditor/settings_testapp1.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for django_jsoneditor project.
3 |
4 | Generated by 'django-admin startproject' using Django 1.8.3.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/1.8/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/1.8/ref/settings/
11 | """
12 |
13 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14 | import os
15 |
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = '1@5n@*f2ng(+il*9im)f$ie8lpc)c3an!3-3z2f9cwn*=6pzvc'
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = True
27 |
28 | ALLOWED_HOSTS = [
29 | 'django-jsoneditor-nnseva.c9users.io',
30 | '127.0.0.1',
31 | ]
32 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
33 |
34 |
35 | # Application definition
36 |
37 | INSTALLED_APPS = (
38 | 'testapp',
39 |
40 | 'jsoneditor',
41 |
42 | 'django.contrib.admin',
43 | 'django.contrib.auth',
44 | 'django.contrib.contenttypes',
45 | 'django.contrib.sessions',
46 | 'django.contrib.messages',
47 | 'django.contrib.staticfiles',
48 | )
49 |
50 | MIDDLEWARE_CLASSES = (
51 | 'django.contrib.sessions.middleware.SessionMiddleware',
52 | 'django.middleware.common.CommonMiddleware',
53 | 'django.middleware.csrf.CsrfViewMiddleware',
54 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
55 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
56 | 'django.contrib.messages.middleware.MessageMiddleware',
57 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
58 | 'django.middleware.security.SecurityMiddleware',
59 | )
60 |
61 | MIDDLEWARE = (
62 | 'django.middleware.security.SecurityMiddleware',
63 | 'django.contrib.sessions.middleware.SessionMiddleware',
64 | 'django.middleware.common.CommonMiddleware',
65 | 'django.middleware.csrf.CsrfViewMiddleware',
66 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
67 | 'django.contrib.messages.middleware.MessageMiddleware',
68 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
69 | )
70 |
71 | ROOT_URLCONF = 'django_jsoneditor.urls'
72 |
73 | TEMPLATES = [
74 | {
75 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
76 | 'DIRS': [],
77 | 'APP_DIRS': True,
78 | 'OPTIONS': {
79 | 'context_processors': [
80 | 'django.template.context_processors.debug',
81 | 'django.template.context_processors.request',
82 | 'django.contrib.auth.context_processors.auth',
83 | 'django.contrib.messages.context_processors.messages',
84 | ],
85 | },
86 | },
87 | ]
88 |
89 | WSGI_APPLICATION = 'django_jsoneditor.wsgi.application'
90 |
91 |
92 | # Database
93 | # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
94 |
95 | DATABASES = {
96 | 'default': {
97 | 'ENGINE': 'django.db.backends.sqlite3',
98 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
99 | }
100 | }
101 |
102 | # Internationalization
103 | # https://docs.djangoproject.com/en/1.8/topics/i18n/
104 |
105 | LANGUAGE_CODE = 'en-us'
106 |
107 | TIME_ZONE = 'UTC'
108 |
109 | USE_I18N = True
110 |
111 | USE_L10N = True
112 |
113 | USE_TZ = True
114 |
115 |
116 | # Static files (CSS, JavaScript, Images)
117 | # https://docs.djangoproject.com/en/1.8/howto/static-files/
118 |
119 | STATIC_URL = '/static/'
120 |
121 | #JSON_EDITOR_JS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/5.26.2/jsoneditor.js'
122 | #JSON_EDITOR_CSS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/5.26.2/jsoneditor.css'
123 |
124 | #JSON_EDITOR_JS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/5.13.3/jsoneditor.js'
125 | #JSON_EDITOR_CSS = 'https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/5.13.3/jsoneditor.css'
126 |
--------------------------------------------------------------------------------
/django_jsoneditor/settings_testapp2.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for django_jsoneditor project.
3 |
4 | Generated by 'django-admin startproject' using Django 1.8.3.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/1.8/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/1.8/ref/settings/
11 | """
12 |
13 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14 | import os
15 |
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = '1@5n@*f2ng(+il*9im)f$ie8lpc)c3an!3-3z2f9cwn*=6pzvc'
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = True
27 |
28 | ALLOWED_HOSTS = [
29 | 'django-jsoneditor-nnseva.c9users.io',
30 | '127.0.0.1',
31 | ]
32 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
33 |
34 |
35 | # Application definition
36 |
37 | INSTALLED_APPS = (
38 | 'testapp2',
39 |
40 | 'jsoneditor',
41 |
42 | 'django.contrib.admin',
43 | 'django.contrib.auth',
44 | 'django.contrib.contenttypes',
45 | 'django.contrib.sessions',
46 | 'django.contrib.messages',
47 | 'django.contrib.staticfiles',
48 | )
49 |
50 | MIDDLEWARE = (
51 | 'django.contrib.sessions.middleware.SessionMiddleware',
52 | 'django.middleware.common.CommonMiddleware',
53 | 'django.middleware.csrf.CsrfViewMiddleware',
54 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
55 | 'django.contrib.messages.middleware.MessageMiddleware',
56 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
57 | 'django.middleware.security.SecurityMiddleware',
58 | )
59 |
60 | ROOT_URLCONF = 'django_jsoneditor.urls'
61 |
62 | TEMPLATES = [
63 | {
64 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
65 | 'DIRS': [],
66 | 'APP_DIRS': True,
67 | 'OPTIONS': {
68 | 'context_processors': [
69 | 'django.template.context_processors.debug',
70 | 'django.template.context_processors.request',
71 | 'django.contrib.auth.context_processors.auth',
72 | 'django.contrib.messages.context_processors.messages',
73 | ],
74 | },
75 | },
76 | ]
77 |
78 | WSGI_APPLICATION = 'django_jsoneditor.wsgi.application'
79 |
80 |
81 | # Database
82 | # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
83 |
84 | DATABASES = {
85 | 'default': {
86 | 'ENGINE': 'django.db.backends.sqlite3',
87 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
88 | }
89 | }
90 |
91 | # Internationalization
92 | # https://docs.djangoproject.com/en/1.8/topics/i18n/
93 |
94 | LANGUAGE_CODE = 'en-us'
95 |
96 | TIME_ZONE = 'UTC'
97 |
98 | USE_I18N = True
99 |
100 | USE_L10N = True
101 |
102 | USE_TZ = True
103 |
104 |
105 | # Static files (CSS, JavaScript, Images)
106 | # https://docs.djangoproject.com/en/1.8/howto/static-files/
107 |
108 | STATIC_URL = '/static/'
109 |
--------------------------------------------------------------------------------
/django_jsoneditor/urls.py:
--------------------------------------------------------------------------------
1 | """django_jsoninput URL Configuration
2 |
3 | The `urlpatterns` list routes URLs to views. For more information please see:
4 | https://docs.djangoproject.com/en/1.8/topics/http/urls/
5 | Examples:
6 | Function views
7 | 1. Add an import: from my_app import views
8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9 | Class-based views
10 | 1. Add an import: from other_app.views import Home
11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12 | Including another URLconf
13 | 1. Add an import: from blog import urls as blog_urls
14 | 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
15 | """
16 | from django.urls import include, re_path
17 | from django.contrib import admin
18 |
19 | urlpatterns = [
20 | re_path(r'^admin/', admin.site.urls),
21 | ]
22 |
--------------------------------------------------------------------------------
/django_jsoneditor/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for django_jsoninput project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.wsgi import get_wsgi_application
13 |
14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_jsoneditor.settings")
15 |
16 | application = get_wsgi_application()
17 |
--------------------------------------------------------------------------------
/jsoneditor/__init__.py:
--------------------------------------------------------------------------------
1 | __version__ = "0.2.4"
2 |
--------------------------------------------------------------------------------
/jsoneditor/fields/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nnseva/django-jsoneditor/dfc1ad59ef7da77c754a1beb31f07417e2a3a334/jsoneditor/fields/__init__.py
--------------------------------------------------------------------------------
/jsoneditor/fields/django3_jsonfield.py:
--------------------------------------------------------------------------------
1 | from django.db.models import JSONField as _JSONField
2 | from django.forms import JSONField as _JSONFormField
3 |
4 | from jsoneditor.forms import JSONEditor
5 |
6 | class JSONFormField(_JSONFormField):
7 | widget = JSONEditor
8 | def __init__(self,*av,**kw):
9 | kw['widget'] = self.widget # force avoiding widget override
10 | super(JSONFormField,self).__init__(*av,**kw)
11 |
12 | class JSONField(_JSONField):
13 | def formfield(self, **kwargs):
14 | defaults = {
15 | 'form_class': kwargs.get('form_class', JSONFormField),
16 | }
17 | defaults.update(kwargs)
18 | return super(JSONField, self).formfield(**defaults)
19 |
--------------------------------------------------------------------------------
/jsoneditor/fields/django_extensions_jsonfield.py:
--------------------------------------------------------------------------------
1 | from django_extensions.db.fields.json import JSONField as _JSONField
2 | from django import forms
3 | from django.core.exceptions import ValidationError
4 |
5 | from jsoneditor.forms import JSONEditor
6 |
7 | import json
8 | import six
9 |
10 | class JSONFormField(forms.CharField):
11 | widget = JSONEditor
12 | def __init__(self,*av,**kw):
13 | kw['widget'] = self.widget # force avoiding widget override
14 | super(JSONFormField,self).__init__(*av,**kw)
15 |
16 | class JSONField(_JSONField):
17 | def formfield(self, **kwargs):
18 | defaults = {
19 | 'form_class': kwargs.get('form_class', JSONFormField),
20 | }
21 | defaults.update(kwargs)
22 | return super(JSONField, self).formfield(**defaults)
23 |
24 | def to_python(self, value):
25 | if isinstance(value, six.string_types):
26 | try:
27 | json.loads(value)
28 | except json.decoder.JSONDecodeError as ex:
29 | raise ValidationError(ex)
30 | return super(JSONField, self).to_python(value)
31 |
--------------------------------------------------------------------------------
/jsoneditor/fields/django_json_field.py:
--------------------------------------------------------------------------------
1 | from json_field import JSONField as _JSONField
2 | from json_field.forms import JSONFormField as _JSONFormField
3 |
4 | from jsoneditor.forms import JSONEditor
5 |
6 | class JSONFormField(_JSONFormField):
7 | widget = JSONEditor
8 | def __init__(self,*av,**kw):
9 | kw['widget'] = self.widget # force avoiding widget override
10 | super(JSONFormField,self).__init__(*av,**kw)
11 |
12 | class JSONField(_JSONField):
13 | def formfield(self, **kwargs):
14 | defaults = {
15 | 'form_class': kwargs.get('form_class', JSONFormField),
16 | }
17 | defaults.update(kwargs)
18 | return super(JSONField, self).formfield(**defaults)
19 |
--------------------------------------------------------------------------------
/jsoneditor/fields/django_jsonfield.py:
--------------------------------------------------------------------------------
1 | from jsonfield import JSONField as _JSONField
2 | from jsonfield.fields import JSONFormField as _JSONFormField
3 |
4 | from jsoneditor.forms import JSONEditor
5 |
6 | class JSONFormField(_JSONFormField):
7 | widget = JSONEditor
8 | def __init__(self,*av,**kw):
9 | kw['widget'] = self.widget # force avoiding widget override
10 | super(JSONFormField,self).__init__(*av,**kw)
11 |
12 | class JSONField(_JSONField):
13 | def formfield(self, **kwargs):
14 | defaults = {
15 | 'form_class': kwargs.get('form_class', JSONFormField),
16 | }
17 | defaults.update(kwargs)
18 | return super(JSONField, self).formfield(**defaults)
19 |
--------------------------------------------------------------------------------
/jsoneditor/fields/jsonfield.py:
--------------------------------------------------------------------------------
1 | from jsonfield import JSONField as _JSONField
2 | from jsonfield.forms import JSONField as _JSONFormField
3 |
4 | from jsoneditor.forms import JSONEditor
5 |
6 | class JSONFormField(_JSONFormField):
7 | widget = JSONEditor
8 | def __init__(self,*av,**kw):
9 | kw['widget'] = self.widget # force avoiding widget override
10 | super(JSONFormField,self).__init__(*av,**kw)
11 |
12 | class JSONField(_JSONField):
13 | def formfield(self, **kwargs):
14 | defaults = {
15 | 'form_class': kwargs.get('form_class', JSONFormField),
16 | }
17 | defaults.update(kwargs)
18 | return super(JSONField, self).formfield(**defaults)
19 |
--------------------------------------------------------------------------------
/jsoneditor/fields/postgres_jsonfield.py:
--------------------------------------------------------------------------------
1 | from django.contrib.postgres.fields import JSONField as _JSONField
2 | from django.contrib.postgres.forms import JSONField as _JSONFormField
3 |
4 | from jsoneditor.forms import JSONEditor
5 |
6 | class JSONFormField(_JSONFormField):
7 | widget = JSONEditor
8 | def __init__(self,*av,**kw):
9 | kw['widget'] = self.widget # force avoiding widget override
10 | super(JSONFormField,self).__init__(*av,**kw)
11 |
12 | class JSONField(_JSONField):
13 | def formfield(self, **kwargs):
14 | defaults = {
15 | 'form_class': kwargs.get('form_class', JSONFormField),
16 | }
17 | defaults.update(kwargs)
18 | return super(JSONField, self).formfield(**defaults)
19 |
--------------------------------------------------------------------------------
/jsoneditor/forms.py:
--------------------------------------------------------------------------------
1 | import json
2 | from packaging import version
3 |
4 | import django
5 | from django.conf import settings
6 | from django.forms.widgets import Textarea
7 | from django.utils.safestring import mark_safe
8 | from django.core.serializers.json import DjangoJSONEncoder
9 |
10 |
11 | try:
12 | from django.forms.util import flatatt
13 | except ImportError:
14 | from django.forms.utils import flatatt
15 |
16 | try:
17 | unicode = unicode
18 | except NameError:
19 | # 'unicode' is undefined, must be Python 3
20 | str = str
21 | unicode = str
22 | bytes = bytes
23 | basestring = (str, bytes)
24 | else:
25 | # 'unicode' exists, must be Python 2
26 | str = str
27 | unicode = unicode
28 | bytes = str
29 | basestring = basestring
30 |
31 |
32 | class JSONEditor(Textarea):
33 | class Media:
34 | js = (
35 | 'admin/js/vendor/jquery/jquery.js',
36 | 'admin/js/jquery.init.js',
37 | getattr(settings, "JSON_EDITOR_JS", 'jsoneditor/jsoneditor.js'),
38 | getattr(settings, "JSON_EDITOR_ACE_OPTIONS_JS", 'django-jsoneditor/ace_options.js'),
39 | getattr(settings, "JSON_EDITOR_INIT_JS", 'django-jsoneditor/init.js'),
40 | 'django-jsoneditor/django-jsoneditor.js',
41 | )
42 | css = {
43 | 'all': (
44 | getattr(settings, "JSON_EDITOR_CSS", 'jsoneditor/jsoneditor.css'),
45 | 'django-jsoneditor/django-jsoneditor.css',
46 | )
47 | }
48 |
49 | def __init__(self, *args, **kwargs):
50 | self.jsonschema = kwargs.pop('jsonschema', None)
51 | self.init_options = kwargs.pop('init_options', None)
52 | self.ace_options = kwargs.pop('ace_options', None)
53 | self.encoder = kwargs.pop("encoder", None)
54 | super().__init__(*args, **kwargs)
55 |
56 | def render(self, name, value, attrs=None, renderer=None):
57 | attrs['jsonschema'] = json.dumps(self.jsonschema)
58 | attrs['init_options'] = json.dumps(self.init_options)
59 | attrs['ace_options'] = json.dumps(self.ace_options)
60 |
61 | if not isinstance(value, basestring):
62 | value = json.dumps(value, cls=self.encoder)
63 |
64 | input_attrs = {'hidden': True}
65 | input_attrs.update(attrs)
66 | if 'class' not in input_attrs:
67 | input_attrs['class'] = 'for_jsoneditor'
68 | else:
69 | input_attrs['class'] += ' for_jsoneditor'
70 | r = super(JSONEditor, self).render(name, value, input_attrs)
71 | div_attrs = {}
72 | div_attrs.update(attrs)
73 | div_attrs.update({'id': (attrs['id'] + '_jsoneditor')})
74 | if version.parse(django.get_version()) >= version.parse("1.11"):
75 | final_attrs = self.build_attrs(div_attrs, extra_attrs={'name': name})
76 | else:
77 | final_attrs = self.build_attrs(div_attrs, name=name)
78 | r += '''
79 |