diff --git a/.gitignore b/.gitignore index b241183..0475c3b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ venv/* build/* dist/* *.pyc +.venv +.mypy_cache diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ef1c38..dffd151 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changes +## 1.1.2 +- Allow string or dict form data +- Update json-editor to 1.3.5 + ## 1.1.1 - Fix compatibility with django 3.0 diff --git a/django_jsonforms/apps.py b/django_jsonforms/apps.py index 2a1f235..3720aa1 100644 --- a/django_jsonforms/apps.py +++ b/django_jsonforms/apps.py @@ -1,5 +1,6 @@ from django.apps import AppConfig + class JSONFormsConfig(AppConfig): name = 'django_jsonforms' verbose_name = 'Django JSON Forms' diff --git a/django_jsonforms/forms.py b/django_jsonforms/forms.py index 54b3830..4fff5ee 100644 --- a/django_jsonforms/forms.py +++ b/django_jsonforms/forms.py @@ -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/json-editor@1.0.0/dist/jsoneditor.min.js', 'django_jsonforms/jsoneditor_init.js') + js = ( + 'https://cdn.jsdelivr.net/npm/@json-editor/json-editor@1.3.5/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): diff --git a/django_jsonforms/tests/testapp/settings.py b/django_jsonforms/tests/testapp/settings.py index 66ea3ae..9abe546 100644 --- a/django_jsonforms/tests/testapp/settings.py +++ b/django_jsonforms/tests/testapp/settings.py @@ -3,8 +3,6 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ diff --git a/django_jsonforms/tests/testapp/urls.py b/django_jsonforms/tests/testapp/urls.py index 0cebac9..e6fb7d7 100644 --- a/django_jsonforms/tests/testapp/urls.py +++ b/django_jsonforms/tests/testapp/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import include, url +from django.conf.urls import url from django_jsonforms.tests.testapp import views urlpatterns = [ diff --git a/django_jsonforms/tests/testapp/views.py b/django_jsonforms/tests/testapp/views.py index 219d3ca..1e0edf7 100644 --- a/django_jsonforms/tests/testapp/views.py +++ b/django_jsonforms/tests/testapp/views.py @@ -3,23 +3,25 @@ from django.http import HttpResponse from django.urls import reverse -from django_jsonforms.forms import JSONSchemaField, JSONSchemaForm +from django_jsonforms.forms import JSONSchemaField # Forms -class JSONTestFormStatic(Form): +class JSONTestFormStatic(Form): json = JSONSchemaField(schema='test_schema.json', options={}) + class JSONTestFormDouble(Form): json1 = JSONSchemaField(schema='test_schema.json', options={}) json2 = JSONSchemaField(schema='test_schema.json', options={}) + class JSONTestForm(Form): json = JSONSchemaField( - schema = { + schema={ 'type': 'object', 'properties': { 'color': { @@ -44,29 +46,31 @@ class JSONTestForm(Form): # Views + class JSONFormView(FormView): - template_name="form.html" + template_name = "form.html" form_class = JSONTestForm def get_success_url(self): return reverse('success') -class JSONFormViewStatic(FormView): - template_name="form.html" +class JSONFormViewStatic(FormView): + template_name = "form.html" form_class = JSONTestFormStatic def get_success_url(self): return reverse('success') -class JSONFormViewDouble(FormView): - template_name="form.html" +class JSONFormViewDouble(FormView): + template_name = "form.html" form_class = JSONTestFormDouble def get_success_url(self): return reverse('success') + def success_view(request): return HttpResponse('
Success
') diff --git a/django_jsonforms/tests/tests.py b/django_jsonforms/tests/tests.py index 6018fef..07393e2 100644 --- a/django_jsonforms/tests/tests.py +++ b/django_jsonforms/tests/tests.py @@ -1,5 +1,5 @@ from django.test import TestCase, override_settings -from django.forms import ValidationError, Form +from django.forms import Form from django_jsonforms.forms import JSONSchemaField, JSONSchemaForm from django.contrib.staticfiles.testing import StaticLiveServerTestCase from django.conf import settings @@ -9,13 +9,11 @@ from selenium.webdriver.common.by import By import json import os -import time from unittest import skipUnless -from django_jsonforms.forms import JSONSchemaField - thisdir = os.path.dirname(os.path.dirname(__file__)) + class JSONTestForm(Form): def __init__(self, schema, options, ajax=True, *args, **kwargs): @@ -23,6 +21,7 @@ def __init__(self, schema, options, ajax=True, *args, **kwargs): self.fields['json1'] = JSONSchemaField(schema=schema, options=options, ajax=ajax) self.fields['json2'] = JSONSchemaField(schema=schema, options=options, ajax=ajax) + class DjangoFormsTest(TestCase): def setUp(self): @@ -80,7 +79,6 @@ def test_render_valid_data(self): self.assertNotEqual(media.find('jsoneditor.min.js'), -1) self.assertNotEqual(media.find('jsoneditor_init.js'), -1) - def test_valid_data_for_schema_two_fields(self): form_data = {'json1': json.dumps(self.test_json), 'json2': json.dumps(self.test_json)} @@ -137,7 +135,8 @@ def test_valid_data_with_schema_file_dir_setting(self): form = JSONSchemaForm(schema='tests/testapp/staticfiles/test_schema.json', options=self.options, data=form_data) self.assertTrue(form.is_valid()) -@skipUnless(settings.SELENIUM_TEST == True, "Selenium tests not requested") + +@skipUnless(settings.SELENIUM_TEST, "Selenium tests not requested") class JSONFormsLiveTest(StaticLiveServerTestCase): @classmethod diff --git a/setup.py b/setup.py index 6ddf890..bb7ff07 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ here = path.abspath(path.dirname(__file__)) -__version__ = '1.1.1' +__version__ = '1.1.2' # Get the long description from the README file with open(path.join(here, 'README.rst'), encoding='utf-8') as f: