-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from harrywhite4/formPrep
Handle dict or string form values
- Loading branch information
Showing
9 changed files
with
39 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ venv/* | |
build/* | ||
dist/* | ||
*.pyc | ||
.venv | ||
.mypy_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class JSONFormsConfig(AppConfig): | ||
name = 'django_jsonforms' | ||
verbose_name = 'Django JSON Forms' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
from django import forms | ||
from django.forms import fields, ValidationError | ||
from django.forms.widgets import Textarea, Widget | ||
from django.forms.widgets import Widget | ||
import jsonschema | ||
import os | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.conf import settings | ||
|
||
try: | ||
import json | ||
except ImportError: | ||
from django.utils import simplejson as json | ||
import json | ||
|
||
|
||
class JSONEditorWidget(Widget): | ||
|
||
template_name = 'django_jsonforms/jsoneditor.html' | ||
|
||
class Media: | ||
js = ('https://cdn.jsdelivr.net/npm/@json-editor/[email protected]/dist/jsoneditor.min.js', 'django_jsonforms/jsoneditor_init.js') | ||
js = ( | ||
'https://cdn.jsdelivr.net/npm/@json-editor/[email protected]/dist/jsoneditor.min.js', | ||
'django_jsonforms/jsoneditor_init.js' | ||
) | ||
|
||
def __init__(self, schema, options, *args, **kwargs): | ||
super(JSONEditorWidget, self).__init__(*args, **kwargs) | ||
|
@@ -42,6 +41,7 @@ def get_context(self, name, value, attrs): | |
context['widget']['type'] = 'hidden' | ||
return context | ||
|
||
|
||
class JSONSchemaField(fields.CharField): | ||
|
||
def __init__(self, schema, options, ajax=True, *args, **kwargs): | ||
|
@@ -75,7 +75,7 @@ def to_python(self, value): | |
if isinstance(value, str): | ||
try: | ||
return json.loads(value) | ||
except: | ||
except json.JSONDecodeError: | ||
raise ValidationError('Invalid JSON') | ||
return value | ||
|
||
|
@@ -92,7 +92,10 @@ def clean(self, value): | |
return value | ||
|
||
def prepare_value(self, value): | ||
return json.dumps(value) | ||
if isinstance(value, dict): | ||
return json.dumps(value) | ||
return value | ||
|
||
|
||
class JSONSchemaForm(forms.Form): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters