Skip to content

Commit

Permalink
Merge pull request #16 from harrywhite4/formPrep
Browse files Browse the repository at this point in the history
Handle dict or string form values
  • Loading branch information
harrywhite4 authored Jun 10, 2020
2 parents 97158ef + c2cd75b commit 96ae78b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ venv/*
build/*
dist/*
*.pyc
.venv
.mypy_cache
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions django_jsonforms/apps.py
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'
23 changes: 13 additions & 10 deletions django_jsonforms/forms.py
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)
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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):

Expand Down
2 changes: 0 additions & 2 deletions django_jsonforms/tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
2 changes: 1 addition & 1 deletion django_jsonforms/tests/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
20 changes: 12 additions & 8 deletions django_jsonforms/tests/testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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('<p id=\"success\">Success</p>')
11 changes: 5 additions & 6 deletions django_jsonforms/tests/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,20 +9,19 @@
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):
super(JSONTestForm, self).__init__(*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):
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 96ae78b

Please sign in to comment.